php解析器xml与curl [英] php parser xml with curl

查看:203
本文介绍了php解析器xml与curl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

PHP Curl,提取XML响应

到php解析器xml与curl?这里是我的代码,如何获取xml每个项目的值?感谢。

How to php parser xml with curl? Here is my code, how to get xml each item values? Thanks.

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


推荐答案

CURL与XML无关,传递机制,为您检索XML。

CURL has nothing to do with XML, it's simply a delivery mechanism that retrieves XML for you. It's like saying milk tastes different because one delivery driver uses a car and one uses a truck.

要使用PHP处理XML,请使用 DOM SimpleXML < a>

To process XML in PHP, use DOM or SimpleXML

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

这篇关于php解析器xml与curl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