0
votes

I want to retrieve XML data related to weather. For that i use this API for it.

https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text=%22Kandy%22)

I tried to di it with CURL in PHP

This is my code

$url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text=%22Kandy%22)";

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);    // get the url contents

$data = curl_exec($ch); // execute curl request
curl_close($ch);

$xml = simplexml_load_string($data);
print_r($xml);

But it retrieve blank output without any of text. Please help me to sortout this problem.

1
does $data empty ?Oleg Butuzov
It seems to work. Did you curl_init()?Syscall
If that is your code ... where do you curl_init()??brombeer
yes it also a mistake. i got an answer from george and it worked. im new to this section actually. thanks all...Buddhika Priyabhashana
yes it also a mistake. i got an answer from george and it worked. im new to this section actually. thanks all...Buddhika Priyabhashana

1 Answers

1
votes

You need to initialize the cURL request like this:

   $url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text=%22Kandy%22)";

    $ch=curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);    // get the url contents

    $data = curl_exec($ch); // execute curl request
    curl_close($ch);

    $xml = (array)simplexml_load_string($data);
    echo "<pre>";
    print_r($xml);
    echo "<pre>";