I have an sms android app that works remotely using a http server, It need to get a formed url request like this :
http://server.com:9090/sendsms?phone=123456789&text=foobar&pass=123456
When i type that url in the browser address bar and hit enter, the app sends the sms.
I'm new to curl, and I dont know how to test it, here is my code so far:
$phonenumber= '12321321321'
$msgtext = 'lorem ipsum'
$pass = '1234'
$url = 'http://server.com:9090/sendsms?phone=' . urlencode($phonenumber) . '&text=' . urlencode($msgtext) . '&password=' . urlencode($pass);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url
));
So my questions are, is the code correct? and how to test it?
Altough this is a simple GET, I cannot fully agree with hek2mgl. There are many situations, when you have to take care of timeouts, http response codes, etc. and this is what cURL is for.
This is a basic setup:
$handler = curl_init();
curl_setopt($handler, CURLOPT_URL, $url);
curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handler, CURLOPT_FOLLOWLOCATION, true);
// curl_setopt($handler, CURLOPT_MAXREDIRS, 10); // optional
// curl_setopt($handler, CURLOPT_TIMEOUT, 10); // optional
$response = curl_exec($handler);
curl_close($handler);
If you can access the url using the address bar in browser, then it is a HTTP GET request. The simplest thing to do that in PHP would be using file_get_contents() since it can operate on urls as well:
$url = 'http://server.com:9090/sendsms?phone=123456789&text=foobar&pass=123456';
$response = file_get_contents($url);
if($response === FALSE) {
die('error sending sms');
}
// ... check the response message or whatever
...
Of course you can use the curl extension, but for a simple GET request, file_get_contents() will be the simplest and most portable solution.
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 trying to send some parameters for an autentication on a web page this is the code
$url = "http://www.webpage.com/account/submit";
$handler = curl_init();
curl_setopt($handler, CURLOPT_URL, $url);
curl_setopt($handler, CURLOPT_POST,true);
curl_setopt($handler, CURLOPT_POSTFIELDS, "username=user#webmail.com&remember=true&password=123456");
$response = curl_exec ($handler);
curl_close($handler);
When I run this script shows nothing, Im trying to autenticate me succesfully into a website without the HTML form . . . there is another way to do this ???
Ofcourse it shows nothing. You would at least have to echo something (and likely do something with $response to actually check the result of the login). Also, make sure error reporting is enabled.
I'm probably not supposed to use file_get_contents() What should I use? I'd like to keep it simple.
Warning: file_get_contents(http://en.wikipedia.org/w/api.php?action=query&titles=Your_Highness&prop=revisions&rvprop=content&rvsection=0): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden
The problem you are running into here is related to the MW API's User-Agent policy - you must supply a User-Agent header, and that header must supply some means of contacting you.
You can do this with file_get_contents() with a stream context:
$opts = array('http' =>
array(
'user_agent' => 'MyBot/1.0 (http://www.mysite.com/)'
)
);
$context = stream_context_create($opts);
$url = 'http://en.wikipedia.org/w/api.php?action=query&titles=Your_Highness&prop=revisions&rvprop=content&rvsection=0';
var_dump(file_get_contents($url, FALSE, $context));
Having said that, it might be considered more "standard" to use cURL, and this will certainly give you more control:
$url = 'http://en.wikipedia.org/w/api.php?action=query&titles=Your_Highness&prop=revisions&rvprop=content&rvsection=0';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'MyBot/1.0 (http://www.mysite.com/)');
$result = curl_exec($ch);
if (!$result) {
exit('cURL Error: '.curl_error($ch));
}
var_dump($result);
The error message you are really receiving is
Scripts should use an informative User-Agent string with contact information, or they may be IP-blocked without notice.
This means that you should provide additional details about yourself when using the API. Your usage of file_get_contents does send the required User-Agent.
Here is a working example in curl that identifies itself as a Test for this question:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://en.wikipedia.org/w/api.php?action=query&titles=Your_Highness&prop=revisions&rvprop=content&rvsection=0&format=xml");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, "Testing for http://stackoverflow.com/questions/8956331/how-to-get-results-from-the-wikipedia-api-with-php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
file_get_contents Should work.
file_get_contents('http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=New_York_Yankees&rvprop=timestamp|user|comment|content')
This was previously discussed on stackoverflow here
Also, some nice looking code samples here
They themselves say in their API documentation:
Use any programming language to make an HTTP GET request for that URL
You need to get the URL right, thefollowing worksfor me :
http://en.wikipedia.org/w/api.php?format=json&action=query&titles=Main%20Page&prop=revisions&rvprop=content
you are not specifying the output format as far as I can notice right now!
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,
always: https://www.google.com/accounts/o8/ud
i got wordpress openid ok. so i think is is just discovery phase got some probelms..
<?php $ch = curl_init();
$url = 'https://www.google.com/accounts/o8/id';
$url = $url.'?';
$url = $url.'openid.mode=checkid_setup';
$url = $url.'&openid.ns=http://specs.openid.net/auth/2.0';
$url = $url.'&openid.claimed_id=http://specs.openid.net/auth/2.0/identifier_select';
$url = $url.'&openid.identity=http://specs.openid.net/auth/2.0/identifier_select';
$url = $url.'&openid.return_to='.site_url().'/user/openid/login_callback';
$url = $url.'&openid.realm=http://www.example.com/';
// set url
curl_setopt($ch, CURLOPT_URL, $url);
//return the transfer as a string
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,array("Accept: */*"));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
// $output contains the output string
$xdr = curl_exec($ch);
if (!$xdr) {
die(curl_error($ch));
}
// close curl resource to free up system resources
curl_close($ch);
$xml = new SimpleXMLElement($xdr);
$url = $xml->XRD->Service->URI;
$request = $connection->begin($url);
$request always null...
Take a look at https://blog.stackoverflow.com/2009/11/google-offers-named-openids/ where Jeff explains this behavior and what the user can do about it:
Well, the good news is, now you can! Google just gave us a fantastic Thanksgiving Day present in the form of Google Profiles supporting OpenID. And with a Google Profile, you get to pick a named URL of your choice!
Your question has the right endpoint URL (the one ending in /ud), but your example code is sending the request to the identifier URL (/id), not the endpoint URL.
My above code do return https://www.google.com/accounts/o8/ud in $url, which is correct actually
the problem is, you do not need to use openid php lib, just redirect the user to https://www.google.com/accounts/o8/ud with query string like:
https://www.google.com/accounts/o8/ud?openid.mode=checkid_setup&......