-1

I need to send data using json_encode. I just get the reference below:

$key = 'my_key';
$url = 'url';

$data = array (
   'username' => 'user', 
   'api_key' => 'key',
   'orderid' => 'ORDERID-0001'
   'shipper_name' => 'SENDER',
   'shipper_contact' => 'SENDER',
   'shipper_phone' => 'sender_phone',
   'shipper_addr' => 'sender address',
   'origin_code' => 'CITYCODE',
   'receiver_name' => 'RECEIVER',
   'receiver_phone' => 'receiver_phone',
   'receiver_addr' => 'receiver address',     
   'receiver_zip' => 'receiver zip',
   'destination_code' => 'DESTCODE',
   'receiver_area' => 'SDA001',
   'qty' => '1',
   'weight' => '1',
   'goodsdesc' => 'TESTING!!',
   'servicetype' => '1',
   'insurance' => '50000',
   'orderdate' => '2019–2-10 22:02:00',
   'item_name' => 'hat',
   'cod' => '200000',
   'sendstarttime' => '2017–2-10 08:00:00',
   'sendendtime' => '2017–2-10 22:00:00',
   'expresstype' => '1',
   'goodsvalue' => '1000',
);

$data_json = json_encode(array('detail'=>array($data)));
$data_request = array (
    'data_param'=>$data_json, 
    'data_sign'=> base64_encode(md5($data_json.$key))
);

I was send the data above using Postman but it returned "Parameters can not null". Then I try using curl but the response HTTP 500.

Can someone here tell me the best practice to get best response?

By the way this is sample response if the code above is success:

{
   "success":true,
   "desc":"request success",
   "detail":
   [
      {
         "orderid" : "ORDERID-0001",
         "status" : "Error",
         "reason" : "username or api key wrong!",
      }
      {
         "orderid" : "ORDERID-0002",
         "awb_no" : "AWBJNT-001"
         "status" : "Success",
      }
   ]
}
  • If that is the code you're using, one reason for the problems is that your $data array is missing comma after orderid causing syntax error. – yasoh Feb 12 '19 at 07:57
  • If https://stackoverflow.com/questions/11079135/how-to-post-json-data-with-php-curl solves your problem then this can be closed as a duplicate. – Nigel Ren Feb 12 '19 at 08:08
  • @yasoh ow okay it's my fault, after give a comma after orderid i got nothing, just white screen. – Zapra Gartiast Feb 12 '19 at 08:09
  • @NigelRen thanks for the link, but it's a bit confused, maybe I need more explanation... – Zapra Gartiast Feb 12 '19 at 08:15
  • _“Can someone here tell me the best practice to get best response?”_ - best practice would be to actually properly analyze and debug your problem, _instead of_ asking for “best practices” at such a point. (Following best practices is not wrong of course; but reading up on them would be your responsibility IMHO, outside of SO.) _“I was send the data above using Postman”_ - you mean you tried to send it? Or are you the receiver of such a request, and don’t know how to work with it? _Show us_ what you actually tried, instead of just telling us that you tried something. – 04FS Feb 12 '19 at 09:08
  • @04FS thanks for the suggestion. Problem solved now, I get right good answer below – Zapra Gartiast Feb 12 '19 at 13:38

1 Answers1

0

First set header as below

header('Content-Type: application/json');

Then generate response and echo it.

$response[] = array(
                        'status' => 'true',
                        'desc' => 'request success',
                        'detail'=> $data,
                    );

echo json_encode(array('response' => $response));

Note: I added basic params here, you can add other details as per your need.

Devsi Odedra
  • 5,244
  • 1
  • 23
  • 37