Search code examples
phpsimplexmlyql

Simple XML Load File not working


how come this isn't working:

$url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20xpath%3D%22%2F%2Fmeta%22%20and%20url%3D%22http://www.cnn.com%22&format=xml&diagnostics=false";

$xml = (simplexml_load_file($url))

I get multiple errors telling me the HTTP request failed. Ultimately I want to get the results from this file into an array eg

Description = CNN.com delivers the latest breaking news etc.

Keywords = CNN, CNN news, CNN.com, CNN TV etc.

But this initial stage isn't working. Any help please?

EDIT Additional information:

Errors:

warning: simplexml_load_file(http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20xpath%3D%22//meta%22%20and%20url%3D%22http://www.cnn.com%22&format=xml&diagnostics=false) [function.simplexml-load-file]: failed to open stream: HTTP request failed!
# warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20xpath%3D%22//meta%22%20and%20url%3D%22http://www.cnn.com%22&format=xml&diagnostics=false" 

Solution

  • (Note: Potentially useless answer once a real answer has been found…)


    While you're figuring out the XML problem (keep working on it!) know that you can also get the YQL response back as JSON. Here's a quickie example:

    $url = "http://query.yahooapis.com/v1/public/yql?q=select+%2A+"
         . "from+html+where+xpath%3D%22%2F%2Fmeta%5B%40name%3D%27"
         . "Keywords%27+or+%40name%3D%27Description%27%5D%22+and+"
         . "url%3D%22http%3A%2F%2Fwww.cnn.com%22&format=json&diagnostics=false";
    
    // Grab YQL response and parse JSON
    $json   = file_get_contents($url);
    $result = json_decode($json, TRUE);
    
    // Loop over meta results looking for what we want
    $items = $result['query']['results']['meta'];
    $metas = array();
    foreach ($items as $item) {
        $metas[$item['name']] = $item['content'];
    }
    print_r($metas);
    

    Giving an array like (text truncated for the screen):

    Array
    (
        [Description] => CNN.com delivers the latest breaking news and …
        [Keywords] => CNN, CNN news, CNN.com, CNN TV, news, news online …
    )
    

    Note that the YQL query (try it in the console) is slightly different to yours, to make the PHP simpler.