How to use this POST CURL into PHP - 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);

Related

How to do this curl request via php / laravel?

I have a curl request that was given to me as part of an API documentation, and can't for the life of me get it to work via php (in Laravel).
The documentation says to do this...
wget -O- \
--header 'x-user:x' \
--header 'x-password:y' \
'http://example.com'
What I'm trying is this...
$url = 'http://example.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_USERPWD, "test_user:test_password");
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
dd($info);
This is just echoing out Authentication Failed and then a bunch of info on my request, but not actual data.
Can anyone point out what I'm doing wrong?
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'x-user: something', 'x-password: something' ));

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.

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