may I know is the correct way, to pass curl PHP
$data6 = array (
"CorrelationId" => 'CorrelationId',
"ConfirmationId" => 'ConfirmationId',
"Contact.Title" => 'Miss',
"Contact.FirstName" => 'FirstName',
"Contact.LastName" => 'LastName',
"Contact.MobilePhone" => '1234567',
"Contact.HomePhone" => '12356778',
"Contact.Email" => '[email protected]',
"Contact.Remark" => '',
"Guests[0]Index" => 1,
"Guests[0]Title" => 'Miss',
"Guests[0]FirstName" => 'FirstName',
"Guests[0]LastName" => 'LastName',
"Guests[0]MobilePhone" => '123456',
"Guests[0]HomePhone" => ' 123456677',
"Guests[0]Email" => '[email protected]',
"Guests[0]Type" => 1,
"Guests[0]Age" => 21,
"Beds" => '',);
Below is the original JSON passes data, the original JSON data is from the postman, I running postman is good to go. I think my mistake was from my code to pass parameter.
{
"CorrelationId": "CorrelationId",
"ConfirmationId": "ConfirmationId",
"Contact": {
"Title": "Miss",
"FirstName": "FirstName",
"LastName": "LastName",
"MobilePhone": "1234567",
"HomePhone": " 6287717564805",
"Email": "[email protected]",
"Remark": ""
},
"Guests": [
{
"Index": 1,
"Title": "Miss",
"FirstName": "FirstName",
"LastName": "LastName",
"MobilePhone": "1234567",
"HomePhone": " 6287717564805",
"Email": "[email protected]",
"Type": 1,
"Age": 21
}
],
"Beds": []
}
CodePudding user response:
Yes marliah, you can pass your array as follows. I suggest there's no need to add an extra brackets to guests. If you do, you have to use index 0 to access data. like Guests[0][Age]
<?php
$data = array(
"CorrelationId" => "CorrelationId",
"ConfirmationId" => "ConfirmationId",
"Contact" => [
"Title" => "Miss",
"FirstName" => "FirstName",
"LastName" => "LastName",
"MobilePhone" => "1234567",
"HomePhone" => " 6287717564805",
"Email" => "[email protected]",
"Remark" => ""
],
"Guests" => [[
"Index" => 1,
"Title" => "Miss",
"FirstName" => "FirstName",
"LastName" => "LastName",
"MobilePhone" => "1234567",
"HomePhone" => " 6287717564805",
"Email" => "[email protected]",
"Type" => 1,
"Age" => 21
]],
"Beds" => []
);
print_r(json_encode($data));
You can use the following function to pass your array to the backend.
Don't forget to use json_encode().
call('POST','API_URL',json_encode($data),'USERNAME','PASSWORD');
If you don't have any authentication setup at the backend, ignore the $username and the $password.
remove this line to ignore authentication credentials.
curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
.
public function call($method, $url, $data, $username, $password)
{
$curl = curl_init();
switch ($method) {
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// OPTIONS:
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Accept: application/json",
));
curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//Check for errors.
if (curl_errno($curl)) {
//If an error occured, throw an Exception.
throw new Exception(curl_error($curl));
}
// EXECUTE:
$result = curl_exec($curl);
if (!$result) {
die("Connection Failures");
}
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
echo 'HTTP status: ' . $httpcode;
return $result;
}
