Reputation: 427
Hi I am trying to make JSON Request to a API but I am not sure how to as I have never worked on something similar ever before. I would really appreciate if someone can help me please.
Below is the request that I need to make:
request payload:
{
"sessionId": "1234567890",
"availabilityRequest": {
"checkInDate": "29042014",
"checkOutDate": "",
"noRooms": 1,
"noNights": 1,
"userType": "leisure",
"rateType": "standard",
"roomPreference": [
{
"noAdult": 1,
"noChild": 0
}
],
"siteCode": [
"GB0758",
"GB0746",
"GB0738",
"GB0755",
"GB0742"
],
"includeDisabled": "F"
}
}
This is what I have done but I am getting error Array ( [error] => Array ( [code] => 4007 [message] => Invalid JSON POST data (unable to decode): ) )
$postData = '{
"sessionId":"1234567890",
"availabilityRequest":
{
"checkInDate": "29042014",
"checkOutDate": "",
"noRooms": 1,
"noNights": 1,
"userType": "leisure",
"rateType": "standard",
"roomPreference":
[
{ "noAdult":1, "noChild":0 }
],
"siteCode":
[
"GB0758","GB0746","GB0738","GB0755","GB0742"
],
"includeDisabled":"F"
}
}';
$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POSTFIELDS => $postData));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Send the request
$response = curl_exec($ch);
// Check for errors
if($response === FALSE){
die(curl_error($ch));
}
// Decode the response
$responseData = json_decode($response, TRUE);
// Print the date from the response
print_r($responseData);
I would be really grateful is someone can help me please. Thank you
Upvotes: 0
Views: 90
Reputation: 1540
I can't really test this because I have no URL to test it with, but you can try to set a header.
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($postData)));
Upvotes: 0
Reputation: 2463
Your JSON is invalid. Validate it using a tool such as http://jsonlint.com/
You should add an other } to the end to close it. Then you will have a valid JSON.
Upvotes: 1