I am using the following code to send a CURL request to send a SMS. But the SMS is being sent twice.
$message = urlencode($message);
$smsurl = "http://$url/sendmessage.php?user=matkaon&password=$password&mobile=$mobile&message=$message&sender=$sender&type=3";
$ch = curl_init($smsurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$sentsms = curl_exec($ch);
curl_close($ch);
I tried commenting some of the lines which solved the issue but gives an output as below:
What is the proper method to send a CURL request only once?
Try this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $smsurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
Don't pass the URL as argument on the init function.
I don't know why the function is being called twice, but I never pass the URL as argument and always work great this way.
You'd usually use curl_init() with no parameters, then pass the URL into curl_exec.
Modified example 1 from curl_exec docs:
<?php
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $smsurl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>
Related
I've been reading this helpful post:
http://techslides.com/hacking-the-google-trends-api
It shows how you can use cURL in command line/terminal to request data from google trends, for example;
curl --data "ajax=1&cid=actors&geo=US&date=201310" http://www.google.com/trends/topcharts/trendingchart
Gives you a big block of what I think is JSON. Below is an example of what I am doing to use cURL within my PHP to get data like this- however I cannot find anything that would get the data from the above cURL command to work in PHP like below.
<?php
//initialize session
$url = "http://www.google.com/trends/hottrends/atom/feed?pn=p1";
$ch = curl_init();
//set options
curl_setopt($ch, CURLOPT_URL, $url);
//execute session
$data = curl_exec($ch);
echo $data;
//close session
curl_close($ch);
?>
How do I go about getting the data from above?
You can do the same with the PHP cURL extension. You just need to set the options through curl_setopt, so you would do something like this
$url = "http://www.google.com/trends/topcharts/trendingchart";
$fields = "ajax=1&cid=actors&geo=US&date=201310";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
Now you have the response of the website in $data and you can do whatever you want with it.
You can find the PHP cURL documentation on http://php.net/curl
Try this
// Complete url with paramters
$url = "http://www.google.com/trends/topcharts/trendingchart?ajax=1&cid=actors&geo=US&date=201310";
// Init session
$ch = curl_init();
// Set options
curl_setopt($ch, CURLOPT_URL, $url);
// Set option to return the result instead of echoing it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Execute session
$data = curl_exec($ch);
// Close session
curl_close($ch);
// Dump json decoded result
var_dump(json_decode($data, true));
I am trying to send SMS from my localhost with xamp installed.
Requested page is on https and an .aspx page.
I am getting error: "HTTP Error 400. The request is badly formed." or blank page only in some cases.
Detaisl is as follows :
$url = 'https://www.ismartsms.net/iBulkSMS/HttpWS/SMSDynamicAPI.aspx';
$postArgs = 'UserId='.$username.
'&Password='.$password.
'&MobileNo='.$destination.
'&Message='.$text.
'&PushDateTime='.$PushDateTime.
'&Lang='.$Lang;
function getSslPage($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$response = getSslPage($all);
echo "<pre>";
print_r($response); exit;
I tried every possible solution/combination found on internet but could not resolve that. The API developers do not have a example for php script.
I tried httpful php library and file_get_contents function but getting empty page. Also tried every combination with curl_setup.
I need to call this url without any post data and see the response from it.
Instead getting a blank page.
Please note that when I execute the url with all details in browser it works fine.
Can anybody help me in this regard.
Thank you,
Usman
First do urlencode over your data as follows:
$postArgs = 'UserId='. urlencode($username.
'&Password='.urlencode($password).
'&MobileNo='.urlencode($destination).
'&Message='.urlencode($text).
'&PushDateTime='.urlencode($PushDateTime).
'&Lang='.urlencode($Lang);
After that two possible solutions. One is using GET.
curl_setopt($ch, CURLOPT_URL, $url . "?" . $postArgs);
Second option is using POST method.
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postArgs);
I am trying to access my ec2's public hostname from inside the instance.
I would like to run this command
curl http:// 169 254.169.254/latest/meta-data/public-hostname
inside a php script and save the response to a variable. How can I do this?
You can do like this
<?php
//URL of targeted site
$url = "http://www.yahoo.com/";
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// grab URL and pass it to the browser
$output = curl_exec($ch);
//echo $output;
// close curl resource, and free up system resources
curl_close($ch);
?>
The $output variable contains the response.
Shankar Damodaran provided an example of how to retrieve the response from a curl request, but specifically it is the
CURLOPT_RETURNTRANSFER that does as it says and returns the result from curl_exec
I am trying to fire a HTTP GET request on a secured URL which asks for username and password. This is fine when I am using that from browser but I am not sure how to do that using PHP.
I have tried using the two methods:
1) Using Curl as suggested in here: Make a HTTPS request through PHP and get response
2) Using the file_get_contents as suggested in here: How to send a GET request from PHP?
But the first one didn't give me any response back. And the second one gave me the following error:
failed to open stream: HTTP request failed
And this is my code for the curl:
$url="https://xxxxx.com";
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
and for the file_get_contents:
$url="https://xxxx.com";
$response=file_get_contents($url);
echo $response;
The URL will return a XML response for a API I am testing. Can someone point me to the right direction?
Thanks!
If we focus on the requirement to send a username and password because I suspect that's your main problem, try this
$ch = curl_init();
$url="https://xxxxx.com";
// OR - check with your server's operator
$url="http://xxxxx.com";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// or maybe
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
// - see http://stackoverflow.com/questions/4753648/problems-with-username-or-pass-with-colon-when-setting-curlopt-userpwd
// check the cURL documentation
$output = curl_exec($ch);
$info = curl_getinfo($ch);
// don't forget to check the content of $info, even a print_r($info) is better
// than nothing during debug
curl_close($ch);
I can call a soap server using java like this
Call call = new Call();
URL url = new URL("http://soap-something.dash.com/servlet/rpcrouter");
call.setTargetObjectURI("urn:login-transport");
call.setMethodName("confirmPassword");
call.setParams(a vector);
resp = call.invoke(url, "");
But my question is how can I call this same function using curl and php, I have already tried this, but it may be some kind of funny code
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://soap-something.dash.com/servlet/rpcrouter?urn:login-transport");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, "confirmPassword");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("username"=>"pritom", "password"=>"pritom"));
$head = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo "<br/>HTTP CODE: " . $httpCode;
print_r($head);
But it echo http code 100 and I do not found any result from soap server. But my soap server is ok, tested by java.
Looks fine to me, except CURLOPT_HEADER needs to be either true or false (include HTTP header in output or not),
curl_setopt($ch, CURLOPT_HEADER, "confirmPassword");
should be
curl_setopt($ch, CURLOPT_HEADER, true);
I can only guess what confirmPassword is, if it's a callback, you should use CURLOPT_HEADERFUNCTION instead.
But as Ariel pointed out, you're not telling us what the problem is.
Try removing this line:
curl_setopt($ch, CURLOPT_NOBODY, false); // remove body
Then post what the output is.