How do I use the following example curl to send an actual SMS with PHP?
// -- Sending Direct SMS --
curl -X "POST" "https://api.ringcaptcha.com/APP_KEY/sms" \
-d "api_key=API_KEY" \
-d "phone=TO_NUMBER" \
-d "message=Hi there! This is a test message from
RingCaptcha."
At last Worked :)
$data = array(
'api_key'=>'{api_key}',
'phone'=>'{number}'
);
$string = http_build_query($data);
$ch = curl_init("https://api.ringcaptcha.com/{key}/code/sms");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);
print_r($server_output);
Related
I'm trying to create the box app users using PHP. The curl for create user as follows, and it is working on terminal
curl https://api.box.com/2.0/users \
-H "Authorization: Bearer <Access token>" \
-d '{"name": "New User", "is_platform_access_only": true}' \
-X POST
Same thing I have tried with php But it is giving the following error
{"type":"error","status":400,"code":"invalid_request_parameters","help_url":"http:\/\/developers.box.com\/docs\/#errors","message":"Invalid input parameters in request","request_id":"6688622675982fb5339a37"}
The following one I have tried
$developer_token = "TOKEN" ;
$access_token_url = "https://api.box.com/2.0/users";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $access_token_url);
//Adding Parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'name'=>'NEW USER',
'is_platform_access_only'=>'true',
));
//Adding Header
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '.$developer_token
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response1 = curl_exec($ch);
If I remove the Post parameters, and run with only headers it is give the result of users. But with post it is throws error.
I have rise the same question in Perl tag with Perl code. There I got answer by user #melpomene.
We should encode the data as JSON. It is working,
Then the final code is
$data = array(name=>SOMENAME,is_platform_access_only=>true);
$data = json_encode($data);
$header = array("Authorization: Bearer <TOKEN>");
$ch = curl_init("https://api.box.com/2.0/users/");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response1 = curl_exec($ch);
curl_close($ch);
I'm attempting to POST a local file to an API, it works if I use command line curl but not when I try using php cURL. I set up some variables for the headers and body
$headers = array();
$headers[] = 'Authorization: Passcode '.$passcode;
$headers[] = 'FileType: STD';
$api_url = 'https://url.com';
$field_json = json_encode($field).';type=application/json'
$filepath = realpath('path/file.csv');
$file = new CURLFile($filepath,'text/csv');
curl based on the API's specifications works if I set it as a string and use exec
$cmd = "curl -X POST -H \"$headers[0]\" -H \"$headers[1]\" -F \"field=$field_json\" -F \"filename=#$filepath\" $api_url";
exec($cmd,$result); //This works successfully
but when I attempt to send the POST using the following code with PHP cURL, I get a vague error response 'Unexpected error'
$body = array(
'field' => $field_json,
'filename' => $file
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//curl_setopt($ch, CURLOPT_SAFE_UPLOAD, FALSE);
$result = curl_exec($ch);
curl_close($ch);
echo json_encode($result);
I'm using PHP 5.5.9 and have tried it using the old way with # and the filepath.
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;
I'm trying to use Neo4js Traverser via the HTTP API.
If I use it via curl on the command line it works fine, but when I try to use it via curl through PHP I get an error all the time.
This is curl command:
curl -H Accept:application/json -H Content-Type:application/json -X POST -d '{"order":"depth first"}' http://localhost:7474/db/data/node/5/traverse/node
And this is my PHP Code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost:7474/db/data/node/5/traverse/node");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept:application/json',
'Content-Type:application/json'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, '"{"order": "depth first"}"');
$output = curl_exec($ch);
echo '<pre>';
var_dump(curl_getinfo($ch));
var_dump($output);
curl_close($ch);
This is the error I get:
HTTP ERROR 500
Problem accessing
/db/data/node/5/traverse/node. Reason:
java.lang.String cannot be cast to java.util.Map
Any Ideas?
Looks like you have quotes before the JSON string:
curl_setopt($ch, CURLOPT_POSTFIELDS, '"{"order": "depth first"}"');
Might want to try this:
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"order": "depth first"}');
EDIT: Although better yet, I'd use json_encode with an associative array to ensure proper escaping if necessary:
$json_data = array("order" => "depth first");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($json_data));
I want to access an API provided by Lymbix sentiment via PHP. The cURL command given is
curl -H "AUTHENTICATION:MY_API_KEY" \
-H "ACCEPT:application/json" \
-H "VERSION:2.1" \
http://gyrus.lymbix.com/tonalize \
-d "article=This is a sample sentence, does it make you happy? \
&return_fields=[]"
How would I run the above in PHP?
Thank you.
I hate to feed the trolls, but i was bored. You really should do some legwork on these things first, and also accept (checkmark) answers when they are right, or got you really close.
<?php
$ch = curl_init();
$data = array('article' => 'This is a sample sentence, does it make you happy?', 'returnfields' => '[]');
$headers = array ('AUTHENTICATION'=>'MY_API_KEY','ACCEPT'=>'application/json','VERSION'=>'2.1');
curl_setopt($ch, CURLOPT_URL, "http://gyrus.lymbix.com/tonalize");
curl_setopt($ch, CURLOPT_HTTPHEADERS,$headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
curl_close($ch);
?>
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://gyrus.lymbix.com/tonalize" );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt($curl, CURLOPT_POST, true );
curl_setopt($curl, CURLOPT_POSTFIELDS, "article=This is a sample sentence, does it make you happy?&return_fields=[]");
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"ACCEPT:application/json\n",
"VERSION:2.1\n",
"AUTHENTICATION:MY_API_KEY",
));
$result = curl_exec($curl);
curl_close($curl);