我做了一个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>"
提前谢谢!
答案 0 :(得分:1)
你在代码中做了一个有点普遍(但容易预防)的错误:XML是“手动”创建的,就像写一个字符串一样。即使认为这是可能的,这也很容易出错。
您在问题中提供的XML包含许多错误,格式正确和无效。如果您想详细了解这两个术语,请参阅 Is there a difference between 'valid xml' and 'well formed xml'?(2008年9月)。
这会显示加载到DOMDocument或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或 - 因为这与Soap相关 - SoapClient。
答案 1 :(得分:1)
问题是print_r
在显示simplexml
解析的输出方面表现不佳。完全归功于@Josh Davis和@hakre,他们讨论了这个at this answer。尝试
print_r($test->xpath("//Estado"))
你应该得到<Estado>
标签的内容。有关检索内容的更多方法,请参阅examples from the PHP manual。
答案 2 :(得分:0)
最后我找到了解决方案。
// 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);
这是获取两个标签之间内容的功能。
我希望它会有用;)