如何解决带有curl的php解析器xml [重复]
| 这个问题已经在这里有了答案:解决方法
CURL与XML无关,它只是为您检索XML的一种传递机制。这就像说牛奶的味道不同,因为一位送货司机使用汽车,而一位卡车则使用卡车。
要在PHP中处理XML,请使用DOM或SimpleXML
$dom = new DOMDocument();
$dom->loadXML($xml); // where $xml is the result from curl
... do whatever you want with the DOM object here ...
, 您可以将结果加载到“ 1”中并遍历数据。这是一个例子:
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,CURLOPT_ENCODING,\"deflate,gzip,identity\");
curl_setopt($ch,CURLOPT_HEADER,FALSE);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch,CURLOPT_HTTPGET,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 .
--------------------
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。