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.
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>";
Related
I am trying to get the title of this broadcast from twitch API but I am not sure very much about code.. I have the following code here.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/kraken/channels/test_user1");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
print $result;
and here's what it outputs.
{"mature":null,"status":"Broadcasting LIVE on Justin.tv","broadcaster_language":null,"display_name":"Test_user1","game":null,"language":"en","_id":22747064,"name":"test_user1","created_at":"2011-06-02T20:04:03Z","updated_at":"2016-04-19T21:00:05Z","delay":null,"logo":"https://static-cdn.jtvnw.net/jtv_user_pictures/test_user1-profile_image-ac0a2f0d39dda770-300x300.jpeg","banner":null,"video_banner":null,"background":null,"profile_banner":null,"profile_banner_background_color":null,"partner":false,"url":"https://www.twitch.tv/test_user1","views":99,"followers":1,"_links":{"self":"https://api.twitch.tv/kraken/channels/test_user1","follows":"https://api.twitch.tv/kraken/channels/test_user1/follows","commercial":"https://api.twitch.tv/kraken/channels/test_user1/commercial","stream_key":"https://api.twitch.tv/kraken/channels/test_user1/stream_key","chat":"https://api.twitch.tv/kraken/chat/test_user1","features":"https://api.twitch.tv/kraken/channels/test_user1/features","subscriptions":"https://api.twitch.tv/kraken/channels/test_user1/subscriptions","editors":"https://api.twitch.tv/kraken/channels/test_user1/editors","teams":"https://api.twitch.tv/kraken/channels/test_user1/teams","videos":"https://api.twitch.tv/kraken/channels/test_user1/videos"}}
I am trying to get the portion that says "status":"Broadcasting LIVE on Justin.tv" how can I go about doing so?
$result is encoded in json, you can decode it using json_decode, i.e.:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/kraken/channels/test_user1");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$decodedJson = json_decode($result, true);
echo $decodedJson['status'];
//Broadcasting LIVE on Justin.tv
working code
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://###.#####.###/####/####/T0103/templateCustomWebPage.do?webId=1209221452326&editCurrentLanguage=1209221452328&customWebPageId=1292822288140001019");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$data = curl_exec($ch);
curl_close($ch);
echo $data;[/code]
but if i use url from database than its not working.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $r->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
following error will be display...
parameter is wrong ,please check your input url.
Your input URL:
http://###.#####.###/####/####/T0103/templateCustomWebPage.do?webId=1209221452326&editCurrentLanguage=1209221452328&customWebPageId=1292822288140001019
Thank you in advance!
Your url is stored with html entities in the database. The CURL call doesn't accept those.
Try this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, html_entity_decode($r->url));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
I've added html_entity_decode
http://php.net/manual/en/function.html-entity-decode.php
ok so i am using the following code to set and retrieve the curl url
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$xml_response = curl_exec($ch);
curl_close($ch);
echo $xml_response;
my problem is that the api that i am calling has the data in xml format so curl is returning the data in one big string like this
509f0e1f-b8d4-4cb8-b4f0-109751dca4eb0.0114370000000000TrueASIN0273702440SmallAll0273702440http://www.amazon.com/Accounting-Finance-Non-Specialists-Peter-....(ect)
how to have curl display the data in xml format?
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);
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
Why am I not getting the HTML code when I use PHP curl? This is my code:
// $content = file_get_contents('http://www.datadiary.com/Company/311734/dimsinstituteofhotelmanagement');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.datadiary.com/Company/311734/dimsinstituteofhotelmanagement');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_COOKIESESSION, false);
$content = curl_exec($ch);
curl_close($ch);
echo $content;
By default curl_exec sends the response to the output (usually the browser). Set the CURLOPT_RETURNTRANSFER option if you want curl_exec to return the result instead:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
Relevant manual entries:
http://php.net/curl-exec
http://php.net/curl-setopt
You need to add the option to return the transfer as a string:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);