定制 ktscript/fedex-rest 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ktscript/fedex-rest

Composer 安装命令:

composer require ktscript/fedex-rest

包简介

Fedex REST API project, adapted for php >=7.2.5, Laravel 7-12+, guzzle>=7

README 文档

README

PHP package for integrating with FedEx REST API. This package provides a simple and intuitive interface for working with FedEx services including tracking, shipping, and address validation.

Features

  • 🚀 Easy Integration - Simple, fluent API for interacting with FedEx services
  • 🔐 OAuth 2.0 Support - Built-in authorization handling
  • 📦 Shipping Labels - Create shipping labels and track packages
  • 📍 Address Validation - Validate addresses using FedEx API
  • 📌 Location Search - Find FedEx pickup/dropoff locations by address, coordinates, or phone
  • 🔄 Environment Switching - Switch between sandbox and production environments easily
  • 🎯 Laravel Compatible - Works seamlessly with Laravel 7-12+ using Conditionable trait
  • 🔧 Fluent Interface - Chainable methods for building requests

Requirements

  • PHP >= 7.2.5
  • Laravel 7-12+ (or illuminate/support package)
  • Guzzle HTTP Client ^6|^7
  • JSON extension

Installation

Install the package via Composer:

composer require ktscript/fedex-rest

Configuration

Getting FedEx API Credentials

  1. Register for a FedEx Developer account at FedEx Developer Portal
  2. Create a new application to get your client_id and client_secret
  3. Choose between sandbox (testing) or production environment

Usage

Authorization

First, you need to obtain an access token using your FedEx credentials:

use FedexRest\Authorization\Authorize;

$authorize = new Authorize();
$authorize->setClientId('your_client_id')
    ->setClientSecret('your_client_secret')
    ->useProduction() // Use production environment (optional, defaults to sandbox)
    ->authorize();

$token = $authorize->access_token;

Tracking Packages

Track one or multiple packages by tracking number:

use FedexRest\Services\Track\TrackByTrackingNumberRequest;

$tracking = new TrackByTrackingNumberRequest();
$tracking->setAccessToken($token)
    ->setTrackingNumber('1234567890') // Single tracking number
    ->includeDetailedScans() // Optional: include detailed scan information
    ->useProduction() // Optional: use production environment
    ->request();

// Or track multiple packages
$tracking->setTrackingNumber(['1234567890', '0987654321'])
    ->request();

Creating Shipping Labels

Create shipping labels for packages:

use FedexRest\Services\Ship\CreateTagRequest;
use FedexRest\Entity\Person;
use FedexRest\Entity\Address;
use FedexRest\Entity\Item;
use FedexRest\Entity\Weight;
use FedexRest\Services\Ship\Type\ServiceType;
use FedexRest\Services\Ship\Type\PackagingType;
use FedexRest\Services\Ship\Type\PickupType;

// Create shipper address
$shipperAddress = new Address();
$shipperAddress->setStreetLines('123 Main St')
    ->setCity('Memphis')
    ->setStateOrProvince('TN')
    ->setPostalCode('38116')
    ->setCountryCode('US');

// Create shipper
$shipper = new Person();
$shipper->setPersonName('John Doe')
    ->setPhoneNumber(1234567890)
    ->withAddress($shipperAddress);

// Create recipient address
$recipientAddress = new Address();
$recipientAddress->setStreetLines('456 Oak Ave')
    ->setCity('Los Angeles')
    ->setStateOrProvince('CA')
    ->setPostalCode('90001')
    ->setCountryCode('US');

// Create recipient
$recipient = new Person();
$recipient->setPersonName('Jane Smith')
    ->setPhoneNumber(9876543210)
    ->withAddress($recipientAddress);

// Create item weight
$weight = new Weight();
$weight->setUnit('LB')
    ->setValue(5);

// Create item
$item = new Item();
$item->setItemDescription('Sample Product')
    ->setWeight($weight);

// Create shipping label
$shipment = new CreateTagRequest();
$shipment->setAccessToken($token)
    ->setAccountNumber(123456789)
    ->setShipper($shipper)
    ->setRecipients($recipient)
    ->setLineItems($item)
    ->setServiceType(ServiceType::_FEDEX_GROUND)
    ->setPackagingType(PackagingType::_FEDEX_BOX)
    ->setPickupType(PickupType::_USE_SCHEDULED_PICKUP)
    ->setShipDatestamp('2024-01-15')
    ->useProduction() // Optional: use production environment
    ->request();

Address Validation

Validate addresses using FedEx address validation service:

use FedexRest\Services\AddressValidation\AddressValidation;
use FedexRest\Entity\Address;

$address = new Address();
$address->setStreetLines('123 Main St')
    ->setCity('Memphis')
    ->setStateOrProvince('TN')
    ->setPostalCode('38116')
    ->setCountryCode('US');

$validation = new AddressValidation();
$validation->setAccessToken($token)
    ->setAddress($address)
    ->useProduction() // Optional: use production environment
    ->request();

Location Search (FedEx Location API)

Search for FedEx pickup and dropoff locations by address, coordinates, or phone:

use FedexRest\Services\Location\LocationSearchRequest;
use FedexRest\Services\Location\Type\LocationType;
use FedexRest\Entity\Address;

