php google weather api query

2.8k Views Asked by At

I'm having trouble retrieving data from the google api. When I run the code, it only returns a blank page and not a printout of the xml array. Here is the code:

$url="http://www.google.com/ig/api";

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "?weather=london,england");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

$data = curl_exec($ch); 

curl_close($ch); 


echo "<pre>";
print_r($data); 
1

There are 1 best solutions below

0
On BEST ANSWER

I think that the problem is that you use POST method, and not the GET Try like this

$url="http://www.google.com/ig/api?weather=london,england";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

$data = curl_exec($ch); 

curl_close($ch); 

Hope it helps :)

EDIT: And yes, you have to do some extra parsing to get the data from the XML string