php php从远程url获取xml而不是文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18787477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:13:22  来源:igfitidea点击:

php get xml from remote url not a file

phpxmlcross-domain

提问by Peter Saxton

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

我正在尝试从此 url 获取 xml 数据。

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

http://cloud.tfl.gov.uk/TrackerNet/PredictionSummary/V我可以直接在我的浏览器栏中输入 url 并获得所需的结果。

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.

我正在尝试使用下面的代码来获取 xml 并将其返回到我的浏览器,但现在在正确的域中,以便它可以通过 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

我已经尝试过 file_get_contents 和 simplexml_load_file 但都没有解决

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

我原以为问题出在 url 末尾没有文件这一事实。到目前为止没有以下工作

采纳答案by George Brighton

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:

simplexml_load_file()返回一个对象而不是 XML 字符串。即便如此,使用您当前的代码,XML 也会在 HTML 中丢失。以下相当于直接访问 URL:

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,请尝试此操作。它加载 XML 并打印出根节点的名称 - 在本例中为ROOT

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

Does this help?

这有帮助吗?

回答by x29a

See the documentation for

请参阅文档

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

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

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

to output the string return by file_get_contents()

输出由file_get_contents()返回的字符串

<?php
    $xml = file_get_contents("http://cloud.tfl.gov.uk/TrackerNet/PredictionSummary/V");
    echo $xml;
?>

回答by Vyas Reddy

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;

 ?>