shimango / gophr
用于 Gophr 商业 API 的 PHP 客户端
Requires
- php: >=8.0
- guzzlehttp/guzzle: ^7.5
- spatie/data-transfer-object: ^2.8
Requires (Dev)
- dg/bypass-finals: ^1.4
- phpunit/php-code-coverage: ^9
- phpunit/phpunit: ^9
- rector/rector: ^0.15.4
README
有关 Gophr Restfull API 的文档可在以下位置找到: https://developers.gophr.com/docs
需求
Gophr 商业 API 使用基于 API 密钥的认证。为了使用此客户端,您需要从 Gophr 获取一个 API 密钥。在此了解如何获取它:https://developers.gophr.com/docs/authorisation
安装
使用 composer
composer require shimango/gophr
或者将以下内容添加到您的项目中的 composer.json 文件中
{ "require": { "shimango/gophr": "^2.0.0" } }
或者,从 github 下载此包,然后在包含 composer.json 文件的目录中运行 composer install
以生成自动加载器,然后通过以下方式使用自动加载器:
require_once "vendor/autoload.php"
使用方法
此库中的两个主要类是 Shimango\Gophr\Common\Configuration
和 Shimango\Gophr\Client
Configuration
类用于设置调用 Rest API 时所需的全部变量。配置参数通过该类的构造函数传递或在设置方法中设置。这些是
@param string $apiKey - The API key obtained from Gophr @param bool $isSandbox (Default false) - If true the Gophr sandbox environment will be used @param string $apiVersion (Default "v2") - The API version to be used. Currently, only v2 is supported @param int|null $proxyPort (Default null) - If the calls are sent via proxy the port number can be set here @param bool $proxyVerifySSL (Default false) - Sets verify proxy SSL to true if using a proxy is being used
Client
是此库中的主要类,具有允许轻松调用 Gophr 商业 API 的函数。以下是查询 gophr API 以获取作业列表的简单示例
use Shimango\Gophr\Client; use Shimango\Gophr\Common\Configuration; $config = new Configuration('YOUR_API_KEY'); $gophr = new Client($config); $response = $gophr->listJobs(); var_dump($response->getContentsArray());
use Shimango\Gophr\Client; use Shimango\Gophr\Common\Configuration; $isSandbox = true; $config = new Configuration('YOUR_API_KEY', $isSandbox); $gophr = new Client($config); $page = 2; // The page number to be returned $count = 15; // The number of jobs returned per page $include_finished = false; $response = $gophr->listJobs($page, $count, $include_finished); var_dump($response->getContentsArray());
作业
作业是一系列将被分配给快递员并在同一行程中完成的配送。此库提供了以下方法与作业交互
// Creates a job with a single pickup point and multiple drop off locations. createJob(array $jobData) // Get the details of a job with the given job id. getJob(string $jobId) // Allows a job to be updated to confirmed. updateJob(string $jobId, array $jobData) // Cancels a job CancelJob(string $jobId, array $jobData) // Get all jobs associated with the API key used. listJobs(int $page = 1 ,int $count = 15, bool $include_finished = false)
配送
配送代表一次提货-投递组合以及与之相关的任何包裹。此库提供了以下方法与配送交互
// Creates a delivery for an existing job. The delivery consists of a pickup, dropoff and collection of parcels. // Note that adding deliveries to a job is not allowed once the job has been confirmed. createDelivery(string $jobId, array $deliveryData) // Get the details of a delivery. getDelivery(string $jobId, string $deliveryId) // Updates a delivery. Parcels may also be added / edited via this method. // Note that updating deliveries is not allowed once the job has been confirmed. updateDelivery(string $jobId, string $deliveryId, array $deliveryData) // Get a list of deliveries for a given job. listDeliveries(string $jobId) // Cancels a delivery. Note that cancelling deliveries is not allowed once the job has been confirmed. cancelDelivery(string $jobId, string $deliveryId, array $deliveryData) // Progresses a delivery status. This functionality is not available in production. progressDeliveryStatus(string $jobId, string $deliveryId)
包裹
包裹代表配送中的单个包裹。包裹属于配送,配送可以有多个包裹。此库提供了以下方法与包裹交互
// Creates a parcel for the given delivery. // Note that adding parcels to a delivery is not allowed once the job has been confirmed. createParcel(string $jobId, string $deliveryId, array $parcelData) // Get the details of a given parcel. getParcel(string $jobId, string $deliveryId, string $parcelId) // Updates a parcel. Note that updating parcels is not allowed once the job has been confirmed. // Note that updating parcels is not allowed once the job has been confirmed. updateParcel(string $jobId, string $deliveryId, string $parcelId, array $deliveryData) // Removes a parcel from a delivery. If the parcel count is 0 after deletion then the delivery will be cancelled. // Note that deleting parcels is not allowed once the job has been confirmed. deleteParcel(string $jobId, string $deliveryId, string $parcelId) // Returns a collection of parcels for the given delivery. listParcels(string $jobId, string $deliveryId)
上述所有方法都来自主要的 \Shimango\Gophr\Client
类。或者,可以使用单个资源代替。资源对象将仅具有直接影响该资源的函数。这些资源是
\Shimango\Gophr\Resources\Job
\Shimango\Gophr\Resources\Delivery
\Shimango\Gophr\Resources\Parcel
通过此库对 Gophr API 的所有调用都将返回 GophrResponse 对象。此类符合 PSR-7 规范,并提供了多种常见响应方法,包括两个帮助获取响应体的方法。这些是
getContentsArray() // Returns the response body as an array getContentsObject() // Returns the response body as an object
请求和响应有效载荷
每个 API 调用的请求和响应有效载荷示例可在以下位置找到:https://developers.gophr.com/reference
测试
composer test