如何用curl php解析xml?这是我的代码,如何获取xml每个项目的值?感谢。
<?php
define("EMAIL_ADDRESS", "youlichika@hotmail.com");
$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 TREE
<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>
答案 0 :(得分:11)
CURL与XML无关,它只是一种为您检索XML的交付机制。这就像说牛奶味道不同,因为一个送货司机使用汽车而一个人使用卡车。
$dom = new DOMDocument();
$dom->loadXML($xml); // where $xml is the result from curl
... do whatever you want with the DOM object here ...
答案 1 :(得分:5)
您只需将结果加载到SimpleXML
并循环数据即可。这是一个例子:
define("EMAIL_ADDRESS", "youlichika@hotmail.com");
$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 .
--------------------