Reputation: 97
I am using curl to get weather information from a specific site api.wunderground.com and the issue is its not working. I have tried using the file_get_contents function also, but its not working either. Here is my code for curl:
function get_web_page($url)
{
//echo "curl:url<pre>".$url."</pre><BR>";
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 15, // timeout on connect
CURLOPT_TIMEOUT => 15, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_PROXY => null,
);
$ch = curl_init($url);
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch,CURLINFO_EFFECTIVE_URL );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
//change errmsg here to errno
if ($errmsg)
{
echo "CURL:".$errmsg."<BR>";
}
return $content;
}
$url = "http://api.wunderground.com/api/67927f145c532a19/geolookup/conditions/q/uae/dubai.json";
get_web_page($url);
I have checked my server settings, curl is enabled and the server is using port 80. Can anybody help me with this I am out of ideas.
Upvotes: 0
Views: 12296
Reputation: 21
Simply add
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_PROXY => false,
it resolved the issue
Upvotes: 0
Reputation: 670
maybe you connect to internet by proxy.you can get it by setting a proxy in curl_setopt function.its function well for me.
Upvotes: 0
Reputation: 1219
you just need to echo the output.
echo get_web_page($url);
EDIT:
You may also use file_get_contents
<?php
$url="http://api.wunderground.com/api/67927f145c532a19/geolookup/conditions/q/uae/dubai.json";
echo file_get_contents($url);
?>
Code update:
<?php
function get_web_page($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$curl_result = curl_exec($ch);
curl_close ($ch);
return $curl_result;
}
$url="http://api.wunderground.com/api/67927f145c532a19/geolookup/conditions/q/uae/dubai.json";
echo get_web_page($url);
?>
Upvotes: 3
Reputation: 54212
The API seems to block suspicious User Agents. Try using a standard browser User Agent instead of spider
.
With normal ( a browser's user agent ) , the API returns normal response .
Upvotes: 0
Reputation: 90
i tried the same code and got response as
{ "response": { "version": "0.1" ,"termsofService": "http://www.wunderground.com/weather/api/d/terms.html" ,"features": { "geolookup": 1 , "conditions": 1 }
The above code is right. you simple forget to print the value returned by function
Upvotes: 0