Hi i need to send sms by using php. I am using curl method to post variables in url but i cant get sms. I can get result by using get method, is there any problem in my code?
$url = 'http://online.chennaisms.com/api/mt/SendSMS?';
$postData = array();
$postData['user'] = 'abc';
$postData['password'] = 'qwftgry ';
$postData['senderid'] ='reyty';
$postData['channel'] ='Trans';
$postData['DCS'] =0;
$postData['flashsms'] =0;
$postData['number'] = 91XXXXXXXXXX;
$postData['text'] ='hai ';
$postData['route'] =28;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$result = curl_exec($ch);
curl_close($ch);
Try to delete ? character at the end of url
I don't see any problem in your code, apart from the lack of any error-checking on the cURL transaction (by means of curl_error() etc), so you should probably look elsewhere - was an error message returned by the SMS provider, for example?
Related
Im a newbie im trying to get a script to trigger another script with Curl in PHP but it dosent seem to be sending the paramaters.
Is there a seperate function to append parameters?
<?php
$time = time();
$message = "hello world";
$urlmessage = urlencode( $message );
$ch = curl_init("http://mysite.php?message=$urlmessage&time=$time");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
?>
Could anyone point me in the right direction??
The accepted answer is good for POST, but what if OP wanted specifically to GET? Some REST APIs specify the http method and often it's no good POSTing when you should be GETting.
Here is a fragment of code that does GET with some params:
$endpoint = 'http://example.com/endpoint';
$params = array('foo' => 'bar');
$url = $endpoint . '?' . http_build_query($params);
curl_setopt($ch, CURLOPT_URL, $url);
This will cause your request to be made with GET to http://example.com/endpoint?foo=bar. This is the default http method, unless you set it to something else like POST with curl_setopt($ch, CURLOPT_POST, true) - so don't do that if you specifically need to GET.
If you need to use one of the other http methods (DELETE or PUT for example) then use curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method). This also works for GET and POST.
You need curl_setopt() along with the CURLOPT_POSTFIELDS param.
That'll POST the given params to the target page.
curl_setopt($ch, CURLOPT_POSTFIELDS, 'foo=1&bar=2&baz=3');
PS: also check http_build_query() which is handy when sending many variables.
you need set CURLOPT_POST as true and CURLOPT_POSTFIELDS => parameters
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
a suggestion,set 'CURLOPT_RETURNTRANSFER', as true to return the transfer as a string of the return value of curl_exec($ch) instead of outputting it out directly
Here is A Simple Solution for this.
$mobile_number = $_POST['mobile_number'];
$sessionid = $_POST['session_id'];
CURLOPT_URL => 'https://xxyz.jkl.com/v2.0/search?varible_that_you_want_to_pass='.$mobile_number.'&requestId=1616581154955&locale=en-US&sessionId='.$sessionid,
I'm sending API request using curl and php. The first one works but not the second one. Error shows that postfields data is empty, or the whole request is null. I'm confused.
this is my code
$request_id = "aaa7996d-8d5c-4116-b759-6afb1c84ff39";
$res_url = "http://opeapi.ws.pho.to/getresult";
$res_data = array('request_id'=>$request_id);
$res_ch = curl_init();
curl_setopt($res_ch, CURLOPT_URL, $res_url);
curl_setopt( $res_ch, CURLOPT_POST, true );
curl_setopt($res_ch, CURLOPT_POSTFIELDS,$res_data);
curl_setopt($res_ch, CURLOPT_RETURNTRANSFER, 1);
$results = curl_exec($res_ch);
curl_close($res_ch);
var_dump($results);
but the results returned:
SecurityError612Bad, invalid or empty REQUEST_ID parameter.
Here are a few things I tries:
$res_data = array('request_id'=>urlencode($request_id));
$res_data = "request_id=".$request_id;
$res_data1 = json_encode($res_data);
$res_data1 = http_build_query($res_data);
$res_url = "http://opeapi.ws.pho.to/getresult?request_id=aaa7996d-8d5c-4116-b759-6afb1c84ff39";
none of them really works.
but the curious thing is I have another request before this one. Also using Pho.to API. I processed a photo using their parameters and got the result using curl and php. That one works (that's why I get the request_id to obtain the processing result)
this is my former request. it works, why not the second one??
$data = '(some xml parameters)';
$sign_data = hash_hmac('SHA1', $data,'***');
$url = "http://opeapi.ws.pho.to/addtask";
$posting = array('app_id'=>'***','key'=>'***','sign_data'=>$sign_data,'data'=>$data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_POSTFIELDS,$posting);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
error message suggest there is no request_id...
or the whole request $curl is null...
Any help or suggestion will be deeply appreciated!!! Please~Thanks!!!
I figured it out!
It turns out I'm supposed to use a get request (in order to get my data), not a post.
So I directly write my url to...http://?request_id=, and deleted all the option lines about request (because the default process is get, not post), and it worked!
took me 7 hours to figure this out...TAT
I am using an API that returns JSON from a GET request
Eg.
https://api.domain.com/v1/Account/{auth_id}/Call/{call_uuid}
Returns
{
"call_duration": 4,
"total_amount": "0.00400"
}
How can I call this page from within a script and save call_duation and total_amount as separate variables?
Something like the following?:
$call_duration =
$_GET[https://api.domain.com/v1/Account/{auth_id}/Call/{call_uuid}, 'call_duration'];
$_GET[] contains the get parameters that are passed to your code - they don't generate a GET request.
You could use curl to make your request:
$ch = curl_init("https://api.domain.com/v1/Account/{auth_id}/Call/{call_uuid}");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$result = json_decode($output);
If PHP has allow_url_fopen enabled you can simply do
json_decode(file_get_contents('https://api.domain.com/v1/Account/{auth_id}/Call/{call_uuid}'))
Otherwise you'll have to resort to using something like Curl to get the request going. $_GET is a superglobal array which doesn't actually do anything. It only contains what the script was started with. It does not make any requests itself.
Use curl to get the JSON, then json_decode to decode it into PHP variables
$auth_id = 'your auth id here';
$call_uuid = 'your call_uuid here';
// initialise curl, set URL and options
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.domain.com/v1/Account/{$auth_id}/Call/{$call_uuid}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
// get the response and decode it
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
$call_duration = $response['call_duration'];
$total_amount = $response['total_amount'];
I have this issue please.
I use this script here to send an automatic SMS:
<?php
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_content = get_data('http://url.goes.here:8080/bulksms/bulksms?username=user&passwd=password&type=0&dlr=1&destination=phone&source=FLM&message=hello');
?>
The issue is, if i submit the URL directly i get something like:
1701|355662080090|a2406d38-1baf-42a2-a1a5-e5798859e400
And a sms arrives to my phone, but if i use the above methods i won't get the sms..
Anyone can suggest me why is so?
Thanks..
If you DO have allow_url_fopen activated in your php.ini, then you should look into changing the user agent for the request (use cURL).
Many SMS gateways use user agent filtering when processing their requests...
Correct me if I am wrong, but I suppose you use RouteSMS, right?
Im a newbie im trying to get a script to trigger another script with Curl in PHP but it dosent seem to be sending the paramaters.
Is there a seperate function to append parameters?
<?php
$time = time();
$message = "hello world";
$urlmessage = urlencode( $message );
$ch = curl_init("http://mysite.php?message=$urlmessage&time=$time");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
?>
Could anyone point me in the right direction??
The accepted answer is good for POST, but what if OP wanted specifically to GET? Some REST APIs specify the http method and often it's no good POSTing when you should be GETting.
Here is a fragment of code that does GET with some params:
$endpoint = 'http://example.com/endpoint';
$params = array('foo' => 'bar');
$url = $endpoint . '?' . http_build_query($params);
curl_setopt($ch, CURLOPT_URL, $url);
This will cause your request to be made with GET to http://example.com/endpoint?foo=bar. This is the default http method, unless you set it to something else like POST with curl_setopt($ch, CURLOPT_POST, true) - so don't do that if you specifically need to GET.
If you need to use one of the other http methods (DELETE or PUT for example) then use curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method). This also works for GET and POST.
You need curl_setopt() along with the CURLOPT_POSTFIELDS param.
That'll POST the given params to the target page.
curl_setopt($ch, CURLOPT_POSTFIELDS, 'foo=1&bar=2&baz=3');
PS: also check http_build_query() which is handy when sending many variables.
you need set CURLOPT_POST as true and CURLOPT_POSTFIELDS => parameters
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
a suggestion,set 'CURLOPT_RETURNTRANSFER', as true to return the transfer as a string of the return value of curl_exec($ch) instead of outputting it out directly
Here is A Simple Solution for this.
$mobile_number = $_POST['mobile_number'];
$sessionid = $_POST['session_id'];
CURLOPT_URL => 'https://xxyz.jkl.com/v2.0/search?varible_that_you_want_to_pass='.$mobile_number.'&requestId=1616581154955&locale=en-US&sessionId='.$sessionid,