带有 curl 的 php 解析器 xml

标签 php xml parsing curl

<分区>

Possible Duplicate:
PHP Curl , extract a XML response

如何用 curl 解析 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 树

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

最佳答案

CURL 与 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 ...

关于带有 curl 的 php 解析器 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6180669/

相关文章:

javascript - 使用 parsimmon 库解析基于缩进的语言

python - 解析 hh :mm in Python

php - ISSET PHP mysql

PHP 和 MySQL - 只需一个查询即可获取顶级类别

python - 连接列表中的值(XML、Python)

java - 表格布局 : Text in the last column is not getting displayed completly

android - 使用 Jsoup 解析文本文件

php - 无法在 PHP echo 语句中放置超链接

php - 使用xpath获取文本内容

c# - 使用 XML 进行批量插入