I have a page on a remote server that returns a single xml value
<?xml version="1.0" ?><Tracker>12345</Tracker>
How do I go about getting the value 12345 or whatever is in the tag into a PHP variable?
I know I can’t do something as simple as:
<?php
$var = http://www.www.com/file.php;
echo $var; //12345
?>
I’m new to the entire idea of creating a web service – I think that’s what this should be.?.
You have to go in two steps :
simplexml_load_stringfor instanceNote that those two steps can be merged as one, using
simplexml_load_file, if the configuration of your server allows you to do that.Once you have that XML in a PHP variable, using SimpleXML, it’s quite easy to get the value you want. For instance :
Will get you
And getting the content of that XML from the remote URL can be as simple as :
And if you can do that, you can also use that remote URL directly with SimpleXML :
But this will only work if
allow_url_fopenis enabled in your server’s configuration.If
allow_url_fopenis disabled on your server, you can get the XML from the remote server using curl ; something like this should do the trick, for instance :And, then, you can still use
simplexml_load_string, as I did in my first example.(If using curl, take a look at the documentation of
curl_setopt: there are several options that might interest your — for instance, to specify timeouts)