制作SOAP请求

时间:2014-12-31 10:38:09

标签: php jquery ajax soap

我是SOAP和XML的新手。我已经通过了几个教程,但似乎没有什么对我有用。 下面是我到目前为止编写的代码,但仍然无法得到预期的结果。

$soapclient = new SoapClient('http://www.carrierrate.com/RateQuoteService/service.asmx?WSDL');
$params = array('quoteInfo' => '', 'quoteItems' => '');
$response = $soapclient->RateMyShipment($params);
var_dump($response);

其实我正在努力寻找出货率。它说"用户凭证无效"。看起来我可以将用户凭据作为

传递
$params = array('ShipperUserNameWithCarrierRate' => 'myUsername', 'ShipperPasswordWithCarrierRate' => 'myPassword');

但我没有得到如何传递一个完整的" QuoteInfo"包括ShipmentRequestDate,ShipmentOriginZip ..

请让我知道我缺少的是什么,或者有一种简单的方法来发送此处提到的完整SOAP请求http://www.carrierrate.com/RateQuoteService/service.asmx?op=RateMyShipment通过jQuery / Ajax

我可以使用PHP或jQuery。

你的帮助会非常受欢迎!

5 个答案:

答案 0 :(得分:2)

基本上,quoteInfoquoteItems本身都应该是数组,因此$params成为由2个数组组成的数组。 尝试

$quoteInfo = array('ShipperUserNameWithCarrierRate' => 'myUsername',
                   'ShipperPasswordWithCarrierRate' => 'myPassword',
                   ...);

$quoteItem1 = array('ItemWeight' => ...,
                   'ItemHeight' => ...,
                    ...);
$quoteItems = array('QuoteItemsInfo' => array($quoteItem1, ...));

然后

 $params = array('quoteInfo' => $quoteInfo, 'quoteItems' => $quoteItems);

每个数组的可能键/值对在您链接的SOAP XML desription中定义。

答案 1 :(得分:1)

首先检查http://www.carrierrate.com/RateQuoteService/service.asmx有关功能的文档,这是你想要的; http://www.carrierrate.com/RateQuoteService/service.asmx?op=RateMyShipment

所以基本上你需要的是;

  $params = array('ShipperUserNameWithCarrierRate'=>$username,
 'ShipperPasswordWithCarrierRate'=>$password,
 'OtherParameters' => $parameter);

答案 2 :(得分:1)

我感觉很慷慨,所以我会为此提供坚实的解决方案。我一直在搞乱CarrierRate API至少一年,我已经成功为它创建了一个类

http://pastie.org/10245578

要使用此类,您当然必须在脚本中要求它,然后添加实际上重要的缺失参数。

require('quote.php');
$quoteInfo['ShipmentOriginZip'] = 28906;
$quoteInfo['ShipmentDestinationZip'] = 30301;
$quoteInfo['ShipmentRequestDate'] = date('Y-m-d', strtotime('+2 months'));

$quoteItems['FAKClass'] = 55;
$quoteItems['ItemWeight'] = 10000;
$quoteItems['ItemHeight'] = 60;
$quoteItems['ItemLength'] = 60;
$quoteItems['ItemWidth'] = 60;
$quoteItems['Pieces'] = 60;
$quoteItems['PalletCount'] = 60;

$carrierRate = new CarrierRate($quoteInfo, $quoteItems);
$carrierRate->execQuote();

if($carrierRate->hasError == false) {
    echo "<h2>Business to Business Quotes</h2>";
    echo "<ul>";
    foreach($carrierRate->quote_responses['b'] as $k=>$q) {
        echo "<li>" . $q['carrierName'] . " @ " . $q['after_price'] . "</li>\n";
    }
    echo "</ul>";

    echo "<h2>Business to Residential Quotes</h2>";
    echo "<ul>";
    foreach($carrierRate->quote_responses['r'] as $k=>$q) {
        echo "<li>" . $q['carrierName'] . " @ " . $q['after_price'] . "</li>\n";
    }
    echo "</ul>";
} else {
    echo "<pre>", print_r($carrierRate->errors), "</pre>";

}

答案 3 :(得分:0)

$soapclient = new SoapClient('http://www.carrierrate.com/RateQuoteService/service.asmx?WSDL');

if ($soapclient)
{
   $params = array ('quoteInfo' =>
           array (
                   "ShipperUserNameWithCarrierRate" => "username",
                   "ShipperPasswordWithCarrierRate" => "password",
                   "ShipmentRequestDate"=> "2015-01-16",
                   "ShipmentOriginZip"=> "",
                   "ShipmentDestinationZip"=> "",
                   "ShipmentPickupService" => "None",
                   "ShipmentDropoffService" => "None",
                   "ShipmentExtraService"=> "None",
                   "IsTradeShow"=> "0",
                   "IsSortnSegregate" => "0",
                   "IsInsidePickup" => "0",
                   "IsExtremeLength"=> "0",
                   "ExtremeLength"=> "",
                   "ExtremeLengthBundleCount" => "",  
                   "isInsuranceRequired" => "0",
                   "isNewItem" => "0",
                   "RequiredInsuranceAmount"=> "",
                   "NonCommercialDeliveryType"=> "None",
                   "IsProtectFromFreeze" => "0"
           ), 'quoteItems' =>
           array (
                   "FAKClass" => "",
                   "ItemWeight" => "1",
                   "ItemHeight" => "30",
                   "ItemLength" => "30",
                   "ItemWidth" => "20",
                   "PackageTypeName" => "",
                   "PackageTypeId" => "",
                   "ProductId" => "",
                   "ItemNmfc" => "",
                   "Hazardous" => "1",
                   "Pieces" => "",
                   "PieceDescription" => "",
                   "PalletCount" => ""
   ));

   $response = $soapclient->__soapCall("RateMyShipment", array ($params));
   var_dump($response);
}

答案 4 :(得分:0)

您是否已与服务提供商团队核实过?看起来你可能会遗漏一些强制性参数,因此你得到空响应,如果你的请求对象有一些问题你会得到一些错误代码作为响应。与编写网络服务的人联系,他们是指导你的合适人选。