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 に答える