这是我的 php 代码:
<?php
if (array_key_exists("site", $_GET) && $_GET["site"] != "" )
{
$url = $_REQUEST['site'];
$url = " http://api.mywot.com/0.4/public_query2?target=";
$result= $url.urlencode($url);
$process = curl_init($result);
//init curl connection
curl_setopt($process, CURLOPT_HEADER, 0);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_RETURNTRANSFER,1);
curl_setopt($process,CURLOPT_CONNECTTIMEOUT,1);
$resp = curl_exec($process);
curl_close($process);
header('Content-Type: text/xml');
$xml = new SimpleXMLElement($resp);
}
?>
假定的 xml:
<?xml version="1.0" encoding="UTF-8"?>
<query target="example.com">
<application name="0" r="89" c="44"/>
<application name="1" r="89" c="46"/>
<application name="2" r="92" c="47"/>
<application name="4" r="93" c="48"/>
</query>
我知道怎么读这种xml文件
<Status>
<code>200</code>
<request>geocode</request>
</Status>
$status = (int) $xml->Status->code;
上面的xml中有application
等标签重复,如何读取某些标签?
最佳答案
application
项将作为数组访问,例如:$xml->application[3]
$x = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?>
<query target="example.com">
<application name="0" r="89" c="44"/>
<application name="1" r="89" c="46"/>
<application name="2" r="92" c="47"/>
<application name="4" r="93" c="48"/>
</query>');
print_r($x); // View entire XML structure
print_r($x->application[1]); // View the 2nd application item
echo $x->application[1]['c']; // Get 'c' attribute from 2nd item (46)
关于php - 对如何在 php 中读取 curl 返回的 xml 对象感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4706369/