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.
Related
I'm trying to create a ticket in Freshdesk with a screenshot as an attachment. Screenshot is captured in a canvas and converted to dataurl. And I'm using the following code to pass it to the Freshdesk API (V2)
$data = [
"description" => $messageBody,
"subject" => 'Bug Report from Client',
"email" => $replyTo,
"priority" => 1,
"status" => 2,
"attachments" => [
[
"type" => "file",
"name" => "Screenshot",
"content-type" => "image/png",
"resource" => $dataUrl,
]
]
];
$url = "https://domain.freshdesk.com/api/v2/tickets";
$apiKey = "key";
$headers = array(
'Content-Type:application/json',
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $apiKey);
$resultStr = curl_exec($ch);
It results in the following error.
{
field: "attachments",
message: "It should contain elements of type valid file format only",
code: "datatype_mismatch"
}
Without the attachments field, the above code works fine. How do I fix this?
I'm working with OpenSRS EMAIL API. I'm trying to create a new user email for a domain. In their first example, they are using JSON format :
{
"credentials": {
"user": "domain_admin#example.com",
"password": "sw0rdf1sh"
},
"user": "bhayden#example.com",
"attributes": {
"name": "Bob Hayden",
"password": "changeit",
"delivery_forward": true,
"forward_recipients": [
"bob.hayden#example.com
}
}
I'm sending this with cURL
$json = array(
"user" => $emailName,
"attributes" => array(
"name" => "Janet User",
"password" => $email_password,
"delivery_forward" => false
)
);
//
//
$payload = json_encode( $json );
//
$data = [
'Content-Type:application/json',
'X-Username:' . $connection_details['reseller_username'],
'X-Signature:' . md5(md5($payload . $connection_details['api_key']) . $connection_details['api_key']),
];
//
$ch = curl_init($connection_details['api_host_port']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $data);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
//
$response = curl_exec($ch);
//
//header('Content-type: application/json');
//
echo '<pre>';
echo $response;
echo '</pre>';
This print following error
0.9 400 0 Invalid Content-Type XCP
What am I doing wrong?
I fixed this issue by reformarting the entire request. This worked for me
$data = array(
"user" => $emailName,
"attributes" => array(
"name" => "Janet User",
"password" => $email_password,
"delivery_forward" => false
)
);
$postdata = json_encode($data);
//
$ch = curl_init($connection_details_email['api_host_port']);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
print_r ($result);
I am creating a new PHP application in which I want to interact with Keybase.
First option is to use the API, but unfortunately I am running into some issues.
First step is to call the signup call.
I managed to create a curl request, but I am not sure what the pwh should look like.
I tried:
$fp = #fopen('/dev/urandom','rb');
if ($fp !== FALSE) {
$prBits .= #fread($fp,16);
#fclose($fp);
}
$salt = bin2hex($prBits);
$pwh = implode(unpack("H*",password_hash('MyP#ssword'.$prBits, PASSWORD_BCRYPT)));
But making the call results in:
{
"status": {
"code": 100,
"desc": "need a PDPKA5 key",
"name": "INPUT_ERROR"
},
"csrf_token": "lgHZIDA4ZjYyNzgy.....86aMrL09"
}
The complete call:
$data = [
"name" => "My Name",
"email" => "mymail#example.com",
"username" => "SomeUnusedUsername1234",
"pwh" => $pwh,
"pwh_version" => 3,
"salt" => $salt,
"invitation_id" => "000000001"
);
$dataString = json_encode($data);
$ch = curl_init('https://keybase.io/_/api/1.0/signup.json');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
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);
Anyone got a working example or solution?
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'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));