Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php get xml from remote url not a file

I am trying to get the xml data from this url.

http://cloud.tfl.gov.uk/TrackerNet/PredictionSummary/V I can enter the url directly into my browser bar and get the required result.

I am trying to use the code below to fetch the xml and return it to my browser but now in the correct domain so it can be accessed by javascript.

<!DOCTYPE html>
<html>
<head>
    <link type='text/css' rel='stylesheet' href='style.css'/>
    <title>Get Started!</title>
</head>
<body>
    <p><?php 
    /*echo file_get_contents("http://cloud.tfl.gov.uk/TrackerNet/PredictionSummary/V");*/
    echo simplexml_load_file("http://cloud.tfl.gov.uk/TrackerNet/PredictionSummary/V");
    ?></p>
</body>
</html>

I have tried both file_get_contents and simplexml_load_file but neither have worked out

I had assumed the problem was with the fact that there is not a file at the end of url. NONE OF BELOW WORKING SO FAR

like image 430
Peter Saxton Avatar asked Sep 13 '13 13:09

Peter Saxton


2 Answers

simplexml_load_file() returns an object rather than the XML string. Even then, with your current code, the XML would be lost within HTML. The following would be equivalent to visiting the URL directly:

header('Content-type: application/xml');
echo file_get_contents('http://cloud.tfl.gov.uk/TrackerNet/PredictionSummary/V');

To make sure you can parse the XML, try this. It loads the XML and prints out the name of the root node - in this case, ROOT:

$xml = simplexml_load_file($url); //retrieve URL and parse XML content
echo $xml->getName(); // output name of root element

Does this help?

like image 107
George Brighton Avatar answered Sep 28 '22 06:09

George Brighton


See the documentation for

http://php.net/manual/en/simplexml_load_file

for an example to print the output

<?php
    $xml = simplexml_load_file('http://cloud.tfl.gov.uk/TrackerNet/PredictionSummary/V');
    print_r($xml);
?>

or check the manual

http://php.net/manual/en/function.echo.php

to output the string return by file_get_contents()

<?php
    $xml = file_get_contents("http://cloud.tfl.gov.uk/TrackerNet/PredictionSummary/V");
    echo $xml;
?>
like image 21
x29a Avatar answered Sep 28 '22 07:09

x29a