firewizard/fancourier-api
Composer 安装命令:
composer require firewizard/fancourier-api
包简介
Consumer for FanCourier API
README 文档
README
Table of contents
Installation
Requirements
- PHP >= 7.0
- ext-curl
- ext-json
Composer
Require the package via composer
composer require firewizard/fancourier-api
Manual
If used without composer, you will need to manually require the autoload.php file
require_once '/path/to/fancourier-api/src/autoload.php';
Breaking changes from v1
- removed Auth class
- removed SendsFile trait
- getCities city array keys have changed (county instead of judet, name instead of localitate, exteriorKm instead of km)
- CreateAwbBulk response body is now an array of AWBs instead of an array of CreateAwb response objects
- trackAwb now returns the full events list, as an array, not just the last message
- trackAwbBulk now returns the full events list, as an array, with info about all AWBs in the request. It's up to you to group by AWB
Usage
Authentication
Create a new instance of Fancourier.php supplying the client_id, username and password.
$clientId = 'your_client_id'; $username = 'your_username'; $password = 'your_password'; $fan = new Fancourier\Fancourier($clientId, $username, $password);
Or you can use the test instance static method:
$fan = Fancourier\Fancourier::testInstance();
Caching the auth token
By default, authentication is always called, which means for every regular request, there will be an extra request to obtain the auth token. You can fix this and cache the auth token in your app.
First, you need to create a new class that implements Fancourier\AuthTokenCacheContract:
class FancourierAuthCache implements AuthTokenCacheContract { const CACHE_KEY = 'fancourier_auth_token'; const CACHE_LIFETIME = 43200; //12 hrs public function get() { return Cache::get(static::CACHE_KEY); } public function set($value) { Cache::put(static::CACHE_KEY, $value, static::CACHE_LIFETIME); } }
Then, pass this to the main Fancourier object:
$api = new Fancourier(...); $api->useAuthTokenCache(new FancourierAuthCache());
Note: Tokens change every 24 hours according to Fan Courier, so might want to use a lower cache TTL.
Get estimated shipping cost
Request
$request = new Fancourier\Request\GetRates(); $request ->setParcels(1) ->setWeight(2) ->setRegion('Arad') ->setCity('Aciuta') ->setDeclaredValue(125);
Response
$response = $fan->getRates($request); if ($response->isOk()) { var_dump($response->getBody()); } else { var_dump($response->getErrorMessage()); }
Create AWB
Request
$request = new Fancourier\Request\CreateAwb(); $request ->setParcels(1) ->setWeight(2) ->setReimbursement(125) ->setDeclaredValue(125) ->setNotes('testing notes') ->setContents('SKU-1, SKU-2') ->setRecipient("John Ivy") ->setPhone('0723000000') ->setRegion('Arad') ->setCity('Aciuta') ->setStreet('Str Lunga nr 1');
Response
$response = $fan->createAwb($request); if ($response->isOk()) { var_dump($response->getBody()); } else { var_dump($response->getErrorMessage()); }
Create AWB Bulk
Request
$batchRequest = new Fancourier\Request\CreateAwbBulk(); $request = new Fancourier\Request\CreateAwb(); $request ->setParcels(1) ->setWeight(2) ->setReimbursement(125) ->setDeclaredValue(125) ->setNotes('testing notes') ->setContents('SKU-1, SKU-2') ->setRecipient("John Ivy") ->setPhone('0723000000') ->setRegion('Arad') ->setCity('Aciuta') ->setStreet('Str Lunga nr 1') ; $batchRequest->append($request); $request ->setParcels(1) ->setWeight(1.5) ->setReimbursement(50) ->setDeclaredValue(50) ->setContents('SKU-7') ->setRecipient("Tester Testerson") ->setPhone('0722111000') ->setRegion('Sibiu') ->setCity('Sibiu') ->setStreet('Calea Bucuresti nr 1') ; $batchRequest->append($request); $response = $fan->createAwbBulk($batchRequest); if (!$response->isOk()) { //general error die($response->getErrorMessage()); } foreach ($response->getBody() as $awb) { echo $awb . "\n"; }
Track AWB
Request
$request = new Fancourier\Request\TrackAwb(); $request->setAwb('2150900120086');
Response
$response = $fan->trackAwb($request); if ($response->isOk()) { print_r($response->getBody()); } else { print_r($response->getErrorMessage()); }
Print AWB
Request
$request = new Fancourier\Request\PrintAwb(); $request->setAwb('2150900120086');
Response
$response = $fan->printAwb($request); if ($response->isOk()) { echo $response->getBody(); } else { var_dump($response->getErrorMessage()); }
Print AWB Html
Request
$request = new Fancourier\Request\PrintAwbHtml(); $request->setAwb('2150900120086');
Response
$response = $fan->printAwbHtml($request); if ($response->isOk()) { echo $response->getBody(); } else { var_dump($response->getErrorMessage()); }
Delete AWB
Request
$request = new Fancourier\Request\DeleteAwb(); $request->setAwb('2150900120086');
Response
$response = $fan->deleteAwb($request); if ($response->isOk()) { var_dump($response->getBody()); } else { var_dump($response->getErrorMessage()); }
Track awb in bulk
Request
$request = new Fancourier\Request\TrackAwbBulk(); $request->setAwbs(['2162900120047']);
Response
$response = $fan->trackAwbBulk($request); if ($response->isOk()) { print_r($response->getBody()); } else { var_dump("ERROR: " . $response->getErrorMessage()); }
Get cities
Request - There's no request for this method
Response - will return an array of cities (and other info)
$response = $fan->getCities(); if ($response->isOk()) { print_r($response->getBody()); } else { var_dump("ERROR: " . $response->getErrorMessage()); }
Get counties
Request - There's no request for this method
Response - will return an array of counties
$response = $fan->getCounties(); if ($response->isOk()) { print_r($response->getBody()); } else { var_dump("ERROR: " . $response->getErrorMessage()); }
Get services
Request - There's no request for this method
Response - will return an array with all services
$response = $fan->getServices(); if ($response->isOk()) { print_r($response->getBody()); } else { var_dump("ERROR: " . $response->getErrorMessage()); }
Features not implemented
Feel free to open a pull request for the following features:
- get pudo
- get streets
- external functions
- create carrier request
- delete carrier request
- summary
- get awb tracking events
- bank transfers
- orders functions
- branches
Contributing
Thank you for considering contributing to the Fancourier API, all pull requests are appreciated.
License
Fancourier Api is open-source software licensed under the MIT license.
firewizard/fancourier-api 适用场景与选型建议
firewizard/fancourier-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.18k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2019 年 06 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 firewizard/fancourier-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 firewizard/fancourier-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.18k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-06-25