curl to php code - php

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.

Related

cloudflare api translate curl to php curl and send CNAME update

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);

Translate a PHP cURL to a bash cURL call

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

using curl -G flag in php

Here is my curl command:
curl -X GET \
-H "X-Parse-Application-Id: ..." \
-H "X-Parse-REST-API-Key: ..." \
-G \
--data-urlencode 'where={"playerName":"Jonathan Walsh"}' \
--data-urlencode 'count=1' \
--data-urlencode 'limit=0' \
https://api.parse.com/1/classes/GameScore
I am using php.
For the -X, -H I know the equivalent:
curl_setopt($ch, CURLOPT_URL, "https://api.parse.com/1/classes/GameScore");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-Parse-Application-Id: ...',
'X-Parse-REST-API-Key: ...',
));
What about the -G, --data flags?
What's the equivalent code?
For posting the data(you can use array though):
$request = 'where={"playerName":"Jonathan Walsh"}&count=1&limit=0';
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$request);
For https:// in your url
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
To return the date from curl execution
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
Finally:
$result = curl_exec($ch);
UPDATE:
Didn't check that you are doing GET operation. If you do GET, then it will be simply
$request = 'where={"playerName":"Jonathan Walsh"}&count=1&limit=0';
$url = "http://example.com/" . "?" . $request;
curl_setopt($ch, CURLOPT_URL,$url);
Accordingly to How to switch from POST to GET in PHP CURL, you should use :
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'GET');
Recommended read : http://php.net/manual/en/function.curl-setopt.php

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! ;-)

Translate curl command to PHP

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.

Categories