1

我正在使用以下功能

function file_get_contents_curl($url) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);       

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}
$myTestingUrl = file_get_contents_curl("myUrl");

运行该功能后,我有

$myTestingUrl =
<?xml version="1.0" encoding="UTF-8"?>
<map>
    <entry key="keyName">
        <entry key="ActionUrl">http://www.my_actionUrl.com/</entry>
    </entry>
</map>

您能告诉我如何遍历 $myTestingUrl 以获取 php 变量中的条目键“ActionUrl”(http://www.my_actionUrl.com/)的内容吗?

谢谢!

4

2 回答 2

3

尝试

$xml = simplexml_load_string($myTestingUrl );
$items = $xml->xpath('/map/entry/entry[@key="ActionUrl"]/text()');
echo $items[0];
于 2012-11-12T12:56:14.023 回答
2

我更喜欢@air4x 的 XPath 方法,但这里没有 XPath - 目的是在SimpleXML中显示元素和属性访问:

键盘演示

$obj = simplexml_load_string($myTestingUrl);

foreach($obj->entry as $entry)
{
    if(isset($entry->entry))
    {
        foreach($entry->entry->attributes() as $key => $value)
        {
            if($key == 'key' && $value == 'ActionUrl')
            {
                echo 'ActionUrl is: ' . (string)$entry->entry;
                break 1;
            }
        }
    }
}
于 2012-11-12T13:04:12.267 回答