2014-09-01 32 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