2014-09-01 29 views
0

我想通过使用file_get_contents将多个头传递给URL(API服务器)。该代码下面我有回声出一个HTTP 500错误:get_file_contents中的多个头不起作用

$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":"[email protected]" 

       }, 

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

的事情是我曾尝试/测试的邮差头和我得到一个响应返回但是当我通过它通过上面的代码,我得到的错误。我不确定这是为什么,并且想知道我是否真的把所有三个标头都通过了?有人可以澄清是否是这种情况或者是否有错误?

UPDATE:

我曾试图改变的UserDetails到下面有人指出,它已经以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": "[email protected]", 
      "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

多个报头应该使用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" => "[email protected]"), 
                     "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); 
+0

A right okay我有类似的,但我错过了\ r \ n部分。现在试试吧 – 2014-09-01 13:33:55

+0

我仍然收到同样的错误:警告:file_get_contents(https://api.interlinkexpress.com/shipping/shipment):未能打开流:HTTP请求失败! HTTP/1.1 500内部服务器错误 – 2014-09-01 13:39:30

+0

指定内容类型为JSON,但您使用的是“http_build_query”而不是“json_encode”。 'http_build_query'产生'application/x-www-form-urlencoded' – Barmar 2014-09-01 13:43:07