PHP:UPS API跟踪

时间:2015-03-28 02:13:00

标签: php api ups

使用UPS api跟踪软件包时遇到了很多麻烦。我不明白它是如何工作的,我从UPS示例中下载了这些文件:

$wsdl = " Add Wsdl File Here ";
$endpointurl = ' Add URL Here';

我没有任何wsdl文件,当我下载UPS跟踪示例时,没有任何文件。我也不知道要为终点url放什么。 UPS没有解释,或者至少我找不到他们的位置。

所以我决定试试这个:

https://github.com/gabrielbull/php-ups-api#installation

但我不知道如何使用它。我已经下载了文件,但我不知道我可以把测试文件放在哪里:

$tracking = new Ups\Tracking($accessKey, $userId, $password);

try {
    $shipment = $tracking->track('TRACKING NUMBER');

    foreach($shipment->Package->Activity as $activity) {
        var_dump($activity);
    }

} catch (Exception $e) {
    var_dump($e);
}

我不知道在哪里提供API Access密钥,用户名和密码的凭据。我现在很困惑,我无法弄明白。我很抱歉我的英语不好。谢谢。

以下是UPS的完整代码:

  //Configuration
  $access = "XXX";
  $userid = "XXX";
  $passwd = "XXX";
  $wsdl = " Add Wsdl File Here ";
  $operation = "ProcessTrack";
  $endpointurl = ' Add URL Here';
  $outputFileName = "XOLTResult.xml";

  function processTrack()
  {
      //create soap request
    $req['RequestOption'] = '15';
    $tref['CustomerContext'] = 'Add description here';
    $req['TransactionReference'] = $tref;
    $request['Request'] = $req;
    $request['InquiryNumber'] = '';
    $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 , "Request: \n" . $client->__getLastRequest() . "\n");
    fwrite($fw , "Response: \n" . $client->__getLastResponse() . "\n");
    fclose($fw);

  }
  catch(Exception $ex)
  {
    print_r ($ex);
  }

1 个答案:

答案 0 :(得分:0)

跟踪WSDL位于Tracking> TrackingPackage> TrackingWebServices> Schemas-WSDLs

您需要在$ access,$ userid,$ passwd中替换您的凭据,并将端点设置为UPS测试服务器。

  $access = "XXX";
  $userid = "XXX";
  $passwd = "XXX";
  $wsdl = " Add Wsdl File Here ";
  $operation = "ProcessTrack";
  $endpointurl = ' Add URL Here';