Php Soap请求响应simplexml\u加载\u字符串无法正常工作
我做了一个Soap请求,但是我得到了一个无法转换为XML的字符串,问题是什么 我就是这么做的:Php Soap请求响应simplexml\u加载\u字符串无法正常工作,php,xml,soap,Php,Xml,Soap,我做了一个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:soa
$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.
$url=”https://test.com/services";
$XML='0
ConsultXMLin Language=“1”xmlns:xsd=”http://www.w3.org/2001/XMLSchema“xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance"
咨询
代码XXXXX07000050028012D/代码
/咨询
/咨询公司
';
$ch=curl_init();
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_头,FALSE);
curl_setopt($ch,CURLOPT_禁止重用,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');
卷曲设置($ch,卷曲设置桩,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$XML);
$postResult=curl\u exec($ch);
$test=simplexml\u load\u字符串($postResult);
打印($test);//我一无所获。
我从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>"
string(1128)“信息不可争议。压缩是正确的。信息不可争议。赞成,真正的跨国界领事。”
提前谢谢你 您在代码中犯了一个比较常见(但很容易防止)的错误:XML是“手工”编写字符串的。即使这是可能的,这也是非常容易出错的 您在问题中提供的XML有很多错误,而且格式不正确,也无效。如果您想了解这两个术语的更多信息,请参阅(2008年9月) 这显示了字符串加载到或时产生的错误:
#001
#003
#004
[致命]^-(41)属性定位的规范指令值(4:41)
[致命]^-(65)属性构造错误(4:41)
[致命]^-(73)找不到开始标记的结尾,请参阅第4行(4:41)
#005
#006 ConsultXMLin Language=“1”xmlns:xsd=”http://www.w3.org/2001/XMLSchema“xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance"
#007咨询
#008代码XXXXX070000500128012D/代码
#009/咨询
#010/咨询xmlin
#011
[致命]^-(76)开头和结尾标记不匹配:信封行1和正文(11:55)
[致命]^-(73)预期“>”(11:30)
[致命]^-(76)开头和结尾标记不匹配:正文第3行和参考(11:30)
#012
[致命]^-(5)文件末尾的额外内容(12:21)
与通过字符串创建SOAP XML不同,您可以利用现有的库,如或,因为这与SOAP相关。问题在于
print\r
在显示simplexml
解析的输出方面做得不好。完全归功于@Josh Davis和@hakre,他们讨论了这一点。试一试
您应该获得
标记的内容。有关检索内容的更多方法,请参阅。我终于找到了解决方案
// 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>";
这是一个函数,用于获取两个标记之间的内容
我希望它会有用;) 您使用什么工具或库来解析XML?@cxw我在做simplexml\u load\u string($string);请给出一个调用curl和调用simplexml的代码示例。你可能在引用或其他一些问题上有问题,而这些问题并不明显。谢谢此
/
不是有效的XML。@devtreat感谢您发布代码。请尝试示例1(同时打印$error->column
)并发布结果,好吗?这可能有助于定位错误。我以为OP会问为什么postResult
没有解析。错误转储是什么?这是一个名称空间/模式问题吗?我只是在偷懒,因为我不在控制台:)。我认为您关于使用工具生成XML的观点是非常正确的。
// 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>";
// Call the function.
echo extractString($string, '<XmlIn>', '</Xmlin>');
// 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);