Reputation: 673
I do a Soap request but I obtain an string imposible to convert to XML, what is the problem?
This is what I do:
$url = "https://test.com/services";
$XML ='<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Consult Localitation xmlns="Services/">
<XMLin>
<ConsultXMLin Language="1" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Consult>
<Code>XXXXX0700005020128012D</Code>
</Consult>
</ConsultXMLin></XMLin>
</Consult Localitation></soap:Body>
</soap:Envelope>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_FORBID_REUSE, TRUE);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array( 'Content-Type: text/xml; charset=utf-8','Content-Length: '.strlen($XML),'SOAPAction: Services'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $XML);
$postResult = curl_exec($ch);
$test= simplexml_load_string($postResult);
print_r($test); // I obtain nothing.
I obtain this string from the curl response:
string(1128) "<?xml version="1.0" encoding="Windows-1252"?><ConsultaXMLout xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Respuestas><DatosIdiomas><DatosEnvios><Datos Idioma="1" Codigo="XXXXX0700005020128012D" Evento="1" web_id="Sin web_id"><Estado>Información sobre su envío no disponible. Compruebe si es correcto.</Estado><Descripcion>La información sobre su envío todavía no está disponible. Por favor, realice su consulta transcurridos unos días.</Descripcion><Fecha /></Datos></DatosEnvios></DatosIdiomas></Respuestas></ConsultaXMLout>"
Thank you in advance!
Upvotes: 0
Views: 558
Reputation: 673
Finally I found the solution.
// String to extract string from.
string(1128) "<?xml version="1.0" encoding="Windows-1252"?><ConsultaXMLout xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Respuestas><DatosIdiomas><DatosEnvios><Datos Idioma="1" Codigo="XXXXX0700005020128012D" Evento="1" web_id="Sin web_id"><Estado>Información sobre su envío no disponible. Compruebe si es correcto.</Estado><Descripcion>La información sobre su envío todavía no está disponible. Por favor, realice su consulta transcurridos unos días.</Descripcion><Fecha /></Datos></DatosEnvios></DatosIdiomas></Respuestas></ConsultaXMLout>";
I used htmlspecialchars of the response, then I obtained the full code.
// Call the function.
echo extractString($string, '<XmlIn>', '</Xmlin>');
// Here I taked all the code inside these two tags with the function extractString.
// Function that returns the string between two strings.
function extractString($string, $start, $end) {
$string = " ".$string;
$ini = strpos($string, $start);
if ($ini == 0) return "";
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
And this is the function that gets the content between two tags.
I hope that it will be usefull ;)
Upvotes: 0
Reputation: 17051
The problem is that print_r
doesn't do well at displaying the output of simplexml
parsing. Full credit to @Josh Davis and @hakre, who discuss this at this answer. Try
print_r($test->xpath("//Estado"))
and you should get the contents of the <Estado>
tag. See examples from the PHP manual for more ways to retrieve the content.
Upvotes: 1
Reputation: 198119
You're doing a somewhat common (but easy to prevent) mistake in the code: The XML is created "by hand" as writing a string. Even thought this is possible, this is also very error prone.
The XML you provide in your question is with many errors and neither well-formed nor valid. If you want to learn more about these two terms, please see Is there a difference between 'valid xml' and 'well formed xml'? (Sep 2008).
This shows the errors your string produces when loaded into a DOMDocument or a SimpleXMLElement:
#001 <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
#002 xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
#003 <soap:Body>
#004 <Consult Localitation xmlns="Services/">
[FATAL] ^- (41) Specification mandate value for attribute Localitation (4:41)
[FATAL] ^- (65) attributes construct error (4:41)
[FATAL] ^- (73) Couldn't find end of Start Tag Consult line 4 (4:41)
#005 <XMLin>
#006 <ConsultXMLin Language="1" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
#007 <Consult>
#008 <Code>XXXXX0700005020128012D</Code>
#009 </Consult>
#010 </ConsultXMLin></XMLin>
#011 </Consult Localitation></soap:Body>
[FATAL] ^ ^- (76) Opening and ending tag mismatch: Envelope line 1 and Body (11:55)
[FATAL] ^- (73) expected '>' (11:30)
[FATAL] ^- (76) Opening and ending tag mismatch: Body line 3 and Consult (11:30)
#012 </soap:Envelope>
[FATAL] ^- (5) Extra content at the end of the document (12:21)
Instead of creating the SOAP XML via a string, you can make use of existing libraries like SimpleXML or - as this is Soap related - SoapClient.
Upvotes: 1