Advertisement
Guest User

UPS API

a guest
Jun 13th, 2012
878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.05 KB | None | 0 0
  1. <?php
  2.   error_reporting(E_ALL);
  3.   ini_set('display_errors', '1');
  4.   //Configuration
  5.   $access = "mykey";
  6.   $userid = "myuserid";
  7.   $passwd = "mypassword";
  8.   $wsdl = "FreightRate.wsdl";
  9.   $operation = "ProcessFreightRate";
  10.   $endpointurl = 'https://wwwcie.ups.com/webservices/Ship';
  11.   $outputFileName = "XOLTResult.xml";
  12.  
  13.   function processFreightRate()
  14.   {
  15.       //create soap request
  16.       $option['RequestOption'] = 'RateChecking Option';
  17.       $request['Request'] = $option;
  18.       $shipfrom['Name'] = 'Good Incorporation';
  19.       $addressFrom['AddressLine'] = '2010 WARSAW ROAD';
  20.       $addressFrom['City'] = 'Roswell';
  21.       $addressFrom['StateProvinceCode'] = 'GA';
  22.       $addressFrom['PostalCode'] = '30076';
  23.       $addressFrom['CountryCode'] = 'US';
  24.       $shipfrom['Address'] = $addressFrom;
  25.       $request['ShipFrom'] = $shipfrom;
  26.  
  27.       $shipto['Name'] = 'Sony Company Incorporation';
  28.       $addressTo['AddressLine'] = '2311 YORK ROAD';
  29.       $addressTo['City'] = 'TIMONIUM';
  30.       $addressTo['StateProvinceCode'] = 'MD';
  31.       $addressTo['PostalCode'] = '21093';
  32.       $addressTo['CountryCode'] = 'US';
  33.       $shipto['Address'] = $addressTo;
  34.       $request['ShipTo'] = $shipto;
  35.  
  36.       $payer['Name'] = 'Payer inc';
  37.       $addressPayer['AddressLine'] = '435 SOUTH STREET';
  38.       $addressPayer['City'] = 'RIS TOWNSHIP';
  39.       $addressPayer['StateProvinceCode'] = 'NJ';
  40.       $addressPayer['PostalCode'] = '07960';
  41.       $addressPayer['CountryCode'] = 'US';
  42.       $payer['Address'] = $addressPayer;
  43.       $shipmentbillingoption['Code'] = '10';
  44.       $shipmentbillingoption['Description'] = 'PREPAID';
  45.       $paymentinformation['Payer'] = $payer;
  46.       $paymentinformation['ShipmentBillingOption'] = $shipmentbillingoption;
  47.       $request['PaymentInformation'] = $paymentinformation;
  48.  
  49.       $service['Code'] = '308';
  50.       $service['Description'] = 'UPS Freight LTL';
  51.       $request['Service'] = $service;
  52.  
  53.       $handlingunitone['Quantity'] = '20';
  54.       $handlingunitone['Type'] = array
  55.       (
  56.           'Code' => 'PLT',
  57.           'Description' => 'PALLET'
  58.       );
  59.       $request['HandlingUnitOne'] = $handlingunitone;
  60.  
  61.       $commodity['CommodityID'] = '';
  62.       $commodity['Description'] = 'No Description';
  63.       $commodity['Weight'] = array
  64.       (
  65.          'UnitOfMeasurement' => array
  66.          (
  67.              'Code' => 'LBS',
  68.              'Description' => 'Pounds'
  69.          ),
  70.          'Value' => '750'
  71.       );
  72.       $commodity['Dimensions'] = array
  73.       (
  74.           'UnitOfMeasurement' => array
  75.           (
  76.               'Code' => 'IN',
  77.               'Description' => 'Inches'
  78.           ),
  79.           'Length' => '23',
  80.           'Width' => '17',
  81.           'Height' => '45'
  82.       );
  83.       $commodity['NumberOfPieces'] = '45';
  84.       $commodity['PackagingType'] = array
  85.       (
  86.            'Code' => 'BAG',
  87.            'Description' => 'BAG'
  88.       );
  89.       $commodity['DangerousGoodsIndicator'] = '';
  90.       $commodity['CommodityValue'] = array
  91.       (
  92.            'CurrencyCode' => 'USD',
  93.            'MonetaryValue' => '5670'
  94.       );
  95.       $commodity['FreightClass'] = '60';
  96.       $commodity['NMFCCommodityCode'] = '';
  97.       $request['Commodity'] = $commodity;
  98.  
  99.       $shipmentserviceoptions['PickupOptions'] = array
  100.       (
  101.             'HolidayPickupIndicator' => '',
  102.             'InsidePickupIndicator' => '',
  103.             'ResidentialPickupIndicator' => '',
  104.             'WeekendPickupIndicator' => '',
  105.             'LiftGateRequiredIndicator' => ''
  106.       );
  107.       $shipmentserviceoptions['OverSeasLeg'] = array
  108.       (
  109.              'Dimensions' => array
  110.              (
  111.                   'Volume' => '20',
  112.                   'UnitOfMeasurement' => array
  113.                   (
  114.                       'Code' => 'CF',
  115.                       'Description' => 'String'
  116.                   )
  117.              ),
  118.              'Value' => array
  119.              (
  120.                   'Cube' => array
  121.                   (
  122.                        'CurrencyCode' => 'USD',
  123.                        'MonetaryValue' => '5670'
  124.                   )
  125.              ),
  126.              'COD' => array
  127.              (
  128.                   'CODValue' => array
  129.                   (
  130.                        'CurrencyCode' => 'USD',
  131.                        'MonetaryValue' => '363'
  132.                   ),
  133.                   'CODPaymentMethod' => array
  134.                   (
  135.                        'Code' => 'M',
  136.                        'Description' => 'For Company Check'
  137.                   ),
  138.                   'CODBillingOption' => array
  139.                   (
  140.                        'Code' => '01',
  141.                        'Description' => 'Prepaid'
  142.                   ),
  143.                   'RemitTo' => array
  144.                   (
  145.                        'Name' => 'RemitToSomebody',
  146.                        'Address' => array
  147.                        (
  148.                              'AddressLine' => '640 WINTERS AVE',
  149.                              'City' => 'PARAMUS',
  150.                              'StateProvinceCode' => 'NJ',
  151.                              'PostalCode' => '07652',
  152.                              'CountryCode' => 'US'
  153.                        ),
  154.                        'AttentionName' => 'C J Parker'
  155.                   )
  156.               ),
  157.               'DangerousGoods' => array
  158.               (
  159.                     'Name' => 'Very Safe',
  160.                     'Phone' => array
  161.                     (
  162.                         'Number' => '453563321',
  163.                         'Extension' => '1111'
  164.                     ),
  165.                     'TransportationMode' => array
  166.                     (
  167.                         'Code' => 'CARGO',
  168.                         'Description' => 'Cargo Mode'
  169.                     )
  170.               ),
  171.               'SortingAndSegregating' => array
  172.               (
  173.                     'Quantity' => '23452'
  174.               ),
  175.               'CustomsValue' => array
  176.               (
  177.                     'CurrencyCode' => 'USD',
  178.                     'MonetaryValue' => '23457923'
  179.               ),
  180.               'HandlingCharge' => array
  181.               (
  182.                     'Amount' => array
  183.                     (
  184.                         'CurrencyCode' => 'USD',
  185.                         'MonetaryValue' => '450'
  186.                     )
  187.               )
  188.        );
  189.       $request['ShipmentServiceOptions'] = $shipmentserviceoptions;
  190.  
  191.       echo "Request.......\n";
  192.       echo "<pre>";print_r($request);echo "</pre>";
  193.       echo "\n\n";
  194.       return $request;
  195.   }
  196.  
  197.   try
  198.   {
  199.  
  200.     $mode = array
  201.     (
  202.          'soap_version' => 'SOAP_1_1',  // use soap 1.1 client
  203.          'trace' => 1
  204.     );
  205.  
  206.     // initialize soap client
  207.     $client = new SoapClient($wsdl , $mode);
  208.  
  209.     //set endpoint url
  210.     $client->__setLocation($endpointurl);
  211.  
  212.  
  213.     //create soap header
  214.     $usernameToken['Username'] = $userid;
  215.     $usernameToken['Password'] = $passwd;
  216.     $serviceAccessLicense['AccessLicenseNumber'] = $access;
  217.     $upss['UsernameToken'] = $usernameToken;
  218.     $upss['ServiceAccessToken'] = $serviceAccessLicense;
  219.  
  220.     $header = new SoapHeader('http://www.ups.com/XMLSchema/XOLTWS/UPSS/v1.0','UPSSecurity',$upss);
  221.     $client->__setSoapHeaders($header);
  222.  
  223.     //get response
  224.     $resp = $client->__soapCall($operation ,array(processFreightRate()));
  225.  
  226.     //get status
  227.     echo "Response Status: " . $resp->Response->ResponseStatus->Description ."\n";
  228.  
  229.     //save soap request and response to file
  230.     $fw = fopen($outputFileName , 'w');
  231.     fwrite($fw , "Request: \n" . $client->__getLastRequest() . "\n");
  232.     fwrite($fw , "Response: \n" . $client->__getLastResponse() . "\n");
  233.     fclose($fw);
  234.  
  235.   }
  236.   catch(Exception $ex)
  237.   {
  238.     echo "<pre>";print_r ($ex);echo "</pre>";
  239.   }
  240.  
  241. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement