【问题标题】:Simple XML Load File not working简单的 XML 加载文件不起作用
【发布时间】:2011-01-25 11:39:26
【问题描述】:

这怎么不行:

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

我收到多个错误,告诉我 HTTP 请求失败。最终我想将此文件中的结果放入一个数组中,例如

描述 = CNN.com 提供最新的突发新闻等。

关键字 = CNN、CNN 新闻、CNN.com、CNN 电视等。

但是这个初始阶段不起作用。有什么帮助吗?

编辑 附加信息:

错误:

警告: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]:打开流失败:HTTP 请求失败!
# 警告:simplexml_load_file() [function.simplexml-load-file]:I/O 警告:未能加载外部实体“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" 

【问题讨论】:

  • 我在本地 xampp 安装上对此进行了测试,但无法重现您的错误消息。我的脚本:pastie.org/2895218。您能否尝试在浏览器中打开请求的页面,将其保存为 test.xml,将其上传到您的服务器并尝试simplexml_load_file("test.xml");?当你尝试这个时,你会得到同样的错误吗?
  • 我尝试了你的脚本(没有运气),但是按照你的建议使用了一个本地文件,它确实有效!那么这是什么意思?感谢您迄今为止的大力帮助...
  • hmmm...您的服务器 - oder php-settings 一定有问题 - 但我不知道是什么... 再给我一些时间:我会回来的
  • 您使用的是什么版本的 libxml? (phpinfo() 会告诉你)
  • 在脚本开头添加error_reporting(-1);,同时检查phpinfo(),确保“http”出现在“Registered PHP Streams”部分。

标签: php simplexml yql


【解决方案1】:

(注意:一旦找到真正的答案,可能会无用的答案……)


在您解决 XML 问题时(继续努力!)知道您也可以将 YQL 响应返回为 JSON。这是一个快速示例:

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

给出一个数组,例如(屏幕截断的文本):

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

请注意,YQL 查询 (try it in the console) 与您的略有不同,以使 PHP 更简单。

【讨论】:

  • 嗨,这很好,但我想要所有的元数据,而不仅仅是描述和关键字。该脚本需要适用于每个站点,因此它可以解析站点抛出的每个元标记,而不仅仅是指定列表中的那些。谢谢。
  • 在这种情况下,将 YQL 查询更改为限制较少(如您的原始查询)。请注意,并非所有元标记都包含namecontent 属性。
  • 我实际上只是尝试了您的代码,不幸的是我得到了类似的错误:file_get_contents(http://query.yahooapis.com/v1/public/yql?q=select+%2A+from+html+where+xpath%3D%22%2F%2Fmeta...) [function.file-get-contents]: failed to open stream: HTTP request failed!warning: Invalid argument supplied for foreach() 可能有什么问题???
  • 这是一个远景,但是您在运行脚本的地方和外部世界之间是否有防火墙?尝试使用 cURL 来查看它是否也被阻止。你能远程登录到 query.yahooapis.com 吗?
  • 抱歉,但我不知道如何形成 cURL 请求(有一些简单的代码可以尝试吗?)而且我没有 telnet 客户端。似乎很遗憾,在我的开发环境(本地主机)中,在我的生产环境(即网络)中可能会起作用的东西不起作用。
【解决方案2】:

嗯,XML 是 GETable。至于有效,它缺少<?xml version="1.0"?>,但我认为它不是必需的。

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

在我的本地服务器(PHP 5.3)上测试过,没有报告错误。我已经使用了您的源代码,并且可以正常工作。这是一个 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
                                )

                        )

                )

        )

)

我建议您对 URL 进行编码,但这已经完成了。您可以尝试使用 cURL 执行查询。

【讨论】:

    猜你喜欢
    • 2016-01-31
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    相关资源
    最近更新 更多