Soap 请求响应 simplexml_load_string 无法正常工作
Soap Request Response simplexml_load_string dont work properly
我发出了 Soap 请求,但我获得了一个无法转换为 XML 的字符串,这是什么问题?
我就是这样做的:
$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.
我从 curl 响应中获取此字符串:
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>"
提前致谢!
您在代码中犯了一个有点常见(但很容易避免)的错误:XML 是在 "by hand" 编写字符串时创建的。即使认为这是可能的,这也很容易出错。
您在问题中提供的 XML 有很多错误,既不 格式正确 也不 有效 。如果您想了解有关这两个术语的更多信息,请参阅 Is there a difference between 'valid xml' and 'well formed xml'?(2008 年 9 月).
这显示了您的字符串在加载到 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)
无需通过字符串创建 SOAP XML,您可以使用 SimpleXML or - as this is Soap related - SoapClient.
等现有库
问题是 print_r
不能很好地显示 simplexml
解析的输出。完全归功于讨论此问题的@Josh Davis 和@hakre at this answer。尝试
print_r($test->xpath("//Estado"))
你应该得到 <Estado>
标签的内容。有关检索内容的更多方法,请参阅 examples from the PHP manual。
我终于找到了解决办法。
// 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>";
我使用了响应的htmlspecialchars,然后我获得了完整的代码。
// Call the function.
echo extractString($string, '<XmlIn>', '</Xmlin>');
// 这里我用函数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);
这是获取两个标签之间内容的函数。
希望对你有用 ;)
我发出了 Soap 请求,但我获得了一个无法转换为 XML 的字符串,这是什么问题?
我就是这样做的:
$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.
我从 curl 响应中获取此字符串:
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>"
提前致谢!
您在代码中犯了一个有点常见(但很容易避免)的错误:XML 是在 "by hand" 编写字符串时创建的。即使认为这是可能的,这也很容易出错。
您在问题中提供的 XML 有很多错误,既不 格式正确 也不 有效 。如果您想了解有关这两个术语的更多信息,请参阅 Is there a difference between 'valid xml' and 'well formed xml'?(2008 年 9 月).
这显示了您的字符串在加载到 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)
无需通过字符串创建 SOAP XML,您可以使用 SimpleXML or - as this is Soap related - SoapClient.
等现有库问题是 print_r
不能很好地显示 simplexml
解析的输出。完全归功于讨论此问题的@Josh Davis 和@hakre at this answer。尝试
print_r($test->xpath("//Estado"))
你应该得到 <Estado>
标签的内容。有关检索内容的更多方法,请参阅 examples from the PHP manual。
我终于找到了解决办法。
// 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>";
我使用了响应的htmlspecialchars,然后我获得了完整的代码。
// Call the function.
echo extractString($string, '<XmlIn>', '</Xmlin>');
// 这里我用函数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);
这是获取两个标签之间内容的函数。
希望对你有用 ;)