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'/>";
?>
Related
I need to make the structure of my XML SOAP request to look like following:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.fines.pl/api/contract">
<SOAP-ENV:Body>
<ns1:newApplicationRequest>
<user_login>XYZ</user_login>
<user_password>XYZ</user_password>
<contract>
<?xml version="1.0" encoding="UTF-8"?>
<sof:Contract xmlns:s="http://www.fines.pl/simple" xmlns:sof="http://www.fines.pl/sof" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.fines.pl/sof model.xsd ">
<product>
<prefix>MOP</prefix>
</product>
<participants>
<customers>
<main_borrower>
<personal_data>
<pesel>85050949761</pesel>
<firstname>Anna</firstname>
<lastname>Test</lastname>
<firstname_father></firstname_father>
<firstname_mother></firstname_mother>
<secondname />
<sex>female</sex>
</personal_data>
<contact_data>
<addresses>
<address>
<type>registered</type>
<street_name>Grunwaldzka</street_name>
<block_number>11</block_number>
<flat_number>5</flat_number>
<postal_code>80-100</postal_code>
<city>GdaĆsk</city>
</address>
</addresses>
<phones_mobile>
<phone_mobile>
<type>personal</type>
<number>602200300</number>
</phone_mobile>
</phones_mobile>
</contact_data>
<incomes>
<income>
<type>employment</type>
<main_income>true</main_income>
<fixed_term_contract>false</fixed_term_contract>
<paychecks>
<paycheck>
<amount_net>
<amount>1444.00</amount>
<currency>PLN</currency>
</amount_net>
<type>base</type>
</paycheck>
</paychecks>
</income>
</incomes>
<household_pointer>/households.0</household_pointer>
</main_borrower>
</customers>
</participants>
</sof:Contract></contract></ns1:newApplicationRequest></SOAP-ENV:Body></SOAP-ENV:Envelope>
I've set my $params variable to contain user_login and user_password elements with their respective values, but I have no idea how may I set this contract param with required content.
Providing $contract variable with XML code (which i thought may be a workaround) poop the Fatal error: Uncaught SoapFault exception: [xml_structure] String could not be parsed as XML error.
Would be grateful for explaining how could this be done.
You have declared <?xml?> twice , one on the first line and second on the eight. That's the only error I see in the xml side of things.
How do I read this soap response and echo the values of subid and points.
Here is the response
<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:zuku">
<SOAP-ENV:Body>
<ns1:showloyaltypointsResponse xmlns:ns1="http://tempuri.org">
<return xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:subpoints[1]">
<item xsi:type="tns:subpoints">
<subid xsi:type="xsd:integer">618341</subid>
<points xsi:type="xsd:string">0</points>
</item>
</return>
</ns1:showloyaltypointsResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
And here is my php code
$result = $client ->call('showloyaltypoints',array("subid" =>$subid,"username" =>"$username", "password" =>"$password"));
echo $client->response;
I found out the solution by converting it to an array first.
https://stackoverflow.com/a/42311833/7042343
I've searched in the SO, but nothing what i've found could help me.
I'm doing system integration with JadLog, a freight service.
When I pass the query with the product and delivery variables, an XML is returned.
Return example:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<valorarResponse xmlns="">
<ns1:valorarReturn xmlns:ns1="http://jadlogEdiws">
<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://www.jadlog.com.br/JadlogEdiWs/services">
<Jadlog_Valor_Frete>
<versao>1.0</versao>
<Retorno>1458,62</Retorno>
<Mensagem>Valor do Frete</Mensagem>
</Jadlog_Valor_Frete>
</string>
</ns1:valorarReturn>
</valorarResponse>
</soapenv:Body>
</soapenv:Envelope>
So, there is more than one XML declaration, right?
The only value that I need is the value of the freight, wich is in the tag Retorno. I this case, 1458,62.
What am I trying to do:
$your_xml_response = file_get_contents($url_project);
$clean_xml = str_replace('soapenv:', '', $your_xml_response);
$xml = simplexml_load_string($clean_xml);
var_dump($xml);
What its returns:
object(SimpleXMLElement)[1]
public 'Body' =>
object(SimpleXMLElement)[2]
public 'valorarResponse' =>
object(SimpleXMLElement)[3]
If I try to echo $xml->Retorno, it Returns empty.
How can I get the value of tag Retorno?
When I tried to load your XML string with simplexml_load_string it causes me
There is more than one XML declaration which is not allowed in xml . That's why I used regular express to get Retorno value. May be its not the best approach to use regex on XML but you can make a try.
<?php
$re = '/<Retorno>(.*?)<\/Retorno>/m';
$str = '<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<valorarResponse xmlns="">
<ns1:valorarReturn xmlns:ns1="http://jadlogEdiws">
<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://www.jadlog.com.br/JadlogEdiWs/services">
<Jadlog_Valor_Frete>
<versao>1.0</versao>
<Retorno>1458,62</Retorno>
<Mensagem>Valor do Frete</Mensagem>
</Jadlog_Valor_Frete>
</string>
</ns1:valorarReturn>
</valorarResponse>
</soapenv:Body>
</soapenv:Envelope>
';
preg_match_all($re, $str, $matches);
//debugging
print '<pre>';
print_r($matches);
print '<pre>';
//result
echo $matches[0][0];
SO,
I'm receiving the following SOAP response as a string:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetListItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<GetListItemsResult>
<listitems xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema">
<rs:data ItemCount="3">
<z:row ows_DocIcon="jpg" ows_LinkFilename="18380014229851.jpg" ows_Modified="2016-10-08 17:27:40" ows_Editor="179440;#asdf" ows_Last_x0020_Modified="2;#2016-10-08 17:29:29" ows_ID="2" ows_Created_x0020_Date="2;#2016-10-08 17:27:40" ows_FileLeafRef="2;#18380014229851.jpg" />
<z:row ows_DocIcon="jpg" ows_LinkFilename="18380014229851_2.jpg" ows_Modified="2016-10-08 17:27:40" ows_Editor="179440;#asfd" ows_Last_x0020_Modified="3;#2016-10-08 17:29:29" ows_ID="3" ows_Created_x0020_Date="3;#2016-10-08 17:27:41" ows_FileLeafRef="3;#18380014229851_2.jpg" />
<z:row ows_DocIcon="jpg" ows_LinkFilename="18380014229851_3.jpg" ows_Modified="2016-10-08 17:27:40" ows_Editor="179440;#asdf" ows_Last_x0020_Modified="4;#2016-10-08 17:30:03" ows_ID="4" ows_Created_x0020_Date="4;#2016-10-08 17:27:41" ows_FileLeafRef="4;#18380014229851_3.jpg" />
</rs:data>
</listitems>
</GetListItemsResult>
</GetListItemsResponse>
</soap:Body>
</soap:Envelope>
I am attempting to get each of the "z:row" entries, but am struggling due to the namespaces (after some googling that's what i'm understanding them to be called).
Here is the code I am using:
$xml = simplexml_load_string($sp->soapClient->__last_response);
foreach($xml->GetListItemsResult as $item)
{
$ns_li = $item->children('uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882');
$ns_rs = $ns_li->children('urn:schemas-microsoft-com:rowset');
$ns_z = $nr_rs->children('#RowsetSchema');
echo $ns_z->row;
}
Right now I am getting no output from echo statement. What am I doing wrong?
You can use XPath expression to get specific part of XML document using various criteria. For example, to get z:row elements anywhere in the XML document you can simply do //z:row, after registering the prefix z :
$xml->registerXPathNamespace("z", "#RowsetSchema");
foreach($xml->xpath('//z:row') as $item)
{
echo $item["ows_LinkFilename"] ."\n";
}
eval.in demo
output :
18380014229851.jpg
18380014229851_2.jpg
18380014229851_3.jpg
How can i get errorCode and errorMsg values? I went trough many example but still struggling.
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:doOperationsResponse xmlns:ns1="urn:bulkdeployer.easy.gintel.com" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<result href="#id0"/>
</ns1:doOperationsResponse>
<multiRef xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="urn:bulkdeployer.easy.gintel.com" id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns2:ResponseType">
<errorCode xsi:type="xsd:int">0</errorCode>
<errorMsg xsi:type="xsd:string">No errors</errorMsg>
</multiRef>
</soapenv:Body>
</soapenv:Envelope>
What I have tried:
$xml= simplexml_load_string($content);
$xml->registerXPathNamespace('nam','urn:bulkdeployer.easy.gintel.com'); // need to register namespace
$return_code = $xml->xpath('//nam:errorCode');
$return_msg = $xml->xpath('//nam:errorMsg');
Thanks