I have this PHP code that makes a curl call:
$postData['api_user'] = 'username';
$postData['api_key'] = 'password!';
$ch = curl_init('https://api.example.com/send.json');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
I know that in order to make a curl call with json in bash I have to do this:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" http://hostname/resource
But I'm confused in how I can transfer all my php code to bash curl call, what I mean is for example what would be the equivalent of curl_setopt(), and to pass the credentials as an array like I did with http_build_query()
Use cURL to POST your data as JSON.
curl -i \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-X POST --data '{"api_user": "username", "api_key":"password!"}' \
--insecure \
https://api.example.com/send.json
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); equals -k, --insecure.
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData))
equals -X POST --data ...
CURLOPT_RETURNTRANSFER... hmm, i don't know ,)
-d is the curl flag to send POST data, but you'll have to format it as a JSON string:
curl -H "Content-Type: application/json" -d '{ "api_user": "username", "api_key": "password" }' https://api.example.com/send.json
And now with those extra options (look them up in the curl man page):
curl -X POST -k -H "Content-Type: application/json" -d '{ "api_user": "username", "api_key": "password" }' https://api.example.com/send.json
As a much friendlier alternative to curl I recommend checking out httpie. Here's what your call would look like with httpie:
http --verify=no https://api.example.com/send.json api_user=username api_key=password
Related
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.
This is a question and answer after much research using some info from other responses found on stackoverflow.
How do I convert command-line curl to php:
$ curl -X POST "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records" \
-H "X-Auth-Email: user#example.com" \
-H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
-H "Content-Type: application/json" \
--data '{"type":"A","name":"example.com","content":"127.0.0.1","ttl":120}'
The above curl is an example from cloudflare's api manual on how to add an A record. I needed to add numerous CNAMES or subdomains. Cloudflare does not explain how to modify the above code to create CNAMES. So I will get that out of the way first:
$ curl -X POST "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records" \
-H "X-Auth-Email: user#example.com" \
-H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
-H "Content-Type: application/json" \
--data '{"type":"CNAME","name":"subdomain.example.com","content":"example.com","ttl":120}'
First I changed "type" field to "CNAME". Next the "name" field is where you put the subdomain / CNAME or in my case the resulting variable from a foreach. Finally under the "content" field you put the domain your new CNAME points to.
What this looks like in php:
$ch = curl_init();
$headers = array(
'X-Auth-Email: user#example.com',
'X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41',
'Content-Type: application/json',
);
$data = array(
'type' => 'CNAME',
'name' => 'subdomain.example.com',
'content' => 'example.com',
'ttl' => '120',
);
$json = json_encode($data);
curl_setopt($ch, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_exec($ch);
curl_close($ch);
The other answers left out the need for "json_encode". Hope this helps.
To translate valid curl commands to valid PHP code I recommend this super useful online converter:
https://incarnate.github.io/curl-to-php/
By using it, your original command line:
curl -X POST "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records" \
-H "X-Auth-Email: user#example.com" \
-H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
-H "Content-Type: application/json" \
--data '{"type":"A","name":"example.com","content":"127.0.0.1","ttl":120}'
Would be automatically converted to:
curl_setopt($ch, CURLOPT_URL, 'https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"type\":\"A\",\"name\":\"example.com\",\"content\":\"127.0.0.1\",\"ttl\":120}");
$headers = array();
$headers[] = 'X-Auth-Email: user#example.com';
$headers[] = 'X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
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! ;-)
I have the following code :
curl -X PUT -u "<app key>:<secret key>" \
-H "Content-Type: application/json" \
--data '{"alias": "myalias"}' \
https://go.xxx.com/api/device_tokens/<token>/
I tried to convert it to php but it seems not working and i don't know what is the problem.
this is what i tried
<?
$token = $_POST["token"];
$al = $_POST["alias"];
exec('curl -X PUT -u "_rEUqXXXmSVEBXXuMfdtg:vpB2XXXXX_2HZ_XXXX7t-Q" \
-H "Content-Type: application/json" \
--data \'{"alias": "'.$al.'"}\' \
https://go.xxx.com/api/device_tokens/'.$token'/');
?>
$ch = curl_init();
// echo $url; die;
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("custom: header"));
$returned = curl_exec($ch);
curl_close ($ch);
There are many more options to do what you want in the PHP docs, don't they help.
I need to know how to convert the code below to .php code (which I'm not good with)
the code:
curl -X POST -u "<application key>:<master secret>" \
-H "Content-Type: application/json" \
--data '{"device_tokens": ["<token>"], "aps": {"alert": "Hello!"}}' \
https://go.xxx.com/api/push/
EDIT: I TRIED this:
<?
$msg = "hello from bob";
$token = "72F75474E3360C4C2F26C6AB16FC1E638FE55FCAC92EE4EB4C196123XXXXXXXX";
exec('curl -X POST -u "_rEUqtOtSmSVEBd8uMfdtg:vpB2wmR8Q_2HZ_Jmi37t-Q" \
-H "Content-Type: application/json" \
--data \'{"aps": {"alert": "'.$msg.'", "sound": "default"}, "device_tokens": ["'.$token.'"]}\' \
https://go.xxx.com/api/push/
');
?>
You can try using PHP's cURL functions to accomplish this. The following should achieve what you want (untested):
$handle = curl_init("https://go.xxx.com/api/push/");
curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($handle, CURLOPT_USERPWD, "<application key>:<master secret>");
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($handle, CURLOPT_POST, 1);
curl_setopt($handle, CURLOPT_POSTFIELDS, '{"device_tokens": ["<token>"], "aps": {"alert": "Hello!"}}')
$result = curl_exec($handle);
curl_close($handle);
Via the cURL library. You should be able to find sufficient information on this specific problem via the documentation on the different curl_* functions in the PHP manual.