2012-01-22 32 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