Translate curl command to PHP - 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.

Related

How to use this POST CURL into PHP

EXCLAMATION_MARK='!'
curl -X POST https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages.json \
--data-urlencode "From=whatsapp:$$$$$" \
--data-urlencode "Body=Hello there$EXCLAMATION_MARK" \
--data-urlencode "To=whatsapp:+$$$$$" \
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN
Hey all,
how can I use this curl code in PHP code?
i have been tried a lot of options but non was succeed
you would do something like.
$TWILIO_ACCOUNT_SID="whatever";
$TWILIO_AUTH_TOKEN="FLEH";
$ch = curl_init("https://api.twilio.com/2010-04-01/Accounts/{$TWILIO_ACCOUNT_SID}/Messages.json");
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,array(
"From"=>"whatsapp:\$\$\$\$\$",
"Body"=>"Hello there!",
"To"=>"whatsapp:+\$\$\$\$\$"
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN");
$response = curl_exec($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! ;-)

curl to php code

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.

Categories