【发布时间】:2015-04-12 17:02:00
【问题描述】:
我正在尝试连接到 UPS API 以检索信息,但无法正常工作。我正在使用此代码:
//die(phpinfo());
//Configuration
$access = "ACCESS NUMBER";
$userid = "USERNAME";
$passwd = "PASSWORD";
$wsdl = "wsdl/Track.wsdl"; //wsdl path
$operation = "ProcessTrack";
$endpointurl = 'https://wwwcie.ups.com/webservices/Track'; //https://onlinetools.ups.com/ups.app/xml/Track or https://wwwcie.ups.com/webservices/Track
$outputFileName = "XOLTResult.xml";
function processTrack()
{
//create soap request
$req['RequestOption'] = '15';
$tref['CustomerContext'] = 'Add description here';
$req['TransactionReference'] = $tref;
$request['Request'] = $req;
$request['InquiryNumber'] = TRACKING NUMBER;
$request['TrackingOption'] = '02';
//echo "Request.......\n";
print_r($request);
//echo "\n\n";
return $request;
}
try
{
$mode = array
(
'soap_version' => 'SOAP_1_1', // use soap 1.1 client
'trace' => 1
);
// initialize soap client
$client = new SoapClient($wsdl , $mode);
//set endpoint url
$client->__setLocation($endpointurl);
//create soap header
$usernameToken['Username'] = $userid;
$usernameToken['Password'] = $passwd;
$serviceAccessLicense['AccessLicenseNumber'] = $access;
$upss['UsernameToken'] = $usernameToken;
$upss['ServiceAccessToken'] = $serviceAccessLicense;
$header = new SoapHeader('http://www.ups.com/XMLSchema/XOLTWS/UPSS/v1.0','UPSSecurity',$upss);
$client->__setSoapHeaders($header);
//get response
$resp = $client->__soapCall($operation ,array(processTrack()));
//get status
//echo "Response Status: " . $resp->Response->ResponseStatus->Description ."\n";
//save soap request and response to file
$fw = fopen($outputFileName , 'w');
fwrite($fw , "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n" . $client->__getLastResponse() . "\n");
fclose($fw);
//echo "<br> Request:<br>";
//print_r("<?xml version=\"1.0\" encoding=\"UTF-8\" \n" . $client->__getLastResponse() . "\n");
header('Content-type: text/xml');
echo $client->__getLastResponse();
}
catch(Exception $ex)
{
print_r($ex);
/*
header('Content-type: text/xml');
echo "<xml>Error</xml>";
*/
}
我得到的错误是:
Array ( [Request] => Array ( [RequestOption] => 15 [TransactionReference] => Array ( [CustomerContext] => Add description here ) ) [InquiryNumber] => [TRACKING NUMBER] [TrackingOption] => 02 ) SoapFault Object ( [message:protected] => Could not connect to host [string:Exception:private] => [code:protected] => 0 [file:protected] => /Applications/XAMPP/xamppfiles/htdocs/kestrak/php/ups.php [line:protected] => 58 [trace:Exception:private] => Array ( [0] => Array ( [function] => __doRequest [class] => SoapClient [type] => -> [args] => Array ( [0] => [CREDENTIALS]Add description here[TRACKING NUMBER] [1] => https://wwwcie.ups.com/webservices/Track [2] => http://onlinetools.ups.com/webservices/TrackBinding/v2.0 [3] => 1 [4] => 0 ) ) [1] => Array ( [file] => /Applications/XAMPP/xamppfiles/[DIRECTORY] [line] => 58 [function] => __soapCall [class] => SoapClient [type] => -> [args] => Array ( [0] => ProcessTrack [1] => Array ( [0] => Array ( [Request] => Array ( [RequestOption] => 15 [TransactionReference] => Array ( [CustomerContext] => Add description here ) ) [InquiryNumber] => [TRACKING NUMBER] [TrackingOption] => 02 ) ) ) ) ) [previous:Exception:private] => [faultstring] => Could not connect to host [faultcode] => HTTP )
我刚刚从网站下载了 UPS API。在我的 phpinfo() 上,soap 已启用。奇怪的是,当我使用 c9.io 时,它工作得非常好,但目前在我的本地主机上,我无法让它工作。 所以我最后得到的应该是一个xml文件,它输出使用跟踪号找到的结果。
【问题讨论】: