Pregunta

¿cómo es que esto no está funcionando:

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

consigo múltiples errores que me dice la petición HTTP fracasaron. En última instancia quiero obtener los resultados de este archivo en una matriz, por ejemplo

Descripción = CNN.com ofrece la última noticias de última hora, etc.

Palabras clave = CNN, noticias CNN, CNN.com, CNN televisión, etc.

Pero esta etapa inicial no está funcionando. Cualquier ayuda por favor?

Editar Información adicional:

Errores:

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" 
¿Fue útil?

Solución

(Nota: Potencialmente inútil respuesta una vez a la respuesta real se ha encontrado ...)


Mientras que estés averiguar el problema XML (seguir trabajando en ello!) Sabe que también se puede obtener la respuesta YQL atrás como JSON. He aquí un ejemplo polvo rápido:

$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);

Dar una matriz como (texto truncado para la pantalla):

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

Tenga en cuenta que la consulta YQL ( probarlo en la consola ) es ligeramente diferente a la suya, para hacer que el PHP simple.

Otros consejos

Bueno, el XML es getable. En cuanto a validez, carece <?xml version="1.0"?>, sin embargo, yo creo que no es necesario.

<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="5" yahoo:created="2010-03-09T05:09:03Z" yahoo:lang="en-US" yahoo:updated="2010-03-09T05:09:03Z" yahoo:uri="http://query.yahooapis.com/v1/yql?q=select+*+from+html+where+xpath%3D%22%2F%2Fmeta%22+and+url%3D%22http%3A%2F%2Fwww.cnn.com%22"><results><meta content="HTML Tidy for Java (vers. 26 Sep 2004), see www.w3.org" name="generator"/><meta content="1800;url=?refresh=1" http-equiv="refresh"/><meta content="CNN.com delivers the latest breaking news and information on the latest top stories, weather, business, entertainment, politics, and more. For in-depth coverage, CNN.com provides special reports, video, audio, photo galleries, and interactive guides." name="Description"/><meta content="CNN, CNN news, CNN.com, CNN TV, news, news online, breaking news, U.S. news, world news, weather, business, CNN Money, sports, politics, law, technology, entertainment, education, travel, health, special reports, autos, developing story, news video, CNN Intl" name="Keywords"/><meta content="text/html; charset=iso-8859-1" http-equiv="content-type"/></results></query><!-- total: 250 --> 
 

Probado en mi servidor local (PHP 5.3), ningún otro error. He utilizado el código fuente y funciona. He aquí un print_r ():


SimpleXMLElement Object
(
    [results] => SimpleXMLElement Object
        (
            [meta] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [content] => HTML Tidy for Java (vers. 26 Sep 2004), see www.w3.org
                                    [name] => generator
                                )

                        )

                    [1] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [content] => 1800;url=?refresh=1
                                    [http-equiv] => refresh
                                )

                        )

                    [2] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [content] => CNN.com delivers the latest breaking news and information on the latest top stories, weather, business, entertainment, politics, and more. For in-depth coverage, CNN.com provides special reports, video, audio, photo galleries, and interactive guides.
                                    [name] => Description
                                )

                        )

                    [3] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [content] => CNN, CNN news, CNN.com, CNN TV, news, news online, breaking news, U.S. news, world news, weather, business, CNN Money, sports, politics, law, technology, entertainment, education, travel, health, special reports, autos, developing story, news video, CNN Intl
                                    [name] => Keywords
                                )

                        )

                    [4] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [content] => text/html; charset=iso-8859-1
                                    [http-equiv] => content-type
                                )

                        )

                )

        )

)

Yo sugeriría que le permite codificar la URL, pero que ya está hecho. Usted podría tratar de realizar la consulta con rizo.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top