douqiu0796 2015-02-19 14:51
浏览 274
已采纳

在XML地址中插入外部变量(连接)

So its me again, i'm facing a wired issue while calling an external XML file into my PHP code. Thing is, same way of concatenating a variable with URL and then calling the URL as json works fine but when i try to concatenate a variable in URL of XML, it returns error. Here is what works fine (json):

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

Here is XML one which does 'not' works:

$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);

And the error is:

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

Maybe its a dumb question but i tried finding about that around and couldn't find this simple problem's issue.

  • 写回答

1条回答 默认 最新

  • dongqiang2358 2015-02-20 12:59
    关注

    Use:

    $xml = simplexml_load_string($xmlString);
    

    I've made this php file and it worked fine:

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

    Can you try this?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下