dongyou2635 2014-09-01 13:18
浏览 24
已采纳

get_file_contents中的多个标头不起作用

I am trying to pass multiple headers through to a URL (API server) using file_get_contents. The code I have below echos out a HTTP 500 error:

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

The thing is I have tried/tested the headers on POSTMAN and I am getting a response back however when I pass it through the code above I get the error. I am unsure why this is and was wondering if I am actually passing all three headers across or not? Could someone please clarify whether this is the case or if there is an error?

UPDATE:

I have tried changing the userdetails to the below as someone pointed out it was already in JSON format however the API does not seem to be liking the 'content' => $userdetails now?

$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条回答 默认 最新

  • doushaiyu5065 2014-09-01 13:30
    关注

    Multiple headers should be connected using CRLF

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

    To encode the data properly, first create a PHP containing the data, and then use json_encode to format it:

    $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!