douxun2018 2013-02-27 18:10
浏览 92
已采纳

too long

I use PHP's simplexml_load_file() function to call an API that returns changed results based on a Timestamp that I send.

So the API will return only results that have changed since my Timestamp. The problem I am having is if the timestamp is too soon and there are no results for the API to return, then it does not return a proper XML file, instead it will just return a blank page.

This is causing all sorts of problems with simplexml_load_file

Here is a simple test I can run...

$xml = 'http://api.rescuegroups.org/rest/?key=CZivWWGD&type=orgs&limit=300&updatedAfter=1361941202&startPage=1';

$xmlObj = new SimpleXMLElement($xml, NULL, TRUE);

This results in...

Warning: SimpleXMLElement::__construct(): http://api.rescuegroups.org/rest/?key=CZivWWGD&type=orgs&limit=300&updatedAfter=1361941202&startPage=1:1: parser error : Document is empty in E:\Server\htdocs\labs\freelance\dogAPI\testorg.php on line 9

Warning: SimpleXMLElement::__construct(): in E:\Server\htdocs\labs\freelance\dogAPI\testorg.php on line 9

Warning: SimpleXMLElement::__construct(): ^ in E:\Server\htdocs\labs\freelance\dogAPI\testorg.php on line 9

Warning: SimpleXMLElement::__construct(): http://api.rescuegroups.org/rest/?key=CZivWWGD&type=orgs&limit=300&updatedAfter=1361941202&startPage=1:1: parser error : Start tag expected, '<' not found in E:\Server\htdocs\labs\freelance\dogAPI\testorg.php on line 9

Warning: SimpleXMLElement::__construct(): in E:\Server\htdocs\labs\freelance\dogAPI\testorg.php on line 9

Warning: SimpleXMLElement::__construct(): ^ in E:\Server\htdocs\labs\freelance\dogAPI\testorg.php on line 9

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in E:\Server\htdocs\labs\freelance\dogAPI\testorg.php:9 Stack trace: #0 E:\Server\htdocs\labs\freelance\dogAPI\testorg.php(9): SimpleXMLElement->__construct('http://api.resc...', 0, true) #1 {main} thrown in E:\Server\htdocs\labs\freelance\dogAPI\testorg.php on line 9

Now if I pass the API a Timestamp that is further back where there are results, then it will return a perfect XML document.

I am looking for a way to possibly prevent this nasty error from happening somehow?

  • 写回答

2条回答 默认 最新

  • duanhe6718 2013-02-27 18:14
    关注
    $xml = @simplexml_load_file('http://api.rescuegroups.org/rest/?key=CZivWWGD&type=orgs&limit=300&updatedAfter=1361941202&startPage=1');
    if(false !== $xml)
    {
        // Do anything with xml
    }
    

    If there an errors @ hide it and return false

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料