6

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

6
  • remove the = as you are not assigning Commented Sep 13, 2013 at 13:25
  • Your syntax is wrong. I suggest you read php.net/manual/en/function.echo.php Commented Sep 13, 2013 at 13:26
  • Ahh my mistake a left over from the other things I have tried. I have corrected the syntax and it still doesn't work Commented Sep 13, 2013 at 13:29
  • @PeterSaxton: if you're trying to display the XML document in your browser, then simply use file_get_contents. echo file_get_contents("http://cloud.tfl.gov.uk/TrackerNet/PredictionSummary/V"); Commented Sep 13, 2013 at 13:30
  • unless I've missed something that's what I have tried to no avail Commented Sep 13, 2013 at 13:32

3 Answers 3

7

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?

Sign up to request clarification or add additional context in comments.

8 Comments

I had assumed that return the xml even lost in the html would be visible even if just as a messy string. I just want to have confirmation that the xml is correctly loaded into my php script and the easiest way to do that would be to see it
So my first code sample, in a PHP file of its own, doesn't display anything at your end?
In php.ini, is the allow_url_fopen directive set to On?
I am looking but have yet to find the php.ini file
What OS is PHP running on (Windows, Ubuntu etc.)?
|
2

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;
?>

3 Comments

Can you elaborate on whats not working? Look with wireshark (or your browsers dev tools) which data gets over the net. Maybe they are looking for browser specific requests on that page?
I just end up with a blank page. Have tested in IE and chrome. In chrome when I press F12(is that developer tools). and when I inspect the element I can see the paragraph element is empty. When I check the console no error message has occured
I am new to serverside scripting and php and have spent several days looking through but when I am faced with a blank screen I am finding it very difficult to trouble shoot. I was hoping for a simple bounce option so I could keep using javascript I know well.
0

Try This ... Worked for me..simple and easy ..

<?php
 $url=file_get_contents('http://www.w3schools.com/php/note.xml');
 $xml = new SimpleXMLElement($url);

 echo $xml->to;

 ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.