我需要向 Estes 发送一个 SOAP 请求来检索费率报价。我在执行此操作时遇到问题,因为我使用过的其他 API 要么发布 XML 要么使用 URL 字符串。这对我来说有点不同。
我相信我的问题是我无法弄清楚需要为请求发送的数组。
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:rat="http://ws.estesexpress.com/ratequote" xmlns:rat1="http://ws.estesexpress.com/schema/2012/12/ratequote">
<soapenv:Header>
<rat:auth>
<rat:user>XXXXX</rat:user>
<rat:password>XXXX</rat:password>
</rat:auth>
</soapenv:Header>
<soapenv:Body>
<rat1:rateRequest>
<rat1:requestID>XXXXXX</rat1:requestID>
<rat1:account>XXXXXXX</rat1:account>
<rat1:originPoint>
<rat1:countryCode>XX</rat1:countryCode>
<rat1:postalCode>XXXXX</rat1:postalCode>
<rat1:city>XXXXXX</rat1:city>
<rat1:stateProvince>XX</rat1:stateProvince>
</rat1:originPoint>
<rat1:destinationPoint>
<rat1:countryCode>XX</rat1:countryCode>
<rat1:postalCode>XXXXX</rat1:postalCode>
</rat1:destinationPoint>
<rat1:payor>X</rat1:payor>
<rat1:terms>XX</rat1:terms>
<rat1:stackable>X</rat1:stackable>
<rat1:baseCommodities>
<rat1:commodity>
<rat1:class>X</rat1:class>
<rat1:weight>XXX</rat1:weight>
</rat1:commodity>
</rat1:baseCommodities>
</rat1:rateRequest>
</soapenv:Body>
</soapenv:Envelope>
这是我之前使用的代码,它不起作用。
<?php
$client = new SoapClient("https://www.estes-express.com/rating/ratequote/services/RateQuoteService?wsdl");
$request_object = array(
"header"=>array(
"auth"=>array(
"user"=>"XXXXX",
"password"=>"XXXXX",
)
),
"rateRequest"=>array(
"requestID"=>"XXXXXXXXXXXXXXX",
"account"=>"XXXXXX",
),
"originPoint"=>array(
"countryCode"=>"XX",
"postalCode"=>"XXXXX",
"city"=>"XXXXX",
"stateProvince"=>"XX",
),
"destinationPoint"=>array(
"countryCode"=>"XX",
"postalCode"=>"XXXXX",
),
"payor"=> "X",
"terms"=> "XXXX",
"stackable"=> "X",
"baseCommodities"=>array(
"commodity"=>array(
"class"=>"XX",
"weight"=>"XXXX",
)
),
);
$result = $client->rateRequest(array("request"=>$request_object));
var_dump($result);
?>
这是错误
Fatal error: Uncaught SoapFault exception: [Client] Function ("rateRequest") is not a valid method for this service in /home/content/54/11307354/html/test/new/estes.php:36
Stack trace: #0 /home/content/54/11307354/html/test/new/estes.php(36): SoapClient->__call('rateRequest', Array) #1 /home/content/54/11307354/html/test/new/estes.php(36):
SoapClient->rateRequest(Array) #2 {main} thrown in /home/content/54/11307354/html/test/new/estes.php on line 36
这是我传递给 Estes API 的$params数组
$params = array(
"requestID" => "xxxxxxxx",
"account" => "xxxxxxxx",
"originPoint" => array('countryCode' => 'US', 'postalCode' => $fromzip),
"destinationPoint" => array('countryCode' => 'US', 'postalCode' => $shipzip),
"payor" => 'T',
"terms" => 'PPD',
"stackable" => 'N',
"baseCommodities" => array('commodity' => $comArray ),
"accessorials" => array('accessorialCode' => $accArray)
如果没有附件,则需要从$params数组
中删除该数组 ); 如果没有附件代码,则删除附件条目 if(sizeof($accArray) == 0){ $params = array_slice($params, 0, 8);删除附件条目 }
我从类数组和权重构建商品数组的方式
array:
// load the $params commodities array
$comArray = array();
for ($i=0; $i<count($class_tbl); $i++) {
$comArray[] = array('class'=>$class_tbl[$i], 'weight'=>$weight_tbl[$i]);
}
以下代码设置附件数组的格式
if($inside == "Yes") {
$accArray[$i] = "INS";
++$i;
}
if($liftgate == "Yes") {
$accArray[$i] = "LGATE";
++$i;
}
if($call == "Yes") {
$accArray[$i] = "NCM";
++$i;
}
如果交付到企业以外的其他部门(例如学校、教堂等),则还应将 Estes 目标附件代码添加到 $accArray 数组中。
看看我们的肥皂调用是否有任何帮助。我们正在像这样进行肥皂调用:
// define transaction arrays
$url = "http://www.estes-express.com/rating/ratequote/services/RateQuoteService?wsdl";
$username = 'xxxxxxxx';
$password = 'xxxxxxxx';
// setting a connection timeout of five seconds
$client = new SoapClient($url, array("trace" => true,
"exceptions" => true,
"connection_timeout" => 5,
"features" => SOAP_WAIT_ONE_WAY_CALLS,
"cache_wsdl" => WSDL_CACHE_NONE));
$old = ini_get('default_socket_timeout');
ini_set('default_socket_timeout', 5);
//Prepare SoapHeader parameters
$cred = array(
'user' => $username,
'password' => $password
);
$header = new SoapHeader('http://ws.estesexpress.com/ratequote', 'auth', $cred);
$client->__setSoapHeaders($header);
$params = array(
"requestID" => "xxxxxxxx",
"account" => "xxxxxxxx",
"originPoint" => array('countryCode' => 'US', 'postalCode' => $fromzip),
"destinationPoint" => array('countryCode' => 'US', 'postalCode' => $shipzip),
"payor" => 'T',
"terms" => 'PPD',
"stackable" => 'N',
"baseCommodities" => array('commodity' => $comArray ),
"accessorials" => array('accessorialCode' => $accArray)
);
// remove accessorials entry if no accessorial codes
if(count($accArray) == 0){
$params = array_slice($params, 0, 8); // remove accesorials entry
}
// call Estes API and catch any errors
try {
$reply = $client->getQuote($params);
}
catch(SoapFault $e){
// handle issues returned by the web service
//echo "Estes soap fault<br>" . $e . "<br>";
$edit_error_msg = "Estes quote API timed out or failed to return a quote";
return "0.00";
}
catch(Exception $e){
// handle PHP issues with the request
//echo "PHP soap exception<br>" . $e . "<br>";
$edit_error_msg = "Estes quote API timed out or failed to return a quote";
return "0.00";
}
unset($client);
ini_set('default_socket_timeout', $old);
// print_r($reply);
使用验证器查找 WSDL,看起来可用的两种方法是 echo
和 getQuote
。
看看 WSDL 本身,你也可以看到这一点:
<wsdl:operation name="getQuote">
<wsdl:input name="rateRequest" message="tns:rateRequestMsg"></wsdl:input>
<wsdl:output name="quoteInfo" message="tns:rateQuoteMsg"></wsdl:output>
<wsdl:fault name="schemaErrorMessage" message="tns:schemaErrorMsg"></wsdl:fault>
<wsdl:fault name="generalErrorMessage" message="tns:generalErrorMsg"></wsdl:fault>
</wsdl:operation>
尝试拨打getQuote
而不是rateRequest
。
$result = $client->__soapCall('getQuote', array("request"=>$request_object));