2014-04-02 29 views
0

我已经XML文档从链路捕获并随时我用使用simplexml_load_file(),它给了我*警告一个错误,说使用simplexml_load_file()开始标签预计, '<' 用C未找到:\ WAMP \ WWW \客商\ get_merchants.php第6行*使用simplexml_load_file()给错误:警告:使用simplexml_load_file():<a href="https://kga-dev.mirakl.net/api/shops?:1" rel="nofollow">https://kga-dev.mirakl.net/api/shops?:1</a>:分析器错误:

这是我的xml文件

<body> 
<shops> 
<shop>...</shop> 
<shop> 
<approval_delay xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
<approval_rate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
<order_messages_response_delay xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
<banner xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
<billing_info> 
<bank_city xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
<bank_name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
<bank_street xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
<zip_code xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
<bic xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
<iban xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
<owner xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
</billing_info> 

的一部分,这是我的简单php代码

<?php 

    $apiLink = "https://kga-dev.mirakl.net/api/shops?"; 

    $xml=simplexml_load_file($apiLink); 
    print_r($xml); 

    //echo $xml->shop_id; 

?> 
+0

什么是读取XML数据的第一个标志?您是否试图通过file_get_contents()获取xml数据的第一个符号,然后找到可用的第一个符号。 SimpleXML需要一个“<”,这是没有给出的。因此,另一个标志是内容读入的第一个标志。它是哪一个?也许它有助于获得内容并修剪它? – Marcel

+0

刚做了file_get_contents(),我得到了这个 {“shops”:[{“approval_delay”:null,“approval_rate”:null,“banner”:null,“billing_info”:{“bank_city”:null, :NULL, “bank_street”:NULL, “BIC”:NULL, “IBAN”:空, “所有者”:NULL, “ZIP_CODE”:空}, “信道”:[ “INIT”], “closed_from”:空, “closed_to”:null,“contact_informations”: – user3416016

+0

正如你所看到的,这不是XML。这看起来像JSON中的REST Response。在这个内容上试试json_decode(),你就可以为你的问题找到解决方案。 – Marcel

回答

1

此问题的解决方案是API提供JSON编码的字符串而不是XML字符串。所以传递的内容不能用SimpleXML分析。尝试使用以下示例获得它

$content = file_get_contents($apiLink); 
$data = json_decode($content); 

在这种情况下,$ data应该是一个对象或数组,其中包含api提供的所有数据。

+0

谢谢。我想你是正确的。 – user3416016

1

代码

<approval_rate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 

使用单引号

<?php 
    echo "<order_messages_response_delay xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true'/>"; 
?> 
+0

我无法改变这一点。有没有解决的办法? – user3416016

+0

您需要更改您在上面编写代码的get_merchants.php表单 –

+0

您不明白。 XML来自API,我无法更改文档。我只是试图加载它并从中获取细节。 – user3416016

1

你确定这是同一XML作为返回给脚本?请注意,您没有拨打:1 - 我无法检查,因为它需要授权,但在您给出的示例中,第7行没有错误,因此这意味着可能会返回其他内容,如下面的代码所示:

<?php 
$s = '<body> 
    <shops> 
     <shop>...</shop> 
     <shop> 
      <approval_delay xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
      <approval_rate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
      <order_messages_response_delay xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
      <banner xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
     </shop> 
    </shops> 
</body>'; 

$xml=simplexml_load_string($s); 
print_r($xml); 

?>

是不是授权问题您的结局?我需要授权给你的web服务:你以不同的方式处理你的IP或类似的东西?

+0

我删除了XML的一些部分,但它是如何开始的。此外,我删除了链接的某些部分,因此没有人可以访问它,因为它包含特权信息。 – user3416016

+0

我猜测你的第6行可能会因你所切入的内容不同而不同 - 例如不同的商店首先返回(?),并且在它未正确关闭之前返回。 –

+0

第6行就是这条线。 $ XML =使用simplexml_load_file($ apiLink); – user3416016

相关问题