can any one help me to convert following curl code to php curl script?
curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/xxx/SMS/Messages.xml' \
-d 'From=%2Byyyy' \
-d 'To=%2Bxxxx' \
-d 'Body=Dear%2C+Plz+wake+up%3B+it'\''s+time+4+SAHERI.%0D%0AYour+1+night+sacrifice+will+make+thousands+night+wonderful+by+the+grace+of+almighty+ALLAH.%0D%0A%0D%0A-biplob+(%2Bxxxxx)' \
-u xxx:yyy
best regards
MD Kamrul Hassan
The options you'd need set on your curl handle are as follows.
curl_setopt($ch, CURLOPT_URL, 'https://api.twilio.com/2010-04-01/Accounts/xxx/SMS/Messages.xml');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('From' => 'blah', 'To' => 'blih', 'Body' => 'Please wake up whatever'));
curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
Related
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);
I was trying to work on cloudflare api and I have found this in their documentation. Unfortunately I have no Idea how I can use this in fsockopen or through curl. Here is the example of POST Request. I just need to know how I can make a request with below data as POST using curl or fsockopen
curl https://www.cloudflare.com/api_json.html
-d 'a=cache_lvl' \
-d 'tkn=8afbe6dea02407989af4dd4c97bb6e25' \
-d 'email=sample#example.com' \
-d 'z=example.com' \
-d 'v=agg'
Here is the link to cloudflare
https://www.cloudflare.com/docs/client-api.html#s4.2
Try this one:
$ch = curl_init("https://www.cloudflare.com/api_json.html");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
// handling of -d curl parameter is here.
$param = array(
'a' => 'cache_lvl',
'tkn' => '8afbe6dea02407989af4dd4c97bb6e25',
'email' => 'sample#example.com',
'z' => 'ex'
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param));
$result = curl_exec($ch);
curl_close($ch);
print $result;
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
Teaching myself some Facebook feed posting via PHP and I'm trying to figure out how I would execute this curl HTTP POST via the the PHP curl command, any suggestions?
curl -X POST \
-F 'message=Post%20with%20app%20access%20token' \
-F 'access_token=YOUR_APP_ACCESS_TOKEN' \
https://graph.facebook.com/4804827/feed
It's found here
This is how to do it:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/4804827/feed");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'message' => 'Post with app access token',
'access_token' => 'YOUR_APP_ACCESS_TOKEN'
));
echo curl_exec($ch);
curl_close($ch);
I recommend looking at the documentation:
http://www.php.net/manual/en/function.curl-init.php
http://www.php.net/manual/en/function.curl-setopt.php
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! ;-)