wpdesk / ups-api
PHP UPS API
Requires
- php: ^7.0 || ^8.0
- guzzlehttp/guzzle: ^6.0
- psr/log: ^1.0
Requires (Dev)
- phpunit/phpunit: ^4.8 || ^5.0
Suggests
- ext-soap: Required if you need the Tradeability API of UPS
- dev-master
- 0.10.4
- 0.10.3
- 0.10.2
- 0.10.1
- 0.10.0
- 0.9.5
- 0.9.4
- 0.9.3
- 0.9.2
- 0.9.1
- 0.9
- 0.8.0
- 0.7.14
- 0.7.13
- 0.7.12
- 0.7.11
- 0.7.10
- 0.7.9
- 0.7.8
- 0.7.7
- 0.7.6
- 0.7.5
- 0.7.4
- 0.7.3
- 0.7.2
- 0.7.1
- 0.7.0
- 0.6.3
- 0.6.2
- 0.6.1
- 0.6.0
- 0.5.2
- dev-develop / 0.5.1.x-dev
- 0.5.1
- 0.5.0
- 0.4.0
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.5
- 0.2.4
- 0.2.3
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.9
- 0.1.8
- 0.1.7
- 0.1.6
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.0
- dev-bugfix/packaging-type-code
- dev-bugfix/php-8.1
- dev-bugfix/label-specification
- dev-feature/pickup-type
- dev-bugfix/empty-response
- dev-bugfix/empty-reposnse
- dev-feature/request-log
- dev-feature/newest-changes
- dev-feature/add-ship-name
- dev-feature/phone
- dev-feature/label-stock-size
- dev-feature/label-recovery
- dev-feature/simple-address-validation
- dev-rynner-master
- dev-quickfix/encoding-issue
This package is auto-updated.
Last update: 2024-09-01 09:03:55 UTC
README
这个库旨在将所有UPS API包装成一个易于使用的PHP库。它目前涵盖了Quantum View®、跟踪API、运输API、评级API和运输时间API。欢迎贡献力量。
目录
需求
此库使用PHP 5.5+。
要使用UPS API,您必须从UPS申请访问密钥。对于每个请求,您都必须提供访问密钥、您的UPS用户ID和密码。
安装
建议您通过composer安装PHP UPS API库。为此,运行Composer命令安装PHP UPS API的最新稳定版本
composer require gabrielbull/ups-api
如果没有使用composer,您还必须包含以下库: Guzzle、Guzzle Promises、[Guzzle PSR7] (https://github.com/guzzle/psr7)、PHP-Fig PSR Log和PHP-Fig HTTP Message。
地址验证类(街道级)
地址验证类允许您在街道级别验证地址。当地址无效时,会提供建议。
注意:UPS有两种地址验证。这是街道级选项,它包括正常地址验证类的所有选项,并增加了街道级别验证。
目前仅支持美国和波多黎各。
示例
$address = new \Ups\Entity\Address(); $address->setAttentionName('Test Test'); $address->setBuildingName('Test'); $address->setAddressLine1('Address Line 1'); $address->setAddressLine2('Address Line 2'); $address->setAddressLine3('Address Line 3'); $address->setStateProvinceCode('NY'); $address->setCity('New York'); $address->setCountryCode('US'); $address->setPostalCode('10000'); $xav = new \Ups\AddressValidation($accessKey, $userId, $password); $xav->activateReturnObjectOnValidate(); //This is optional try { $response = $xav->validate($address, $requestOption = \Ups\AddressValidation::REQUEST_OPTION_ADDRESS_VALIDATION, $maxSuggestion = 15); } catch (Exception $e) { var_dump($e); }
AddressValidation::validateReturnAVObject()
在上面的代码中,$xav->activateReturnObjectOnValidate()
是完全可选的。调用此方法将导致AddressValidation::validate()
返回一个AddressValidationResponse
对象。如果您不调用此方法,validate
将继续按照以前的模式运行。如果您不调用此方法,将返回一个单独的对象,其中包含已匹配的有效地址或地址模糊时的第一个候选地址。
AddressValidationResponse对象提供了一些方法,允许您更容易地查询API响应以确定结果。在上面的示例中,返回一个AddressValidationResponse
对象将允许您更具体地处理各种结果
if ($response->noCandidates()) { //Do something clever and helpful to let the use know the address is invalid } if ($response->isAmbiguous()) { $candidateAddresses = $response->getCandidateAddressList(); foreach($candidateAddresses as $address) { //Present user with list of candidate addresses so they can pick the correct one } } if ($response->isValid()) { $validAddress = $response->getValidatedAddress(); //Show user validated address or update their address with the 'official' address //Or do something else helpful... }
参数
地址验证参数包括
address
地址对象,如示例中所构造requestOption
三个请求选项之一。见文档。默认值 = 地址验证。maxSuggestion
返回的最大建议数量。最大 = 50
简单地址验证类
简单地址验证类允许您验证的范围比之前的类更少。它返回所提供地址的质量分数并提供替代方案。
注意:UPS有两种地址验证。这是简单选项。
目前仅支持美国和波多黎各。
示例
$address = new \Ups\Entity\Address(); $address->setStateProvinceCode('NY'); $address->setCity('New York'); $address->setCountryCode('US'); $address->setPostalCode('10000'); $av = new \Ups\SimpleAddressValidation($accessKey, $userId, $password); try { $response = $av->validate($address); var_dump($response); } catch (Exception $e) { var_dump($e); }
参数
简单地址验证参数包括
address
地址对象,如示例中所构造
QuantumView类
QuantumView类允许您请求Quantum View数据订阅。
示例
$quantumView = new Ups\QuantumView($accessKey, $userId, $password); try { // Get the subscription for all events for the last hour $events = $quantumView->getSubscription(null, (time() - 3600)); foreach($events as $event) { // Your code here echo $event->Type; } } catch (Exception $e) { var_dump($e); }
参数
QuantumView参数是
name
用户请求的订阅名称。如果 为空,将返回所有事件。beginDateTime
订阅检索条件的开始日期时间。格式:Y-m-d H:i:s 或 Unix 时间戳。endDateTime
订阅检索条件的结束日期时间。格式:Y-m-d H:i:s 或 Unix 时间戳。fileName
用户请求的特定订阅的文件名。bookmark
将文件标记为下次检索。
如果您提供了 beginDateTime
,但没有 endDateTime
,则 endDateTime
将默认为当前日期时间。
要使用 fileName
参数,不要提供 beginDateTime
。
跟踪类
跟踪类允许您使用 UPS 跟踪 API 跟踪货物运输。
使用跟踪号/邮件创新跟踪号的示例
$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); }
参数
跟踪参数包括
trackingNumber
包裹的跟踪号。requestOption
可选处理。对于邮件创新,唯一有效的选项是最后活动记录和所有活动记录。
使用参考号的示例
$tracking = new Ups\Tracking($accessKey, $userId, $password); try { $shipment = $tracking->trackByReference('REFERENCE NUMBER'); foreach($shipment->Package->Activity as $activity) { var_dump($activity); } } catch (Exception $e) { var_dump($e); }
参数
跟踪参数包括
referenceNumber
通过参考号跟踪任何 UPS 包裹或货物的能力。参考号可以是采购订单号、工作号等。参考号在生成货物运输时提供。requestOption
可选处理。对于邮件创新,唯一有效的选项是最后活动记录和所有活动记录。
使用参考号和附加参数的示例
$tracking = new Ups\Tracking($accessKey, $userId, $password); $tracking->setShipperNumber('SHIPPER NUMBER'); $beginDate = new \DateTime('2016-01-01'); $endDate = new \DateTime('2016-01-31'); $tracking->setBeginDate($beginDate); $tracking->setEndDate($endDate); try { $shipment = $tracking->trackByReference('REFERENCE NUMBER'); foreach($shipment->Package->Activity as $activity) { var_dump($activity); } } catch (Exception $e) { var_dump($e); }
参数 shipperNumber、beginDate 和 endDate 是可选的。这些参数可以单独设置。当跟踪参考号时,这些参数可以帮助缩小搜索范围,因为可能使用的参考号不是唯一的。当使用跟踪号跟踪时,这些参数不是必需的,因为跟踪号是唯一的。
评级类
费率类允许您使用 UPS 费率 API 获取货物运输费率。
示例
$rate = new Ups\Rate( $accessKey, $userId, $password ); try { $shipment = new \Ups\Entity\Shipment(); $shipperAddress = $shipment->getShipper()->getAddress(); $shipperAddress->setPostalCode('99205'); $address = new \Ups\Entity\Address(); $address->setPostalCode('99205'); $shipFrom = new \Ups\Entity\ShipFrom(); $shipFrom->setAddress($address); $shipment->setShipFrom($shipFrom); $shipTo = $shipment->getShipTo(); $shipTo->setCompanyName('Test Ship To'); $shipToAddress = $shipTo->getAddress(); $shipToAddress->setPostalCode('99205'); $package = new \Ups\Entity\Package(); $package->getPackagingType()->setCode(\Ups\Entity\PackagingType::PT_PACKAGE); $package->getPackageWeight()->setWeight(10); // if you need this (depends of the shipper country) $weightUnit = new \Ups\Entity\UnitOfMeasurement; $weightUnit->setCode(\Ups\Entity\UnitOfMeasurement::UOM_KGS); $package->getPackageWeight()->setUnitOfMeasurement($weightUnit); $dimensions = new \Ups\Entity\Dimensions(); $dimensions->setHeight(10); $dimensions->setWidth(10); $dimensions->setLength(10); $unit = new \Ups\Entity\UnitOfMeasurement; $unit->setCode(\Ups\Entity\UnitOfMeasurement::UOM_IN); $dimensions->setUnitOfMeasurement($unit); $package->setDimensions($dimensions); $shipment->addPackage($package); var_dump($rate->getRate($shipment)); } catch (Exception $e) { var_dump($e); }
参数
rateRequest
必需。带有货物运输详细信息的 rateRequest 对象。
此费率类尚未完成!当它完成时将添加参数。
评级运输时间类
RateTimeInTransit 类允许您获取与 Rate 类相同的货物运输费率,但响应还将包括 TimeInTransit 数据。
示例
$rate = new Ups\RateTimeInTransit( $accessKey, $userId, $password ); try { $shipment = new \Ups\Entity\Shipment(); $shipperAddress = $shipment->getShipper()->getAddress(); $shipperAddress->setPostalCode('99205'); $address = new \Ups\Entity\Address(); $address->setPostalCode('99205'); $shipFrom = new \Ups\Entity\ShipFrom(); $shipFrom->setAddress($address); $shipment->setShipFrom($shipFrom); $shipTo = $shipment->getShipTo(); $shipTo->setCompanyName('Test Ship To'); $shipToAddress = $shipTo->getAddress(); $shipToAddress->setPostalCode('99205'); $package = new \Ups\Entity\Package(); $package->getPackagingType()->setCode(\Ups\Entity\PackagingType::PT_PACKAGE); $package->getPackageWeight()->setWeight(10); // if you need this (depends of the shipper country) $weightUnit = new \Ups\Entity\UnitOfMeasurement; $weightUnit->setCode(\Ups\Entity\UnitOfMeasurement::UOM_KGS); $package->getPackageWeight()->setUnitOfMeasurement($weightUnit); $dimensions = new \Ups\Entity\Dimensions(); $dimensions->setHeight(10); $dimensions->setWidth(10); $dimensions->setLength(10); $unit = new \Ups\Entity\UnitOfMeasurement; $unit->setCode(\Ups\Entity\UnitOfMeasurement::UOM_IN); $dimensions->setUnitOfMeasurement($unit); $package->setDimensions($dimensions); $shipment->addPackage($package); $deliveryTimeInformation = new \Ups\Entity\DeliveryTimeInformation(); $deliveryTimeInformation->setPackageBillType(\Ups\Entity\DeliveryTimeInformation::PBT_NON_DOCUMENT); $pickup = new \Ups\Entity\Pickup(); $pickup->setDate("20170520"); $pickup->setTime("160000"); $shipment->setDeliveryTimeInformation($deliveryTimeInformation); var_dump($rate->shopRatesTimeInTransit($shipment)); } catch (Exception $e) { var_dump($e); }
参数
rateRequest
必需。带有货物运输详细信息的 rateRequest 对象。
此 RateTimeInTransit 扩展了尚未完成的 Rate 类!当它完成时将添加参数。
运输时间类
TimeInTransit 类允许您使用 UPS TimeInTransit API 获取所有过境时间。
示例
$timeInTransit = new Ups\TimeInTransit($access, $userid, $passwd); try { $request = new \Ups\Entity\TimeInTransitRequest; // Addresses $from = new \Ups\Entity\AddressArtifactFormat; $from->setPoliticalDivision3('Amsterdam'); $from->setPostcodePrimaryLow('1000AA'); $from->setCountryCode('NL'); $request->setTransitFrom($from); $to = new \Ups\Entity\AddressArtifactFormat; $to->setPoliticalDivision3('Amsterdam'); $to->setPostcodePrimaryLow('1000AA'); $to->setCountryCode('NL'); $request->setTransitTo($to); // Weight $shipmentWeight = new \Ups\Entity\ShipmentWeight; $shipmentWeight->setWeight($totalWeight); $unit = new \Ups\Entity\UnitOfMeasurement; $unit->setCode(\Ups\Entity\UnitOfMeasurement::UOM_KGS); $shipmentWeight->setUnitOfMeasurement($unit); $request->setShipmentWeight($shipmentWeight); // Packages $request->setTotalPackagesInShipment(2); // InvoiceLines $invoiceLineTotal = new \Ups\Entity\InvoiceLineTotal; $invoiceLineTotal->setMonetaryValue(100.00); $invoiceLineTotal->setCurrencyCode('EUR'); $request->setInvoiceLineTotal($invoiceLineTotal); // Pickup date $request->setPickupDate(new DateTime); // Get data $times = $timeInTransit->getTimeInTransit($request); foreach($times->ServiceSummary as $serviceSummary) { var_dump($serviceSummary); } } catch (Exception $e) { var_dump($e); }
参数
timeInTransitRequest
必需。带有货物运输详细信息的 timeInTransitRequest 对象,请参见上面的示例。
定位器类
定位器类允许您搜索 UPS 访问点位置。
示例
$locatorRequest = new \Ups\Entity\LocatorRequest; $originAddress = new \Ups\Entity\OriginAddress; $address = new \Ups\Entity\AddressKeyFormat; $address->setCountryCode('NL'); $originAddress->setAddressKeyFormat($address); $geocode = new \Ups\Entity\GeoCode; $geocode->setLatitude(52.0000); $geocode->setLongitude(4.0000); $originAddress->setGeoCode($geocode); $locatorRequest->setOriginAddress($originAddress); $translate = new \Ups\Entity\Translate; $translate->setLanguageCode('ENG'); $locatorRequest->setTranslate($translate); $acccessPointSearch = new \Ups\Entity\AccessPointSearch; $acccessPointSearch->setAccessPointStatus(\Ups\Entity\AccessPointSearch::STATUS_ACTIVE_AVAILABLE); $locationSearch = new \Ups\Entity\LocationSearchCriteria; $locationSearch->setAccessPointSearch($acccessPointSearch); $locationSearch->setMaximumListSize(25); $locatorRequest->setLocationSearchCriteria($locationSearch); $unitOfMeasurement = new \Ups\Entity\UnitOfMeasurement; $unitOfMeasurement->setCode(\Ups\Entity\UnitOfMeasurement::UOM_KM); $unitOfMeasurement->setDescription('Kilometers'); $locatorRequest->setUnitOfMeasurement($unitOfMeasurement); try { // Get the locations $locator = new Ups\Locator($accessKey, $userId, $password); $locations = $locator->getLocations($locatorRequest, \Ups\Locator::OPTION_UPS_ACCESS_POINT_LOCATIONS); foreach($locations->SearchResults->DropLocation as $location) { // Your code here var_dump($location); } } catch (Exception $e) { var_dump($e); }
参数
定位器类参数包括
locatorRequest
必需。带有请求详细信息的 locatorRequest 对象,请参见示例。requestOption
可选。您正在搜索的位置类型。
交易能力类
可交易性类允许您获取国际货物运输的数据
- 到岸成本(例如,关税)
- 被拒绝方筛选器
- 进口合规性
- 出口许可证检测
注意:目前仅实现了到岸成本 API。
警告:可交易性只能通过 SOAP API 获取。因此,您必须在您的系统上安装 SOAP 扩展。
示例
// Build request $landedCostRequest = new \Ups\Entity\Tradeability\LandedCostRequest; // Build shipment $shipment = new \Ups\Entity\Tradeability\Shipment; $shipment->setOriginCountryCode('NL'); $shipment->setDestinationCountryCode('US'); $shipment->setDestinationStateProvinceCode('TX'); $shipment->setResultCurrencyCode('EUR'); $shipment->setTariffCodeAlert(1); $shipment->setTransportationMode(\Ups\Entity\Tradeability\Shipment::TRANSPORT_MODE_AIR); $shipment->setTransactionReferenceId('1'); // Build product $product = new \Ups\Entity\Tradeability\Product; $product->setProductName('Test'); $tariffInfo = new \Ups\Entity\Tradeability\TariffInfo; $tariffInfo->setTariffCode('5109.90.80.00'); $product->setTariffInfo($tariffInfo); $product->setProductCountryCodeOfOrigin('BD'); $unitPrice = new \Ups\Entity\Tradeability\UnitPrice; $unitPrice->setMonetaryValue(250); $unitPrice->setCurrencyCode('EUR'); $product->setUnitPrice($unitPrice); $weight = new Ups\Entity\Tradeability\Weight; $weight->setValue(0.83); $unitOfMeasurement = new \Ups\Entity\Tradeability\UnitOfMeasurement; $unitOfMeasurement->setCode('kg'); $weight->setUnitOfMeasurement($unitOfMeasurement); $product->setWeight($weight); $quantity = new \Ups\Entity\Tradeability\Quantity; $quantity->setValue(5); $unitOfMeasurement = new \Ups\Entity\Tradeability\UnitOfMeasurement; $unitOfMeasurement->setCode(\Ups\Entity\Tradeability\UnitOfMeasurement::PROD_PIECES); $quantity->setUnitOfMeasurement($unitOfMeasurement); $product->setQuantity($quantity); $product->setTariffCodeAlert(1); // Add product to shipment $shipment->addProduct($product); // Query request $queryRequest = new \Ups\Entity\Tradeability\QueryRequest; $queryRequest->setShipment($shipment); $queryRequest->setSuppressQuestionIndicator(true); // Build $landedCostRequest->setQueryRequest($queryRequest); try { // Get the data $api = new Ups\Tradeability($accessKey, $userId, $password); $result = $api->getLandedCosts($landedCostRequest); var_dump($result); } catch (Exception $e) { var_dump($e); }
参数
对于到岸成本调用,参数包括
landedCostRequest
必需。带有请求详细信息的 landedCostRequest 对象,请参见示例。
运输类
运输类允许您注册货物运输。这也包括退货货物运输。
运输流程包括 2 个步骤
- 确认:向 UPS 发送信息以获取验证并获得可用于接受货物的摘要。
- 接受:完成货物运输,将其标记为即将发货。获取标签和附加信息。
请注意,这只是一个示例。您的用例可能需要向 UPS 发送更多或更少的信息。
在示例中,使用 $return 来展示如何处理返回值。
示例
// Start shipment $shipment = new Ups\Entity\Shipment; // Set shipper $shipper = $shipment->getShipper(); $shipper->setShipperNumber('XX'); $shipper->setName('XX'); $shipper->setAttentionName('XX'); $shipperAddress = $shipper->getAddress(); $shipperAddress->setAddressLine1('XX'); $shipperAddress->setPostalCode('XX'); $shipperAddress->setCity('XX'); $shipperAddress->setStateProvinceCode('XX'); // required in US $shipperAddress->setCountryCode('XX'); $shipper->setAddress($shipperAddress); $shipper->setEmailAddress('XX'); $shipper->setPhoneNumber('XX'); $shipment->setShipper($shipper); // To address $address = new \Ups\Entity\Address(); $address->setAddressLine1('XX'); $address->setPostalCode('XX'); $address->setCity('XX'); $address->setStateProvinceCode('XX'); // Required in US $address->setCountryCode('XX'); $shipTo = new \Ups\Entity\ShipTo(); $shipTo->setAddress($address); $shipTo->setCompanyName('XX'); $shipTo->setAttentionName('XX'); $shipTo->setEmailAddress('XX'); $shipTo->setPhoneNumber('XX'); $shipment->setShipTo($shipTo); // From address $address = new \Ups\Entity\Address(); $address->setAddressLine1('XX'); $address->setPostalCode('XX'); $address->setCity('XX'); $address->setStateProvinceCode('XX'); $address->setCountryCode('XX'); $shipFrom = new \Ups\Entity\ShipFrom(); $shipFrom->setAddress($address); $shipFrom->setName('XX'); $shipFrom->setAttentionName($shipFrom->getName()); $shipFrom->setCompanyName($shipFrom->getName()); $shipFrom->setEmailAddress('XX'); $shipFrom->setPhoneNumber('XX'); $shipment->setShipFrom($shipFrom); // Sold to $address = new \Ups\Entity\Address(); $address->setAddressLine1('XX'); $address->setPostalCode('XX'); $address->setCity('XX'); $address->setCountryCode('XX'); $address->setStateProvinceCode('XX'); $soldTo = new \Ups\Entity\SoldTo; $soldTo->setAddress($address); $soldTo->setAttentionName('XX'); $soldTo->setCompanyName($soldTo->getAttentionName()); $soldTo->setEmailAddress('XX'); $soldTo->setPhoneNumber('XX'); $shipment->setSoldTo($soldTo); // Set service $service = new \Ups\Entity\Service; $service->setCode(\Ups\Entity\Service::S_STANDARD); $service->setDescription($service->getName()); $shipment->setService($service); // Mark as a return (if return) if ($return) { $returnService = new \Ups\Entity\ReturnService; $returnService->setCode(\Ups\Entity\ReturnService::PRINT_RETURN_LABEL_PRL); $shipment->setReturnService($returnService); } // Set description $shipment->setDescription('XX'); // Add Package $package = new \Ups\Entity\Package(); $package->getPackagingType()->setCode(\Ups\Entity\PackagingType::PT_PACKAGE); $package->getPackageWeight()->setWeight(10); $unit = new \Ups\Entity\UnitOfMeasurement; $unit->setCode(\Ups\Entity\UnitOfMeasurement::UOM_KGS); $package->getPackageWeight()->setUnitOfMeasurement($unit); // Set Package Service Options $packageServiceOptions = new \Ups\Entity\PackageServiceOptions(); $packageServiceOptions->setShipperReleaseIndicator(true); $package->setPackageServiceOptions($packageServiceOptions); // Set dimensions $dimensions = new \Ups\Entity\Dimensions(); $dimensions->setHeight(50); $dimensions->setWidth(50); $dimensions->setLength(50); $unit = new \Ups\Entity\UnitOfMeasurement; $unit->setCode(\Ups\Entity\UnitOfMeasurement::UOM_CM); $dimensions->setUnitOfMeasurement($unit); $package->setDimensions($dimensions); // Add descriptions because it is a package $package->setDescription('XX'); // Add this package $shipment->addPackage($package); // Set Reference Number $referenceNumber = new \Ups\Entity\ReferenceNumber; if ($return) { $referenceNumber->setCode(\Ups\Entity\ReferenceNumber::CODE_RETURN_AUTHORIZATION_NUMBER); $referenceNumber->setValue($return_id); } else { $referenceNumber->setCode(\Ups\Entity\ReferenceNumber::CODE_INVOICE_NUMBER); $referenceNumber->setValue($order_id); } $shipment->setReferenceNumber($referenceNumber); // Set payment information $shipment->setPaymentInformation(new \Ups\Entity\PaymentInformation('prepaid', (object)array('AccountNumber' => 'XX'))); // Ask for negotiated rates (optional) $rateInformation = new \Ups\Entity\RateInformation; $rateInformation->setNegotiatedRatesIndicator(1); $shipment->setRateInformation($rateInformation); // Get shipment info try { $api = new Ups\Shipping($accessKey, $userId, $password); $confirm = $api->confirm(\Ups\Shipping::REQ_VALIDATE, $shipment); var_dump($confirm); // Confirm holds the digest you need to accept the result if ($confirm) { $accept = $api->accept($confirm->ShipmentDigest); var_dump($accept); // Accept holds the label and additional information } } catch (\Exception $e) { var_dump($e); }
如果您想从 UPS 快递标签图像数据中创建一个可打印的文件,您可以使用以下类似的方式:
$label_file = $order_id . ".gif";
$base64_string = $accept->PackageResults->LabelImage->GraphicImage;
$ifp = fopen($label_file, 'wb');
fwrite($ifp, base64_decode($base64_string));
fclose($ifp);
参数
对于 confirm
调用,参数如下:
- $validation UPS_Shipping::REQ_* 常量(或 null)。必需
- $shipment 装运数据容器。必需
- $labelSpec 标签规范数据。可选
- $receiptSpec 装运请求收据规范数据。可选
对于 accept
调用,参数如下:
- $shipmentDigest 从 ShipConfirm 请求接收到的 UPS 装运摘要。必需
日志记录
所有构造函数都接受一个符合 PSR-3 的记录器。
除此之外,主要 UPS 类有一个公开的 setLogger
方法,可以在构造函数运行后设置它。
请求和响应(包括 XML,无访问键)以 DEBUG 级别记录。在 INFO 级别只报告事件,不记录 XML 内容。更严重的问题(例如无连接)以更高的严重性记录。
使用 Monolog 的示例
// Create logger
$log = new \Monolog\Logger('ups');
$log->pushHandler(new \Monolog\Handler\StreamHandler('logs/ups.log', \Monolog\Logger::DEBUG));
// Create Rate object + insert logger
$rate = new Ups\Rate($key, $username, $password, $useIntegration, $log);
许可
PHP UPS API 采用 MIT 许可证(MIT) 许可。