通过post使用php发送XML
我知道有很多类似的问题,但我试着把所有的解决方案都搞乱了,似乎都没办法解决。我正在尝试将xml直接发布到web服务并获得响应。从技术上讲,我正在尝试连接到freightquote.com,您可以在页面右上角的文档下找到该文档。我之所以提到这一点,是因为我在他们的xml中看到了很多术语SOAP,这可能会有所不同。无论如何,我想要的是能够将xml发送到某个url并获得响应 所以如果我有以下几点通过post使用php发送XML,php,xml,soap,Php,Xml,Soap,我知道有很多类似的问题,但我试着把所有的解决方案都搞乱了,似乎都没办法解决。我正在尝试将xml直接发布到web服务并获得响应。从技术上讲,我正在尝试连接到freightquote.com,您可以在页面右上角的文档下找到该文档。我之所以提到这一点,是因为我在他们的xml中看到了很多术语SOAP,这可能会有所不同。无论如何,我想要的是能够将xml发送到某个url并获得响应 所以如果我有以下几点 $xml = "<?xml version='1.0' encoding='utf-8'?>
$xml = "<?xml version='1.0' encoding='utf-8'?>
<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<soap:Body>
<GetRatingEngineQuote xmlns='http://tempuri.org/'>
<request>
<CustomerId>0</CustomerId> <!-- Identifier for customer provided by Freightquote -->
<QuoteType>B2B</QuoteType> <!-- B2B / eBay /Freightview -->
<ServiceType>LTL</ServiceType> <!-- LTL / Truckload / Groupage / Haulage / Al -->
<QuoteShipment>
<IsBlind>false</IsBlind>
<PickupDate>2010-09-13T00:00:00</PickupDate>
<SortAndSegregate>false</SortAndSegregate>
<ShipmentLocations>
<Location>
<LocationType>Origin</LocationType>
<RequiresArrivalNotification>false</RequiresArrivalNotification>
<HasDeliveryAppointment>false</HasDeliveryAppointment>
<IsLimitedAccess>false</IsLimitedAccess>
<HasLoadingDock>false</HasLoadingDock>
<IsConstructionSite>false</IsConstructionSite>
<RequiresInsideDelivery>false</RequiresInsideDelivery>
<IsTradeShow>false</IsTradeShow>
<IsResidential>false</IsResidential>
<RequiresLiftgate>false</RequiresLiftgate>
<LocationAddress>
<PostalCode>30303</PostalCode>
<CountryCode>US</CountryCode>
</LocationAddress>
<AdditionalServices />
</Location>
<Location>
<LocationType>Destination</LocationType>
<RequiresArrivalNotification>false</RequiresArrivalNotification>
<HasDeliveryAppointment>false</HasDeliveryAppointment>
<IsLimitedAccess>false</IsLimitedAccess>
<HasLoadingDock>false</HasLoadingDock>
<IsConstructionSite>false</IsConstructionSite>
<RequiresInsideDelivery>false</RequiresInsideDelivery>
<IsTradeShow>false</IsTradeShow>
<IsResidential>false</IsResidential>
<RequiresLiftgate>false</RequiresLiftgate>
<LocationAddress>
<PostalCode>60606</PostalCode>
<CountryCode>US</CountryCode>
</LocationAddress>
<AdditionalServices />
</Location>
</ShipmentLocations>
<ShipmentProducts>
<Product>
<Class>55</Class>
<Weight>1200</Weight>
<Length>0</Length>
<Width>0</Width>
<Height>0</Height>
<ProductDescription>Books</ProductDescription>
<PackageType>Pallets_48x48</PackageType>
<IsStackable>false</IsStackable>
<DeclaredValue>0</DeclaredValue>
<CommodityType>GeneralMerchandise</CommodityType>
<ContentType>NewCommercialGoods</ContentType>
<IsHazardousMaterial>false</IsHazardousMaterial>
<PieceCount>5</PieceCount>
<ItemNumber>0</ItemNumber>
</Product>
</ShipmentProducts>
<ShipmentContacts />
</QuoteShipment>
</request>
<user>
<Name>someone@something.com</Name>
<Password>password</Password>
</user>
</GetRatingEngineQuote>
</soap:Body>
</soap:Envelope>";
首先,若你们的代码是这样写的,我怀疑这会不会是引用的原因。。。 您应该在xml周围使用双引号:
$my_xml = "<?xml version='1.0' standalone='yes'?>
<user>
<Name>xmltest@freightquote.com</Name>
<Password>XML</Password>
</user>";
$my_xml=”
xmltest@freightquote.com
XML
";
此外,您还可以使用firefox插件(chrome上可能有类似的插件)来帮助您处理请求,尤其是在您使用Web服务时。这样,您将能够看到错误是服务器端还是客户端
这将有助于您进行调试。如果您正在了解SOAP服务,我强烈建议您学习一次基础知识,然后反复使用这个伟大的工具。有许多功能您可以直接使用,或者您将重新发明轮子,努力生成xml文件、解析xml文件、故障等。使用准备好的工具,您的生活将更轻松,代码也会更好(bug更少) 看看如何使用SOAP Web服务。这并不难理解 下面是一些代码,可以帮助您分析webserivce。然后将类型映射到类,只发送和接收php对象。您可以寻找一些工具来自动生成类() 回应
HTTP/1.1 200 OK
Date: Mon, 22 Jul 2013 21:46:06 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Content-Length: 660
Set-Cookie: BIGipServerb2b_freightquote_com=570501130.20480.0000; path=/
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetTrackingInformationResponse xmlns="http://tempuri.org/">
<GetTrackingInformationResult>
<BOLNumber>0</BOLNumber>
<EstimatedDelivery>0001-01-01T00:00:00</EstimatedDelivery>
<TrackingLogs />
<ValidationErrors>
<B2BError>
<ErrorType>Validation</ErrorType>
<ErrorMessage>Unable to find shipment with BOL 67635735.</ErrorMessage>
</B2BError>
</ValidationErrors>
</GetTrackingInformationResult>
</GetTrackingInformationResponse>
</soap:Body>
</soap:Envelope>
HTTP/1.1200正常
日期:2013年7月22日星期一格林尼治标准时间21:46:06
服务器:Microsoft IIS/6.0
X-Powered-By:ASP.NET
X-AspNet-Version:2.0.50727
缓存控制:专用,最大年龄=0
内容类型:text/xml;字符集=utf-8
内容长度:660
设置Cookie:BIGipServerb2b_freightquote_com=570501130.20480.0000;路径=/
0
0001-01-01T00:00:00
验证
找不到BOL为67635735的装运。
我使用此命令行脚本测试SOAP调用:
#!/usr/bin/php
<?php
//file client-test.php
$xml_data = file_get_contents('php://stdin');
$ch = curl_init('http://example.com/server/');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('SOAPAction', 'MySoapAction'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
print_r($output);
#/usr/bin/php
您可以使用stream\u context\u create
和file\u get\u contents
在post中发送xml
$xml = "<your_xml_string>";
$send_context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-Type: application/xml',
'content' => $xml
)
));
print file_get_contents($url, false, $send_context);
$xml=”“;
$send\u context=stream\u context\u create(数组(
“http'=>数组(
'方法'=>'发布',
'标题'=>'内容类型:应用程序/xml',
“内容”=>$xml
)
));
打印文件内容($url,false,$send\u context);
通过post发送此XML的php代码是什么样子的?我对其进行了编辑以添加itok…那么哪个部分不起作用?您是否收到任何特定的错误消息?没有错误。。。只有白色屏幕,没有输出。我在codeigniter中这样做,除非出现灾难性的错误,否则我至少应该在没有任何输出的情况下获取html,因此出现了灾难性的错误,所以我假设你说我的curl是100%正确的?我已经编辑了我的文章,以包含我的实际xml。。。。大家的共识似乎是我的curl是对的,但我的xml是错的?也许你有共识,但不是更好的方法。:-)我希望我能给它50票,因为它是一个如此惊人的答案
POST /WebService/QuoteService.asmx HTTP/1.1
Host: b2b.freightquote.com
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.4.17
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://tempuri.org/GetTrackingInformation"
Content-Length: 324
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/">
<SOAP-ENV:Body>
<ns1:GetTrackingInformation>
<ns1:request>
<ns1:BOLNumber>67635735</ns1:BOLNumber>
</ns1:request>
</ns1:GetTrackingInformation>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
HTTP/1.1 200 OK
Date: Mon, 22 Jul 2013 21:46:06 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Content-Length: 660
Set-Cookie: BIGipServerb2b_freightquote_com=570501130.20480.0000; path=/
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetTrackingInformationResponse xmlns="http://tempuri.org/">
<GetTrackingInformationResult>
<BOLNumber>0</BOLNumber>
<EstimatedDelivery>0001-01-01T00:00:00</EstimatedDelivery>
<TrackingLogs />
<ValidationErrors>
<B2BError>
<ErrorType>Validation</ErrorType>
<ErrorMessage>Unable to find shipment with BOL 67635735.</ErrorMessage>
</B2BError>
</ValidationErrors>
</GetTrackingInformationResult>
</GetTrackingInformationResponse>
</soap:Body>
</soap:Envelope>
#!/usr/bin/php
<?php
//file client-test.php
$xml_data = file_get_contents('php://stdin');
$ch = curl_init('http://example.com/server/');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('SOAPAction', 'MySoapAction'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
print_r($output);
$xml = "<your_xml_string>";
$send_context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-Type: application/xml',
'content' => $xml
)
));
print file_get_contents($url, false, $send_context);