get_file_contents中的多个标头不起作用

时间:2014-09-01 13:18:35

标签: php json api file-get-contents

我正在尝试使用file_get_contents将多个标头传递到URL(API服务器)。我在下面的代码回应了HTTP 500错误:

$userdetails = http_build_query(
array(
    'userdetails' => '

                        {

                "jobId": null, "collectionOnDelivery": false, "invoice": null,

                "collectionDate": "2014-08-27T09:11:00", "consolidate": false,

                "consignments": [{ "collectionDetails":{

                "contactDetails":{ "contactName":"Mr David Smith" "telephone":"0121 500 2500"

                },

                "address":{

                "organisation":"GeoPost UK Ltd", "property":"",

                "street":"Roebuck Lane", "locality":"Smethwick", "town":"Birmingham", "county":"West Midlands", "postcode":"B66 1BY", "countryCode":"GB",

                }

                },

                "deliveryDetails":{

                "contactDetails":{ "contactName":"Mr David Smith"

                "telephone":"0121 500 2500"

                },

                "notificationDetails":{ "mobile":"07921 123456" "email":"david.smith@acme.com"

                },

                "address":{ "organisation":"ACME Ltd",

                "property":"Miles Industrial Estate", "street":"42 Bridge Road", "locality":"",

                "town":"Birmingham", "county":"West Midlands", "postcode":"B1 1AA", "countryCode":"GB",

                }

                },

                "networkCode":"1^12",

                "numberOfParcels":1,

                "totalWeight":5, "shippingRef1":"Catalogue Batch 1", "shippingRef2":"Invoice 231", "shippingRef3":"", "customsValue":0,

                "deliveryInstructions":"Please deliver to industrial gate A", "parcelDescription":"",

                "liabilityValue":0,

                "liability":false,

                "parcel":[]

                }]

                }



    '

    )
 );




$content = array('http' =>
    array(
         'method'  => 'POST',
         'header'  => "GEOSESSION: ".$geosession,
          "Accept: application/json",
           "Content-Type: application/json",
            'content' => $userdetails

     )
  );

   $context2  = stream_context_create($content);

   $result_userdetails = file_get_contents('https://api.interlinkexpress.com/shipping/shipment', false, $context2);


 echo $result_userdetails;

问题是我已经尝试/测试了POSTMAN上的标题,但我得到了回复,但是当我通过上面的代码传递它时,我得到了错误。我不确定为什么这是,并且想知道我是否实际上传递了所有三个标题?有人可以澄清是否是这种情况或是否有错误?

更新:

我尝试将用户详细信息更改为以下内容,因为有人指出它已经是JSON格式,但API似乎并不喜欢“内容”。 => $ userdetails现在?

$userdetails = $userdetails = http_build_query(
array(
    'userdetails' => '

                   {
"job_id": null,
"collectionOnDelivery": false,
"invoice": null,
"collectionDate": "",
"consolidate": false,
"consignment": [{
    "consignmentNumber": null,
    "consignmentRef": null,
    "parcels": [],
    "collectionDetails": {
        "contactDetails": {
            "contactName": "My Contact",
            "telephone": "0121 500 2500"
        },
        "address": {
            "organisation": "GeoPostUK Ltd",
            "countryCode": "GB",
            "postcode": "B66 1BY",
            "street": "Roebuck Lane",
            "locality": "Smethwick",
            "town": "Birmingham",
            "county": "West Midlands"
        }
    },
    "deliveryDetails": {
        "contactDetails": {
            "contactName": "My Contact",
            "telephone": "0121 500 2500"
        },
        "address": {
            "organisation": "GeoPostUK Ltd",
            "countryCode": "GB",
            "postcode": "B66 1BY",
            "street": "Roebuck Lane",
            "locality": "Smethwick",
            "town": "Birmingham",
            "county": "West Midlands"
        },
        "notificationDetails": {
            "email": "my.email@geopostuk.com",
            "mobile": "07921000001"
        }
    },
    "networkCode": "2^12",
    "numberOfParcels": 1,
    "totalWeight": 5,
    "shippingRef1": "My Ref 1",
    "shippingRef2": "My Ref 2",
    "shippingRef3": "My Ref 3",
    "customsValue": null,
    "deliveryInstructions": "Please deliver with neighbour",
    "parcelDescription": "",
    "liabilityValue": null,
    "liability": false
}]

}

1 个答案:

答案 0 :(得分:1)

应使用CRLF连接多个标头

$content = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => "GEOSESSION: " . $geosession . "\r\n" .
                     "Accept: application/json\r\n" .
                     "Content-Type: application/json",
        'content' => $userdetails

    )
);

要正确编码数据,首先要创建一个包含数据的PHP,然后使用json_encode对其进行格式化:

$data = array("jobId" => null, "collectionOnDelivery" => false, "invoice" => null,

              "collectionDate" => "2014-08-27T09:11:00", "consolidate" => false,

              "consignments" => array(array( "collectionDetails" =>
                                             array("contactDetails" => array( "contactName" => "Mr David Smith" "telephone" => "0121 500 2500"),
                                                   "address" => array("organisation" => "GeoPost UK Ltd", "property" => "",
                                                                      "street" => "Roebuck Lane", "locality" => "Smethwick", "town" => "Birmingham",
                                                                      "county" => "West Midlands", "postcode" => "B66 1BY", "countryCode" => "GB",)
                                                   ),
                                             "deliveryDetails" => array("contactDetails" => array( "contactName" => "Mr David Smith"
                                                                                                   "telephone" => "0121 500 2500"
                                                                                                   ),
                                                                        "notificationDetails" => array( "mobile" => "07921 123456" "email" => "david.smith@acme.com"),
                                                                        "address" => array( "organisation" => "ACME Ltd",
                                                                                            "property" => "Miles Industrial Estate", "street" => "42 Bridge Road", "locality" => "",
                                                                                            "town" => "Birmingham", "county" => "West Midlands", "postcode" => "B1 1AA", "countryCode" => "GB",)
                                                                        ),
                                             "networkCode" => "1^12",
                                             "numberOfParcels" => 1,
                                             "totalWeight" => 5, "shippingRef1" => "Catalogue Batch 1", "shippingRef2" => "Invoice 231", "shippingRef3" => "", "customsValue" => 0,
                                             "deliveryInstructions" => "Please deliver to industrial gate A", "parcelDescription" => "",
                                             "liabilityValue" => 0,
                                             "liability" => false,
                                             "parcel" => array()
                                             ))
              );
$userdetails = json_encode($data);