【问题标题】:php curl to get xml feed does not return all the dataphp curl获取xml提要不返回所有数据
【发布时间】:2012-01-22 20:29:50
【问题描述】:

所以我有这个私有函数:

private function curl_get($url)
{
    // Initiate the curl session
    $ch = curl_init();

    // Set the URL
    curl_setopt($ch, CURLOPT_URL, $url);

    // Removes the headers from the output
    curl_setopt($ch, CURLOPT_HEADER, 0);

    // Return the output instead of displaying it directly
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    // Execute the curl session
    $output = curl_exec($ch);

    // Close the curl session
    curl_close($ch);

    return $output;
}

我使用 if 例如这个链接:http://www.metacafe.com/api/item/cb-xuFyGC0jJqPfhMoFnewj4Da_ZhHCz4L2/

现在的问题是它没有返回所有数据,我得到的只是:

[data] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [version] => 2.0
                    [source] => Metacafe
                )

            [title] => Metacafe
            [channel] => SimpleXMLElement Object
                (
                    [title] => SimpleXMLElement Object
                        (
                        )

                    [link] => http://www.metacafe.com/watch/cb-GKeDVFevZxk_rNri_uz_K01azz3uV_ZZ/
                    [image] => SimpleXMLElement Object
                        (
                            [url] => http://s.mcstatic.com/Images/MCLogo4RSS.jpg
                            [link] => http://www.metacafe.com
                            [title] => Metacafe
                            [height] => 65
                            [width] => 229
                        )

                    [description] => SimpleXMLElement Object
                        (
                        )

                    [item] => SimpleXMLElement Object
                        (
                            [id] => cb-GKeDVFevZxk_rNri_uz_K01azz3uV_ZZ
                            [author] => CBS
                            [title] => Romney Concedes South Carolina Primary
                            [link] => http://www.metacafe.com/watch/cb-GKeDVFevZxk_rNri_uz_K01azz3uV_ZZ/romney_concedes_south_carolina_primary/
                            [rank] => 4.00
                            [category] => News & Events
                            [description] => SimpleXMLElement Object
                                (
                                )

                            [guid] => http://www.metacafe.com/watch/cb-GKeDVFevZxk_rNri_uz_K01azz3uV_ZZ/romney_concedes_south_carolina_primary/
                            [pubDate] => 18 hours ago +0000
                        )

                )

        )

为什么它不返回对我来说非常重要的描述和标签?

【问题讨论】:

    标签: php xml curl


    【解决方案1】:

    这不是由于curl 而是 SimpleXml 如何处理 CDATA:

    $xml = simplexml_load_string($stringFromCurl, 'SimpleXMLElement', LIBXML_NOCDATA);
    

    将 CDATA 解释为文本节点,请参阅 XML constants on PNP.net

    【讨论】:

    • 哦,不知道……我是 XML 新手,多亏了你才学到了一些新东西,也得到了解决方案。
    • 我想现在我要做的就是 preg_match 来分隔 [description] 中的数据(标签、描述、提交者),对吧?
    【解决方案2】:

    SimpleXML 确实有“所有数据”,只是通过print_r() 不可见。

    echo $your_simplexml_object->channel->item->description;
    

    【讨论】:

    • 不,它只能解决“问题”。所有的数据都在那里,只是没有被print_r() 显示(没有LIBXML_NOCDATA)。
    • 哦,我明白了,但这有点让我的问题变得更加困难,因为现在为了只获得我需要的数据,我必须写一些大量的 preg_match LIBXML_NOCDATADOMDocument我可以让它更容易访问所需的数据
    • 您对preg_match()LIBXML_NOCDATADOMDocument 的使用与您错误地认为SimpleXML 对象没有description 内容这一事实无关。跨度>
    • 确实是@salathe,确实是。非常感谢你今天又教了我一件事。
    猜你喜欢
    • 2011-08-10
    • 2014-07-11
    • 1970-01-01
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    • 2014-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多