user3768973
user3768973

Reputation: 1

php simplexml_load_file() of url returns error

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

Upvotes: 0

Views: 656

Answers (2)

hakre
hakre

Reputation: 198119

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 -->

Upvotes: 1

Rusher
Rusher

Reputation: 507

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.

Upvotes: 0

Related Questions