How to pass this cURL in PHP? - php

I got this from the IBM Watson speech to text documents, to send an audio file for processing:
curl -X POST -u <username>:<password>
--header "Content-Type: audio/flac"
--header "Transfer-Encoding: chunked"
--data-binary #<path>0001.flac
"https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?continuous=true"
But how should I build that into PHP cURL? Something like this?
$headr = array();
$headr[] = 'Content-Type: audio/flac';
$headr[] = 'Transfer-Encoding: chunked';
...
$crl = curl_init('https://stream.watsonplatform.net/speech-to-text/api');
curl_setopt($crl, CURLOPT_POST, 1);
curl_setopt($crl, CURLOPT_POSTFIELDS, array("username:password"=>"myuser:mypassword","data-binary"=>"#<path>0001.flac"));
curl_setopt($crl, CURLOPT_HTTPHEADER, $headr);
What about the -x and -u?

-X POST is already set when you did this
curl_setopt($crl, CURLOPT_POST, 1);
as for -u, you can use this, don't include the username:password in your post field array.
curl_setopt($crl, CURLOPT_USERPWD, "yourusername:yourpassword");
for using --data-binary, there is already an existing stackoverflow post on how to accomplish it using php curl: data-binary parameter in cURL

Related

Example with parameter curl -x php

I have to make a curl request in php with these parameters, can you help me with an example, since I can't find anything similar
the link curl
curl -X GET "https://example.com/nms/api/v2.1/devices?withInterfaces=true&authorized=false&type=uisps&role=switch" -H "accept: application/json" -H "x-auth-token: wsedr4455-3345-es45-2345-4edeesssd"
You should transform it into PHP curl syntax. It should work like this:
$ch = curl_init('https://example.com/nms/api/v2.1/devices?withInterfaces=true&authorized=false&type=uisps&role=switch');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'accept: application/json',
'x-auth-token: wsedr4455-3345-es45-2345-4edeesssd'
));
$result = curl_exec($ch);
curl_close($ch);
The JSON incoming is in $result.

Post the data with header in curl not working in php?

I'm trying to create the box app users using PHP. The curl for create user as follows, and it is working on terminal
curl https://api.box.com/2.0/users \
-H "Authorization: Bearer <Access token>" \
-d '{"name": "New User", "is_platform_access_only": true}' \
-X POST
Same thing I have tried with php But it is giving the following error
{"type":"error","status":400,"code":"invalid_request_parameters","help_url":"http:\/\/developers.box.com\/docs\/#errors","message":"Invalid input parameters in request","request_id":"6688622675982fb5339a37"}
The following one I have tried
$developer_token = "TOKEN" ;
$access_token_url = "https://api.box.com/2.0/users";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $access_token_url);
//Adding Parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'name'=>'NEW USER',
'is_platform_access_only'=>'true',
));
//Adding Header
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '.$developer_token
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response1 = curl_exec($ch);
If I remove the Post parameters, and run with only headers it is give the result of users. But with post it is throws error.
I have rise the same question in Perl tag with Perl code. There I got answer by user #melpomene.
We should encode the data as JSON. It is working,
Then the final code is
$data = array(name=>SOMENAME,is_platform_access_only=>true);
$data = json_encode($data);
$header = array("Authorization: Bearer <TOKEN>");
$ch = curl_init("https://api.box.com/2.0/users/");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response1 = curl_exec($ch);
curl_close($ch);

how Can i post data to an url using curl and json?

i want to use api of a website for send notification to my app
and the website help is not good :/
website said you should write in header of request tokan code and some setting
how can i post json data to this website with php ?
with these header
"Authorization: Token 7fb1………………………………29b464c "
"Content-Type: application/json"
"Accept: application/json"
and the url is from help text of website
curl -X POST "https://panel.pushe.co/api/v1/notifications/"
please help me#_#
Use something like this:
$ curl -X POST -i -H "Content-Type: application/json" -H "Authorization: TOKEN <YOUR_TOKEN>" https://panel.pushe.co/api/v1/notifications/
php example from here
$data = array("name" => "Hagrid", "age" => "36");
$data_string = json_encode($data);
$ch = curl_init('http://api.local/rest/users');
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);

How to use PHP CURL function to do the same request?

Here is my curl commaad line .
curl -u 'username:password' -X GET -H 'Accept: application/json' -H 'Content-Type: application/xml' -d '<request><layout>1</layout><filtermode>category</filtermode><filtervalue>115399046</filtervalue><limit>1</limit><start></start><sortfield></sortfield><sortdir></sortdir></request>' https://example.com/contacts
I use curl command line. It works for me. Now, I need do it in PHP.
The hard part is: The server accept GET request, but need a xml string in request body. I have tried to change GET to POST, the server did NOT return the correct message.
<?php
$ch = curl_init();
$credentials = "user:pass";
$data = 'xml string';
$page = "/contacts";
$headers = array( "GET ".$page." HTTP/1.0", "Content-type: application/xml", "Accept: application/json", "Authorization: Basic " . base64_encode($credentials) );
curl_setopt($ch, CURLOPT_URL, 'example.com/contacts');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$ret = curl_exec($ch);
?>
You can't put the GET line in $header, that causes an invalid header to be sent. The PHP equivalent of -X GET is:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");

PHP Curl with --data flag?

Can someone write a PHP script that reproduces the functionality of this linux shell command?
curl -X POST -u "USERNAME:PASS" \
-H "Content-Type: application/json" \
--data '{"aps": {"alert": "this is a message"}}' \
https://mywebsite.com/push/service/
I think I almost got it in my code, but I'm not sure how to handle the --data attribute.
Here's what my code looks like so far:
$headers = array();
$headers[] = "Content-Type: application/json";
$body = '{"aps":{"alert":"this is a message"}}';
$ch = curl_init();
// Set the cURL options
curl_setopt($ch, CURLOPT_URL, "https://mywebsite.com/push/service/");
curl_setopt($ch, CURLOPT_USERPWD, "USERNAME:PASSWORD");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
print_r($result);
example in:
http://code.google.com/apis/gdata/articles/using_cURL.html
curl https://www.google.com/accounts/ClientLogin \
--data-urlencode Email=brad.gushue#example.com --data-urlencode Passwd=new+foundland \
-d accountType=GOOGLE \
-d source=Google-cURL-Example \
-d service=lh2
A general rule: use the "--libcurl example.c" option to get curl to generate source code for a C program that would use libcurl. The API is very similar to the PHP/CURL one as you will see and you should then quickly realize that --data translates to CURLOPT_POSTFIELDS.
Oh, and you'll note that the -X usage is completely superfluous! ;-)

Categories