在XML地址中插入外部变量(串联)


Inserting an external variable in XML address (concatenation)

所以,我又遇到了一个问题,在将外部XML文件调用到我的PHP代码中时,我面临着一个有线问题。问题是,将一个变量和URL连接起来,然后将URL调用为json的方法同样有效,但当我尝试将一个XML的URL中的变量连接起来时,它会返回错误。以下是运行良好的(json):

$city = input[1]; // Input is an external input...
$liveWeather = file_get_contents("http://weather3.pricop.info/api.php?city=$city");

这是一个"不"工作的XML:

$city = input[1];
$xmlString = file_get_contents("http://api.openweathermap.org/data/2.5/forecast/daily?q=$city&mode=xml&units=imperial&cnt=9");
$xml = new SimpleXMLElement($xmlString);

错误是:

Warning: SimpleXMLElement::__construct(): Entity: line 1: parser error : Start tag expected, '<' not found in /home/u369795042/public_html/weather.php on line 18

也许这是一个愚蠢的问题,但我试着四处寻找,却找不到这个简单问题的问题。

使用:

$xml = simplexml_load_string($xmlString);

我制作了这个php文件,它运行良好:

<?php
error_reporting(E_ALL);
$city = "New+York";
$xmlString = file_get_contents("http://api.openweathermap.org/data/2.5/forecast/daily?q=$city&mode=xml&units=imperial&cnt=9");
$xml=simplexml_load_string($xmlString);
print_r($xml);
unset($xml);
exit();
?>

你能试试这个吗?