سؤال

I have XML document captured from a link and anytime I use simplexml_load_file(), it gives me an error saying *Warning: simplexml_load_file(): https://kga-dev.mirakl.net/api/shops?:1: parser error : Start tag expected, '<' not found in C:\wamp\www\merchants\get_merchants.php on line 6*

This is a part of my xml file

<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>

And this is my simple php code

<?php

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

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

    //echo $xml->shop_id;

?>
هل كانت مفيدة؟

المحلول

The solution for this problem is, that the API serves a JSON encoded string instead of an XML string. So the delivered content can not be parsed with SimpleXML. Try to get it with the following example

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

In this case $data should be an object or array with all the data the api delivers.

نصائح أخرى

Are you sure that this is the same XML as returned to the script? Note that you are not calling :1 - I cannot check as it requires authorization, yet in the example given by you there is no error up to line 7 so it would mean that possibly something else is returned as the code below works:

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

?>

Is not the authorization the problem your end? I need to authorize to your webservice: you handle that in a different way for your IP or something like that?

Your Code

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

use single quotes

<?php
  echo "<order_messages_response_delay xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true'/>";
?>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top