I'm trying to use QPX Express API for my website to search for flights.
https://developers.google.com/qpx-express/v1/requests#Examples
I have no idea how to run
curl -d #request.json --header "Content-Type: application/json" https://www.googleapis.com/qpxExpress/v1/trips/search?key=xxxxxxxxxxxxxx
from my php file. And how should I manage the json file. I quess I should make a php file and set the header type, am I correct?
I could not find anything from anywhere
You do not need to create and save an actual JSON file for each request. You can simply create a JSON string and send that as the POST payload. As far as executing the curl, you should see the native functions available in the PHP Manual. Specifically, curl_init(), curl_setopt(), and curl_exec(). Here's an example...
$url = "https://www.googleapis.com/qpxExpress/v1/trips/search?key=YOUR_API_KEY_HERE";
$postData = '{
"request": {
"passengers": {
"adultCount": 1
},
"slice": [
{
"origin": "BOS",
"destination": "LAX",
"date": "2016-05-10"
},
{
"origin": "LAX",
"destination": "BOS",
"date": "2016-05-15"
}
]
}
}';
$curlConnection = curl_init();
curl_setopt($curlConnection, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($curlConnection, CURLOPT_URL, $url);
curl_setopt($curlConnection, CURLOPT_POST, TRUE);
curl_setopt($curlConnection, CURLOPT_POSTFIELDS, $postData);
curl_setopt($curlConnection, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curlConnection, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlConnection, CURLOPT_SSL_VERIFYPEER, FALSE);
$results = curl_exec($curlConnection);
You could also use an array to create the payload, and then use json_encode() to convert it into a JSON string.
$postData = array(
"request" => array(
"passengers" => array(
"adultCount" => 1
),
"slice" => array(
array(
"origin" => "BOS",
"destination" => "LAX",
"date" => "2016-05-10"
),
array(
"origin" => "LAX",
"destination" => "BOS",
"date" => "2016-05-15"
)
)
)
);
And then use
curl_setopt($curlConnection, CURLOPT_POSTFIELDS, json_encode($postData));
Related
I'm trying to send a post request with this payload:
$request_content = [
"data" => [
[
"sku" => "0987",
"price" => $price,
"category" => "moveis",
"brand" => "bartira",
"zip_code" => "07400000",
"affiliate" => "google-shopping"
]
]
];
Since it's a post i set the CURLOPT_POST to true;
$encoded_request = json_encode($request_content);
$ch = curl_init("https://my-service/endpoint/");
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Token my-token"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded_request);
The $encoded_request content shown in print_r is:
{
"data": [
{
"sku": "0987",
"price": "5.99",
"category": "moveis",
"brand": "bartira",
"zip_code": "07400000",
"affiliate": "google-shopping"
}
]
}
If i use this content on the Postman i get the right response from the service that i'm requesting, but on my code i got the error;
{"data":["This field is required."]}
Which configuration i'm missing on curl_ to format the payload correctly?
You can try to set CURLOPT_HTTPHEADER and change your variable $request_content, something like this:
//set your data
$request_content = [
"data" => [
"sku" => "0987",
"price" => $price,
"category" => "moveis",
"brand" => "bartira",
"zip_code" => "07400000",
"affiliate" => "google-shopping"
]
];
$encoded_request = json_encode($request_content);
$ch = curl_init("https://my-service/endpoint/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded_request);
// Set HTTP Header for POST request
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Token my-token',
'Content-Type: application/json',
'Content-Length: ' . strlen($encoded_request)]
);
I am using Office365 To Sync Email in PHP. The Office 365 REST API supports batch requests. I have tried to use this to retrieve emails using the Office 365 REST API, but so far I receive a "BadRequest." reply. Any help would be appreciated. Here is the sample code
$url = 'https://graph.microsoft.com/v1.0/$batch';
$headers = array(
"Authorization: Bearer ".$accessToken,
"Content-Type: application/json"
);
$msgid1 = "AQMkADAwATNiZmYAZC1kMjFjLWUyMTUtMDACLTAPwAAAHzY91EAAAA=";
$msgid2 = "AQMkADAwATNiZmYAZC1kMjFjLWUyMTUtDACLTvpPwAAAHzY91AAAAA=";
$msgid3 = "AQMkADAwATNiZmYAZC1kMjFjLWUyMTRRRBGUtMDACLAAHzY908AAAA=";
$params = array(
'requests' => array(
array(
"id" => "1",
"method" => "GET",
"url" => "/me/messages/$msgid1/attachments"
),
array(
"id" => "2",
"method" => "GET",
"url" => "/me/messages/$msgid2/attachments"
),
array(
"id" => "3",
"method" => "GET",
"url" => "/me/messages/$msgid3/attachments"
)
)
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
In Response i receive Invalid batch payload format message.
Oh.. There is error in my code.
Do
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));
insted of
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
How would I write below command in curl php:
curl -XPOST https://apiv2.unificationengine.com/v2/message/send
–data
“{ \"message\”: { \“receivers\”: [{\“name\”: \“TO_NAME \”,
\“address\”: \“TO_EMAILADDRESS\” ,
\“Connector\”: \“UNIQUE_CONNECTION_IDENTIFIER\”,
\“type\”: \“to\”}],\“sender\”: {\“address\”: \“EMAIL_ADDRESS\”},
\“subject\”:\“Hello\”,\“parts\”: [{\“id\”: \“1\”,
\“contentType\”: \“text/plain\”,
\“data\”:\“Hi welcome to UE\” ,
\“size\”: 100,\“type\”: \“body\”,\“sort\”:0}]}}“
-u USER_ACCESSKEY:USER_ACCESSSECRET -k
if this is the right way to execute and write curl in php:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://abcproject.com/tester.phtml");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"postvar1=value1&postvar2=value2&postvar3=value3");
// in real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS,
// http_build_query(array('postvar1' => 'value1')));
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
// further processing ....
if ($server_output == "OK") { ... } else { ... }
Here is more sample code SO question.
But I am finding problem that where to mentions -u, --data options in php curl.
curl -u is equivalent to CURLOPT_USERPWD in php-curl.
You set it with curl_setopt, as the others.
--data is CURLOPT_POSTFIELDS, which you are already sending. But in your case you'd want to populate with the json you want to send.
If you are sending a JSON, you'd do well in setting the Content-type header (and content-length wouldn't be amiss either)
Be careful, in your example call there are some weird characters. But the JSON you posted is equivalent to:
$yourjson = <<<EOF
{
"message": {
"receivers": [
{
"name": "TO_NAME ",
"address": "TO_EMAILADDRESS",
"Connector": "UNIQUE_CONNECTION_IDENTIFIER",
"type": "to"
}
],
"sender": {
"address": "EMAIL_ADDRESS"
},
"subject": "Hello",
"parts": [
{
"id": "1",
"contentType": "text/plain",
"data": "Hi welcome to UE",
"size": 100,
"type": "body",
"sort": 0
}
]
}
}
EOF;
But usually you'd start with your data in array form and json_encode it.
So you'd start with something like:
$array = [
'message' =>
[
'receivers' =>
[
0 =>
[
'name' => 'TO_NAME ',
'address' => 'TO_EMAILADDRESS',
'Connector' => 'UNIQUE_CONNECTION_IDENTIFIER',
'type' => 'to',
],
],
'sender' =>
[
'address' => 'EMAIL_ADDRESS',
],
'subject' => 'Hello',
'parts' =>
[
0 =>
[
'id' => '1',
'contentType' => 'text/plain',
'data' => 'Hi welcome to UE',
'size' => 100,
'type' => 'body',
'sort' => 0,
],
],
],
];
... convert that using $yourjson = json_encode($array), and that's it.
E.g.:
// you already have your json inside of $yourjson,
// plus your username and password in their respective variables.
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_POSTFIELDS, $yourjson);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($yourjson)
]
);
to send data as JSON add header content-type and set as JSON and add the option CURLOPT_USERPWD, something like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://apiv2.unificationengine.com/v2/message/send");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS,
“{ \"message\”: { \“receivers\”: [{\“name\”: \“TO_NAME \”, \“address\”: \“TO_EMAILADDRESS\” , \“Connector\”: \“UNIQUE_CONNECTION_IDENTIFIER\”, \“type\”: \“to\”}],\“sender\”: {\“address\”: \“EMAIL_ADDRESS\”},\“subject\”:\“Hello\”,\“parts\”: [{\“id\”: \“1\”,\“contentType\”: \“text/plain\”, \“data\”:\“Hi welcome to UE\” ,\“size\”: 100,\“type\”: \“body\”,\“sort\”:0}]}}“);
// in real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS,
// http_build_query(array('postvar1' => 'value1')));
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$server_output = curl_exec ($ch);
curl_close ($ch);
I am trying to call Google Cloud Speech API by using PHP and got a problem.
$stturl = "https://speech.googleapis.com/v1beta1/speech:syncrecognize?key=xxxxxxxxxxxx";
$upload = file_get_contents("1.wav");
$upload = base64_encode($upload);
$data = array(
"config" => array(
"encoding" => "LINEAR16",
"sampleRate" => 16000,
"languageCode" => "en-US"
),
"audio" => array(
"Content" => $upload,
)
);
$jsonData = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $stturl);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
$result = curl_exec($ch);
The result says that it is INVALID JSON PAYLOAD.
{ "error": { "code": 400, "message": "Invalid JSON payload received.
Unknown name \"content\" at 'audio': Cannot find field.", "status":
"INVALID_ARGUMENT", "details": [ { "#type":
"type.googleapis.com/google.rpc.BadRequest", "fieldViolations": [ {
"field": "audio", "description": "Invalid JSON payload received.
Unknown name \"content\" at 'audio': Cannot find field." } ] } ] } } "
I think this is because $upload isn't configured correctly.
According Google Cloud Speech API, it should be "A base64-encoded string".
https://cloud.google.com/speech/reference/rest/v1beta1/RecognitionAudio
That's why I used base64_encode function, but it seems JSON doesn't process this value correctly.
Any thoughts?
You need to construct the properly formatted input as an array and then json encode it. For example, to send a file, base64encode it as "content" and submit to the API as shown:
$data = array(
"config" => array(
"encoding" => "LINEAR16",
"sample_rate" => $bitRate,
"language_code" => "en-IN"
),
"audio" => array(
"content" => base64_encode($filedata)
)
);
$data_string = json_encode($data);
$ch = curl_init($googlespeechURL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
$result_array = json_decode($result, true);
please make 'content' instead of 'Content'
small letter 'c'
its work for me.
The error:
{"jsonrpc":"2.0","id":0,"error":{"code":1,"message":"Parameters missing or not sent as an JSON array. "}}
My code:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Accept: text/plain, */*; q=0.01"));
curl_setopt($ch, CURLOPT_URL, 'http://example.com/rpc/ClientApi?_session=XX');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array('method' => 'AdsApi.giveAdLifeByCategory','params' => '["Ads4Life"]', 'id' => '0')));
curl_setopt($ch, CURLOPT_POST, 1);
$result = curl_exec($ch);
?>
I'm trying to emulate this request:
[{"method":"AdsApi.giveAdLifeByCategory","params":["Ads4Life"],"id":0}]
Correct Response:
[{
"jsonrpc": "2.0",
"id": 0,
"result": {
"status": true,
"timeStamp": 0
}
}]
What could be the problem?
Wrong bracketing:
... 'AdsApi.giveAdLifeByCategory','params' => '["Ads4Life"]', 'id' => '0')));
^--- ^--
your "emulated" sample has braockets around that, but you're embedding them in your array key definition, so when the JSON is produced, it'll actually be:
... "params": "[\"Ads4Life\"]" ...
with the [" and "] embedded within the string.
Probably should be just
... 'AdsApi.giveAdLifeByCategory','params' => array('Ads4Life'), 'id' => '0')));
^^^^^^^^^^