2011-05-30 65 views
2

Possible Duplicate:
PHP Curl , extract a XML responsephp分析器xml帶捲曲

如何使用curl進行php解析器xml?這裏是我的代碼,如何獲取xml每個項目的值?謝謝。

<?php 
define("EMAIL_ADDRESS", "[email protected]"); 
$ch = curl_init(); 
$cv = curl_version(); 
$user_agent = "curl ${cv['version']} (${cv['host']}) libcurl/${cv['version']} ${cv['ssl_version']} zlib/${cv['libz_version']} <" . EMAIL_ADDRESS . ">"; 
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); 
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt"); 
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt"); 
curl_setopt($ch, CURLOPT_ENCODING, "deflate, gzip, identity"); 
curl_setopt($ch, CURLOPT_HEADER, FALSE); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_HTTPGET, TRUE); 
curl_setopt($ch, CURLOPT_URL, "http://en.wikipedia.org/w/api.php?action=opensearch&search=tokyo&format=xml&limit=2"); 
$xml = curl_exec($ch); 
curl_close($ch); 
var_dump($xml); 
?> 

XML樹

<SearchSuggestion version="2.0"> 
<Query xml:space="preserve">tokyo</Query> 
<Section> 
<Item> 
<Text xml:space="preserve">Tokyo</Text> 
<Description xml:space="preserve"> 
, officially , is one of the 47 prefectures of Japan. 
</Description> 
<Url xml:space="preserve">http://en.wikipedia.org/wiki/Tokyo</Url> 
<Image source="http://upload.wikimedia.org/wikipedia/commons/thumb/a/a6/Japan_Tokyo3.png/36px-Japan_Tokyo3.png" width="36" height="49"/> 
</Item> 
<Item> 
<Text xml:space="preserve">Tokyo Broadcasting System</Text> 
<Description xml:space="preserve"> 
(), TBS Holdings, Inc. or TBSHD, is a stockholding company in Tokyo, Japan. It is a parent company of a television network named and radio network named . 
</Description> 
<Url xml:space="preserve"> 
http://en.wikipedia.org/wiki/Tokyo_Broadcasting_System 
</Url> 
</Item> 
</Section> 
</SearchSuggestion> 
+1

有什麼好東西(http://stackoverflow.com/search?q= php + parse + xml) – 2011-05-30 21:00:33

回答

11

捲曲無關與XML,它只是一個傳遞機制,爲你檢索XML。這就像說牛奶的味道不同,因爲一個送貨司機使用汽車,一個使用卡車。

在PHP中處理XML,使用DOMSimpleXML

$dom = new DOMDocument(); 
$dom->loadXML($xml); // where $xml is the result from curl 
... do whatever you want with the DOM object here ... 
+0

致命錯誤:找不到類'DOM' – 2011-05-30 21:07:56

+0

如果是這種情況,那麼它被編譯爲一個單獨的模塊,需要安裝和/或激活。你在哪個平臺/操作系統上? – 2011-05-30 21:09:49

+1

你想要'$ dom = new DOMDocument();' – 2011-05-30 21:10:31

5

您可以將結果只加載到SimpleXML和環比數據。這裏有一個例子:

define("EMAIL_ADDRESS", "[email protected]"); 
$ch = curl_init(); 
$cv = curl_version(); 
$user_agent = "curl ${cv['version']} (${cv['host']}) libcurl/${cv['version']} ${cv['ssl_version']} zlib/${cv['libz_version']} <" . EMAIL_ADDRESS . ">"; 
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); 
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt"); 
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt"); 
curl_setopt($ch, CURLOPT_ENCODING, "deflate, gzip, identity"); 
curl_setopt($ch, CURLOPT_HEADER, FALSE); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_HTTPGET, TRUE); 
curl_setopt($ch, CURLOPT_URL, "http://en.wikipedia.org/w/api.php?action=opensearch&search=tokyo&format=xml&limit=2"); 
$xml_data = curl_exec($ch); 
curl_close($ch); 

// Make sure we actually have something to parse 
if($xml_data) { 
    $parser = simplexml_load_string($xml_data); 

    foreach($parser->Section as $section) { 
    foreach($section->Item as $item) { 
     echo "{$item->Text}\n\n{$item->Description}\n--------------------\n"; 
    } 
    } 
} 

下面是一些示例輸出:[!從來沒有人問過這個]

$ php test.php 
Tokyo 


, officially , is one of the 47 prefectures of Japan. 

-------------------- 
Tokyo Broadcasting System 


(), TBS Holdings, Inc. or TBSHD, is a stockholding company in Tokyo, Japan. It is a parent company of a television network named and radio network named . 

-------------------- 
+0

simplexml似乎更容易,無需再次安裝。 – 2011-05-30 21:16:07