1

I am using the following function

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");

After I run that function I have

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

Can you please tell me how can I traverse $myTestingUrl to get the content of entry key "ActionUrl" (http://www.my_actionUrl.com/) in a variable in php?

Thank you!

  • possible duplicate of [Traversing XML in PHP](http://stackoverflow.com/questions/6301084/traversing-xml-in-php) – ajreal Nov 12 '12 at 13:01

2 Answers2

3

Try

$xml = simplexml_load_string($myTestingUrl );
$items = $xml->xpath('/map/entry/entry[@key="ActionUrl"]/text()');
echo $items[0];
air4x
  • 5,618
  • 1
  • 23
  • 36
  • Good one! Here is more info on [simpleXML](http://www.w3schools.com/php/php_xml_simplexml.asp) – VDP Nov 12 '12 at 12:57
  • On the last line i receive the following error: Notice: Undefined index: 0 –  Nov 12 '12 at 14:07
  • Copy the code from http://eval.in/2767 and try if it is working. If it is working, then `echo` your xml and check it for any difference. – air4x Nov 12 '12 at 14:55
  • Seems like my server was having simpleXml disabled. Both of the solutions are working! Thanks a lot! :) Ps. On my server just echo $xml->entry->entry.PHP_EOL; is display the right value. –  Nov 12 '12 at 15:10
2

I prefer @air4x's XPath method but here it is without XPath - for the purpose of showing element and attribute access in SimpleXML:

Codepad demo

$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;
            }
        }
    }
}
MrCode
  • 63,975
  • 10
  • 90
  • 112
  • I'm getting a blank page and no answer for this one. I've made the following steps: `echo $myTestingUrl; (prints out http://urlllll/library.xml) $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; } } } }` –  Nov 12 '12 at 14:16
  • @CSSensei you must have an error in some other code, this code works (as seen on the codepad demo). Have a look in your error log. – MrCode Nov 12 '12 at 14:18
  • I have no other code on my page just this for testing it. On codepad there's that trim function. Is that mandatory for the extraction to work? –  Nov 12 '12 at 14:24
  • Try adding the trim like `simplexml_load_string(trim($myTestingUrl))`, I used it in the demo because I added a newline before the opening xml tag, just for readability. – MrCode Nov 12 '12 at 14:32
  • Seems like my server was having simpleXml disabled. Both of the solutions are working! Thanks a lot! –  Nov 12 '12 at 15:10