2012-01-22 35 views
1

所以我有这个私有函数:PHP卷曲获得XML源不返回所有数据

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

如果例如通过以下链接使用:http://www.metacafe.com/api/item/cb-xuFyGC0jJqPfhMoFnewj4Da_ZhHCz4L2/

现在的问题是,没有按“T返回所有的数据,我得到的是这样的:

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

       ) 

     ) 

为什么没有返回描述和标签,这是对我来说非常重要?

回答

2

这不是由于curl但如何SimpleXML的处理CDATA:

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

将解释CDATA文本节点,看到XML constants on PNP.net

+0

哦,不知道......我是XML新手,刚学到了一些新的东西,谢谢你,也得到了解决方案。 – Alex

+0

我想现在我所要做的就是preg_match来分离[描述]中的数据(标签,描述,提交者),对吗? – Alex

1

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

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

tnx但LIBXML_NOCDATA解决了问题 – Alex

+0

不,它只能解决一个“问题”。所有的数据都坐在那里,只是没有被'print_r()'(没有'LIBXML_NOCDATA')显示。 – salathe

+0

哦,我明白了,但是这样做让我的问题变得更加困难,因为现在为了只获得我需要的数据,我必须通过'DOMDocument'通过'LIBXML_NOCDATA'编写一些大规模的'preg_match'我可以使它更容易访问需要的数据 – Alex