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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号