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
}]
}