从 cURL 请求到 Web 服务的 SOAP 解析
我目前有一个从 Web 服务解析数据的脚本,到目前为止我发现的所有示例都告诉我结果只是被解析为 XML,但如果结果实际上不是有效的 XML,这就有点困难。
我的 XML 请求发送到 Web 服务:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.bring.no/logistics/shippingguide/1.0">
<soapenv:Header/>
<soapenv:Body>
<ns:ShippingGuideRequest>
<ns:UserInformation>
<ns:Usercode>ae34ab27-2023-41f5-b800-013f0d0a6015</ns:Usercode>
</ns:UserInformation>
<ns:ProductIds>
<ns:ProductId>SERVICEPAKKE</ns:ProductId>
</ns:ProductIds>
<ns:Packages>
<ns:Package packageId="7,">
<ns:GrossWeight unitCode="GRM">500</ns:GrossWeight>
<ns:FromPostalCode>4045</ns:FromPostalCode>
<ns:ToPostalCode>4045</ns:ToPostalCode>
</ns:Package>
</ns:Packages>
</ns:ShippingGuideRequest>
</soapenv:Body>
</soapenv:Envelope>
我无法使用 PHP5 及更高版本附带的内置 SoapClient,因为我不赞成强迫我的用户进行此配置,因为我已经经历了许多测试用例没有。 我尝试过使用普通套接字和 cURL 来执行请求,但这并没有产生任何不同的结果。
我从通话中得到的结果如下;
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ns:ShippingGuideResponse xmlns:ns="http://www.bring.no/logistics/shippingguide/1.0"><ns:Packages><ns:Package packageId="7,"><ns:Product><ns:ProductId>SERVICEPAKKE</ns:ProductId><ns:GuiInformation><ns:SortOrder>11</ns:SortOrder><ns:MainDisplayCategory>Hent sendingen selv</ns:MainDisplayCategory><ns:SubDisplayCategory/><ns:DisplayName>På postkontor eller post i butikk</ns:DisplayName><ns:ProductName>Klimanøytral Servicepakke</ns:ProductName><ns:DescriptionText>Madla. Åpningstider Man - Fre: 1000-1800, Lør: 1000-1500</ns:DescriptionText><ns:HelpText>Sendingen er en Klimanøytral Servicepakke som blir levert til mottakers postkontor/ post i butikk. Mottaker kan velge å hente sendingen på et annet postkontor/post i butikk enn sitt lokale. Mottaker varsles om at sendingen er ankommet via SMS, e-post eller hentemelding i postkassen. Transporttid er normalt 1-3 virkedager, avhengig av strekning. Sendingen kan spores ved hjelp av sporingsnummeret.</ns:HelpText><ns:Tip/></ns:GuiInformation><ns:Price currencyIdentificationCode="NOK"><ns:PackagePriceWithoutAdditionalServices><ns:AmountWithoutVAT>66.00</ns:AmountWithoutVAT><ns:VAT>16.50</ns:VAT><ns:AmountWithVAT>82.50</ns:AmountWithVAT></ns:PackagePriceWithoutAdditionalServices></ns:Price><ns:ExpectedDelivery><ns:WorkingDays>1</ns:WorkingDays><ns:UserMessage/><ns:AlternativeDeliveryDates/></ns:ExpectedDelivery></ns:Product></ns:Package></ns:Packages><ns:TraceMessages><ns:Message>You are using an outdated schema version (1). Most recent schema version is 4.</ns:Message></ns:TraceMessages></ns:ShippingGuideResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
正如您所看到的,XML 请求缺少 XML 开始标记,因此 PHP 中的 SimpleXMLElement 和类似元素无法解释它。
最后,我用来发送请求的代码,希望它是一些微不足道的东西,比如我丢失或类似的标头。
$ch =curl_init("http://fraktguide.bring.no/fraktguide/ws/1.0/");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=utf-8', 'Content-Length: ' . strlen($xml) ));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_PORT, 80);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$结果=curl_exec($ch);
I currently have a script parsing data from a Web Service, all examples I've found so far tell me the result is just to be parsed as XML, but without the result actually being valid XML this is a bit difficulty.
My XML Request sent to the Web Service:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.bring.no/logistics/shippingguide/1.0">
<soapenv:Header/>
<soapenv:Body>
<ns:ShippingGuideRequest>
<ns:UserInformation>
<ns:Usercode>ae34ab27-2023-41f5-b800-013f0d0a6015</ns:Usercode>
</ns:UserInformation>
<ns:ProductIds>
<ns:ProductId>SERVICEPAKKE</ns:ProductId>
</ns:ProductIds>
<ns:Packages>
<ns:Package packageId="7,">
<ns:GrossWeight unitCode="GRM">500</ns:GrossWeight>
<ns:FromPostalCode>4045</ns:FromPostalCode>
<ns:ToPostalCode>4045</ns:ToPostalCode>
</ns:Package>
</ns:Packages>
</ns:ShippingGuideRequest>
</soapenv:Body>
</soapenv:Envelope>
I am NOT able to use the built in SoapClient that comes with PHP5 and above, as I do not approve of forcing my users to have this configured as I've experienced many of my test cases to not have.
I have tried doing the request using plain sockets as well as cURL, but this did not yield any different results.
The result I get from the call is as follows;
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ns:ShippingGuideResponse xmlns:ns="http://www.bring.no/logistics/shippingguide/1.0"><ns:Packages><ns:Package packageId="7,"><ns:Product><ns:ProductId>SERVICEPAKKE</ns:ProductId><ns:GuiInformation><ns:SortOrder>11</ns:SortOrder><ns:MainDisplayCategory>Hent sendingen selv</ns:MainDisplayCategory><ns:SubDisplayCategory/><ns:DisplayName>På postkontor eller post i butikk</ns:DisplayName><ns:ProductName>Klimanøytral Servicepakke</ns:ProductName><ns:DescriptionText>Madla. Åpningstider Man - Fre: 1000-1800, Lør: 1000-1500</ns:DescriptionText><ns:HelpText>Sendingen er en Klimanøytral Servicepakke som blir levert til mottakers postkontor/ post i butikk. Mottaker kan velge å hente sendingen på et annet postkontor/post i butikk enn sitt lokale. Mottaker varsles om at sendingen er ankommet via SMS, e-post eller hentemelding i postkassen. Transporttid er normalt 1-3 virkedager, avhengig av strekning. Sendingen kan spores ved hjelp av sporingsnummeret.</ns:HelpText><ns:Tip/></ns:GuiInformation><ns:Price currencyIdentificationCode="NOK"><ns:PackagePriceWithoutAdditionalServices><ns:AmountWithoutVAT>66.00</ns:AmountWithoutVAT><ns:VAT>16.50</ns:VAT><ns:AmountWithVAT>82.50</ns:AmountWithVAT></ns:PackagePriceWithoutAdditionalServices></ns:Price><ns:ExpectedDelivery><ns:WorkingDays>1</ns:WorkingDays><ns:UserMessage/><ns:AlternativeDeliveryDates/></ns:ExpectedDelivery></ns:Product></ns:Package></ns:Packages><ns:TraceMessages><ns:Message>You are using an outdated schema version (1). Most recent schema version is 4.</ns:Message></ns:TraceMessages></ns:ShippingGuideResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
As you can see the XML request is lacking the XML start tags, and as such SimpleXMLElement and similar in PHP is unable to interpret it.
Finally, the code I use to send my request, hopefully it's something trivial like a header I'm missing or similar.
$ch = curl_init("http://fraktguide.bring.no/fraktguide/ws/1.0/");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=utf-8', 'Content-Length: ' . strlen($xml) ));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_PORT, 80);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$result = curl_exec($ch);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
数据已获取,但在没有参考点的情况下读取数据似乎是一个问题。
解决方案是使用 XPath 遍历它。
The data was fetched, but reading it without a reference point seemed to be an issue.
The solution was traversing it using XPath.