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

问题描述 投票:-1回答:1

我需要使用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",
      }
   ]
}
php json
1个回答
0
投票

首先设置标题,如下

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

然后生成响应和回声它。

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

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

注:我用基本PARAMS在这里,你可以添加其他细节根据自己的需要。

© www.soinside.com 2019 - 2024. All rights reserved.