// Search by address
$address = new Address();
$address->setStreetLines('123 Main St')
    ->setCity('Memphis')
    ->setStateOrProvince('TN')
    ->setPostalCode('38116')
    ->setCountryCode('US');

$locationSearch = new LocationSearchRequest();
$result = $locationSearch->setAccessToken($token)
    ->setAddress($address)
    ->setDistance(10, LocationSearchRequest::UNITS_KM)
    ->setLocationTypes([LocationType::FEDEX_OFFICE, LocationType::FEDEX_SELF_SERVICE_LOCATION])
    ->setResultsLimit(15)
    ->useProduction()
    ->request();

// Or search by coordinates
$result = $locationSearch->setAccessToken($token)
    ->setCoordinates(35.1495, -90.0490)
    ->setDistance(5, LocationSearchRequest::UNITS_MI)
    ->request();

// Or search by phone number
$result = $locationSearch->setAccessToken($token)
    ->setPhoneNumber('9015551234')
    ->request();

The response is a decoded object. Main keys:

  • output.totalResults, output.resultsReturned — count of locations
  • output.locationDetailList — array of locations (each has distance, contactAndAddress with address/contact/displayName, locationId, storeHours, carrierDetailList, locationType, locationCapabilities, etc.)
  • output.nearestLocation, output.latestLocation — single location objects when applicable
  • output.matchedAddress — normalized search address
  • output.alerts — optional messages

Environment Switching

The package supports easy switching between sandbox (testing) and production environments:

// Use sandbox (default)
$request->request();

// Use production
$request->useProduction()->request();

// Using Laravel's Conditionable trait (when available)
$request->when($isProduction, fn($req) => $req->useProduction())
    ->request();

Raw Response

Get raw HTTP response instead of decoded JSON:

$response = $request->asRaw()->request();
// Returns GuzzleHttp\Psr7\Response object

Available Service Types

Use constants from FedexRest\Services\Ship\Type\ServiceType:

  • ServiceType::_FEDEX_GROUND
  • ServiceType::_FEDEX_2_DAY
  • ServiceType::_STANDARD_OVERNIGHT
  • ServiceType::_PRIORITY_OVERNIGHT
  • ServiceType::_INTERNATIONAL_PRIORITY
  • And many more...

Available Packaging Types

Use constants from FedexRest\Services\Ship\Type\PackagingType:

  • PackagingType::_YOUR_PACKAGING
  • PackagingType::_FEDEX_BOX
  • PackagingType::_FEDEX_ENVELOPE
  • PackagingType::_FEDEX_PAK
  • And more...

Available Pickup Types

Use constants from FedexRest\Services\Ship\Type\PickupType:

  • PickupType::_USE_SCHEDULED_PICKUP
  • PickupType::_DROPOFF_AT_FEDEX_LOCATION
  • PickupType::_CONTACT_FEDEX_TO_SCHEDULE

Exception Handling

The package throws specific exceptions for missing required data:

  • MissingAccessTokenException - When access token is not provided
  • MissingAuthCredentialsException - When client credentials are missing
  • MissingTrackingNumberException - When tracking number is not provided
  • MissingAccountNumberException - When account number is missing
  • MissingLineItemException - When line items are not provided
use FedexRest\Exceptions\MissingAccessTokenException;

try {
    $response = $request->request();
} catch (MissingAccessTokenException $e) {
    // Handle missing access token
}

Advanced Usage

Custom Request Parameters

For shipping labels, you can customize request parameters:

$shipment = new CreateTagRequest();
$params = $shipment->getRequestParams();
// Modify $params array as needed
$shipment->setRequestParams($params)
    ->setAccessToken($token)
    ->request();

Laravel Integration

This package works seamlessly with Laravel 7-12+. The switchableEnv trait uses Laravel's Conditionable trait, allowing you to use the when() method:

use FedexRest\Services\Track\TrackByTrackingNumberRequest;

$tracking = new TrackByTrackingNumberRequest();
$tracking->setAccessToken($token)
    ->setTrackingNumber('1234567890')
    ->when(config('app.env') === 'production', fn($req) => $req->useProduction())
    ->request();

Support

For issues, questions, or contributions, please visit:

License

This package is open-sourced software licensed under the MIT license.

Credits

Special thanks to Sinnbeck for the help and contributions.

Changelog

1.2.1-1.2.5

  • Fix Location API
  • Add LocationType constants
  • Add LocationSearchRequest
  • Add LocationSearchResponse
  • Add LocationSearchResponseItem

1.2.0

  • FedEx Location API: search pickup/dropoff locations by address, coordinates, or phone
  • LocationSearchRequest and LocationType constants
  • Support contact: Telegram @ktscript added to README and composer.json

1.0.0

  • Initial stable release
  • Support for Laravel 7-12+
  • OAuth 2.0 authorization
  • Package tracking
  • Shipping label creation
  • Address validation
  • Environment switching (sandbox/production)
  • Fluent interface with method chaining

ktscript/fedex-rest 适用场景与选型建议

ktscript/fedex-rest 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16.86k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 12 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「rest」 「api」 「FedEx」 「shipping」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ktscript/fedex-rest 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ktscript/fedex-rest 我们能提供哪些服务?
定制开发 / 二次开发

基于 ktscript/fedex-rest 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 16.86k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 21
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-12-12