0

I am attempting to use Yahoos YQL language to access data via xml using php and simplexml. I found the following code on the web:

$xml = simplexml_load_file('
http://query.yahooapis.com/v1/public
/yql?q=select%20*%20from%20geo.places%20where%20text=%22sunnyvale,%20ca%22
');

I can paste the url in the browser and it returns the xml just fine.

When I try to access it via my php file I get the following error:

Warning: simplexml_load_file(): I/O warning : failed to load external entity " http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.places%20where%20text=%22sunnyvale,%20ca%22 " in C:\wamp\www\YQL Example\index.php on line 11

Any help would be appreciated.

Dana

1
  • What does this show? $data = file_get_contents('http://query.yahooapis.com/v1/public /yql?q=select%20*%20from%20geo.places%20where%20text=%22sunnyvale,%20ca%22); var_dump($data);
    – Mr. Llama
    Jun 23, 2014 at 21:56

2 Answers 2

1

Your URI string is broken, the newlines in there.

Instead, assign the URI to a variable, then load it. The only "technical" reason to do so is to better organize your code because the string could be rightly placed as well inside the function call. But variables are your friend oh so often, so use them:

$uri = 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.places%20where%20text=%22sunnyvale,%20ca%22';
$xml = simplexml_load_file($uri);

Just works.

<?xml version="1.0" encoding="UTF-8"?>
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="9" yahoo:created="2014-06-23T22:16:26Z" yahoo:lang="en-US"><results>

...

</results></query>
<!-- total: 40 -->
<!-- engine4.yql.bf1.yahoo.com -->
0
0

simplexml_load_file() gets an XML file (either a file on your disk or a URL) into an object.

Use file_get_contents() to get the feed as a string, and use simplexml_load_string() then.

1
  • Internally simplexml_load_file() does that already. It uses the same internal mechanism like file_get_contents() (PHP Streams incl. the PHP stream wrapper for HTTP).
    – hakre
    Jun 23, 2014 at 22:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.