You have to go in two steps :
- Get the XML string from the remote server
- and, then, parse that XML to extract the data
Note 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 :
$string = '<?xml version="1.0" ?><Tracker>12345</Tracker>';
$xml = simplexml_load_string($string);
echo (string)$xml;
Will get you
12345
And getting the content of that XML from the remote URL can be as simple as :
$string = file_get_contents('http://your-remote-url');
And if you can do that, you can also use that remote URL directly with SimpleXML :
$xml = simplexml_load_file('http://your-remote-url');
echo (string)$xml;
But this will only work if allow_url_fopen
is enabled in your server's configuration.
If allow_url_fopen
is disabled on your server, you can get the XML from the remote server using curl ; something like this should do the trick, for instance :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://your-remote-url");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$string = curl_exec($ch);
curl_close($ch);
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)