从json_encode PHP POST请求获取JSON返回状态

时间:2019-02-12 07:50:07

标签: php json

我需要使用json_encode发送数据。我只是在下面得到参考:

$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))
);

我是使用邮递员发送上述数据的,但返回“参数不能为空”。然后,我尝试使用curl但响应为HTTP 500。

这里有人可以告诉我获得最佳响应的最佳做法吗?

如果上面的代码成功的话,这是示例响应:

{
   "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",
      }
   ]
}

1 个答案:

答案 0 :(得分:0)

首先设置标题如下

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

然后生成响应并回显。

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

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

注意:我在这里添加了基本参数,您可以根据需要添加其他详细信息。