有关Stuart API提供的所有端点的完整文档,您可以访问Stuart API文档。
访问ChangElog
cd demo
docker build -t stuartphpdemo .
export PATH_TO_PROJECT= " /PUT/HERE/YOUR/PATH/TO/PROJECT/stuart-client-php "
docker run -v $PATH_TO_PROJECT /src:/app/vendor/stuartapp/stuart-client-php/src stuartphpdemo通过作曲家:
$ composer require stuartapp/stuart-client-php如果未安装作曲家,请安装它:
$ curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer将库添加到您的项目:
$ composer require stuartapp/stuart-client-php为了加载此库中的所有类,只需在Stuart代码的开头执行自动加载
<?php
require __DIR__ . ' /vendor/autoload.php ' ;
$ environment = Stuart Infrastructure Environment:: SANDBOX ;对于沙箱(测试环境,快递员是机器人)https://dashboard.sandbox.stuart.com/settings/api
用于生产(现实世界,真实的快递)https://dashboard.stuart.com/settings/api
$ environment = Stuart Infrastructure Environment:: SANDBOX ;
$ api_client_id = ' REPLACE_BY_YOU_CLIENT_ID_HERE ' ; // can be found here: https://admin.sandbox.stuart.com/client/api
$ api_client_secret = ' REPLACE_BY_YOUR_CLIENT_SECRET_HERE ' ; // can be found here: https://admin.sandbox.stuart.com/client/api
$ authenticator = new Stuart Infrastructure Authenticator ( $ environment , $ api_client_id , $ api_client_secret );
$ httpClient = new Stuart Infrastructure HttpClient( $ authenticator )
$ client = new Stuart Client ( $ httpClient );您也可以将自己的Guzzle Client实例传递到StuartHttpClient构造函数:
$ guzzleClient = new Guzzle Client ();
$ httpClient = new Stuart Infrastructure HttpClient ( $ authenticator , $ guzzleClient );如果您需要将中间件附加到Guzzle Client,这可能很有用。
强烈建议在身份验证过程中添加缓存机制。为此,只需扩展PsrSimpleCacheCacheInterface类并实现您自己的版本即可。
有一个基于可用的磁盘的缓存,供您使用。要使用它,只需修改身份验证类初始化并传递构造函数中的缓存实现:
$ diskCache = new Stuart Cache DiskCache ( " stuart_cache.txt " );
$ authenticator = new Stuart Infrastructure Authenticator ( $ environment , $ api_client_id , $ api_client_secret , $ diskCache );您可以通过将true值传递给这些类的最后一个构造函数参数来初始化diskcache,authenticator和httpclient类。
重要的是:即使您可以用最小的参数创建作业,我们强烈建议您填写尽可能多的信息,以确保交付过程顺利进行。
$ job = new Stuart Job ();
$ job -> addPickup ( ' 46 Boulevard Barbès, 75018 Paris ' );
$ job -> addDropOff ( ' 156 rue de Charonne, 75011 Paris ' )
-> setPackageType ( ' small ' );
$ client -> createJob ( $ job ); $ job = new Stuart Job ();
$ job -> addPickup ( ' 46 Boulevard Barbès, 75018 Paris ' )
-> setComment ( ' Wait outside for an employee to come. ' )
-> setContactCompany ( ' KFC Paris Barbès ' )
-> setContactFirstName ( ' Martin ' )
-> setContactLastName ( ' Pont ' )
-> setContactPhone ( ' +33698348756 ' );
$ job -> addDropOff ( ' 156 rue de Charonne, 75011 Paris ' )
-> setPackageType ( ' small ' )
-> setComment ( ' code: 3492B. 3e étage droite. Sonner à Durand. ' )
-> setContactCompany ( ' Durand associates. ' )
-> setContactFirstName ( ' Alex ' )
-> setContactLastName ( ' Durand ' )
-> setContactPhone ( ' +33634981209 ' )
-> setPackageDescription ( ' Pizza box. ' )
-> setClientReference ( ' 12345678ABCDE ' ); // Must be unique
$ client -> createJob ( $ job );有关工作计划的更多信息,您应该检查我们的API文档。
$ job = new Stuart Job ();
$ pickupAt = new DateTime ( ' now ' , new DateTimeZone ( ' Europe/Paris ' ));
$ pickupAt -> add ( new DateInterval ( ' PT2H ' ));
$ job -> addPickup ( ' 46 Boulevard Barbès, 75018 Paris ' )
-> setPickupAt ( $ pickupAt );
$ job -> addDropOff ( ' 156 rue de Charonne, 75011 Paris ' )
-> setPackageType ( ' small ' );
$ client -> createJob ( $ job );有关工作计划的更多信息,您应该检查我们的API文档。
请注意,此功能只能在一个下降时使用。
$ job = new Stuart Job ();
$ dropoffAt = new DateTime ( ' now ' , new DateTimeZone ( ' Europe/Paris ' ));
$ dropoffAt -> add ( new DateInterval ( ' PT2H ' ));
$ job -> addPickup ( ' 46 Boulevard Barbès, 75018 Paris ' );
$ job -> addDropOff ( ' 156 rue de Charonne, 75011 Paris ' )
-> setPackageType ( ' small ' )
-> setDropoffAt ( $ dropoffAt );
$ client -> createJob ( $ job ); $ job = new Stuart Job ();
$ job -> addPickup ( ' 46 Boulevard Barbès, 75018 Paris ' );
$ job -> addDropOff ( ' 156 rue de Charonne, 75011 Paris ' )
-> setPackageType ( ' small ' );
$ job -> setFleets ( array ( 1 ));
$ client -> createJob ( $ job ); $ job = new Stuart Job ();
$ job -> addPickup ( ' 46 Boulevard Barbès, 75018 Paris ' );
$ now = new DateTime ();
$ later = new DateTime ();
$ later = $ later -> modify ( ' +15 minutes ' );
$ job -> addDropOff ( ' 156 rue de Charonne, 75011 Paris ' )
-> setPackageType ( ' small ' )
-> setEndCustomerTimeWindowStart ( new DateTime ())
-> setEndCustomerTimeWindowEnd ( $ later );
$ client -> createJob ( $ job ); $ job = new Stuart Job ();
$ job -> addPickup ( ' 46 Boulevard Barbès, 75018 Paris ' )
-> setComment ( ' Wait outside for an employee to come. ' )
-> setContactCompany ( ' KFC Paris Barbès ' )
-> setContactFirstName ( ' Martin ' )
-> setContactLastName ( ' Pont ' )
-> setContactPhone ( ' +33698348756 ' );
$ job -> addDropOff ( ' 156 rue de Charonne, 75011 Paris ' )
-> setPackageType ( ' small ' )
-> setComment ( ' code: 3492B. 3e étage droite. Sonner à Durand. ' )
-> setContactCompany ( ' Durand associates. ' )
-> setContactFirstName ( ' Alex ' )
-> setContactLastName ( ' Durand ' )
-> setContactPhone ( ' +33634981209 ' )
-> setPackageDescription ( ' Red packet. ' )
-> setClientReference ( ' 12345678ABCDE ' ); // Must be unique;
$ job -> addDropOff ( ' 12 avenue claude vellefaux, 75010 Paris ' )
-> setPackageType ( ' small ' )
-> setComment ( ' code: 92A42. 2e étage gauche ' )
-> setContactFirstName ( ' Maximilien ' )
-> setContactLastName ( ' Lebluc ' )
-> setContactPhone ( ' +33632341209 ' )
-> setPackageDescription ( ' Blue packet. ' )
-> setClientReference ( ' ABCDE213124 ' ); // Must be unique
$ client -> createJob ( $ job );成功创建了一项工作后,您可以以这种方式检索:
$ jobId = 126532 ;
$ job = $ client -> getJob ( $ jobId );或创建新工作时:
$ job = new Stuart Job ();
$ job -> addPickup ( ' 46 Boulevard Barbès, 75018 Paris ' );
$ job -> addDropOff ( ' 156 rue de Charonne, 75011 Paris ' )
-> setPackageType ( ' small ' );
$ jobWithRoute = $ client -> createJob ( $ job );
$ jobWithRoute -> getDeliveries (); Stuart API确定代表您的最佳路由,这就是为什么getDeliveries()方法将在尚未创建作业时返回一个空数组的原因。创建工作后, getDeliveries()方法将立即返回Delivery数组。
成功创建了一项工作后,您可以以这种方式取消它:
$ jobId = 126532 ;
$ result = $ client -> cancelJob ( $ jobId );如果取消工作,结果将使布尔值true 。如果有错误,它将包含一个错误对象。
有关取消如何工作的更多详细信息,请参阅我们的专用文档部分。
在创建工作之前,您可以验证它(控制输送区域和地址格式)。验证工作是可选的,不会阻止您创建工作。
$ job = new Stuart Job ();
$ job -> addPickup ( ' 46 Boulevard Barbès, 75018 Paris ' );
$ job -> addDropOff ( ' 156 rue de Charonne, 75011 Paris ' )
-> setPackageType ( ' small ' );
$ result = $ client -> validateJob ( $ job );如果作业有效,则结果将使布尔值true 。如果有错误,它将包含一个错误对象。
我们鼓励验证一个地址,以了解我们是否可以在那里取货 /运送。电话号码仅适用于该地址足够具体的地方。
$ client -> validatePickupAddress ( ' Pau Claris, 08037 Barcelona ' , ' +34677777777 ' );
$ client -> validatePickupAddress ( ' Pau Claris 186, 08037 Barcelona ' );
$ client -> validateDropoffAddress ( ' Pau Claris, 08037 Barcelona ' , ' +34677777777 ' );一旦成功创建了交货,您就可以以这种方式取消它:
$ deliveryId = 126532 ;
$ result = $ client -> cancelDelivery ( $ deliveryId );在创建工作之前,您可以要求定价。要求定价是可选的,并不能阻止您创建工作。
$ job = new Stuart Job ();
$ job -> addPickup ( ' 46 Boulevard Barbès, 75018 Paris ' );
$ job -> addDropOff ( ' 156 rue de Charonne, 75011 Paris ' )
-> setPackageType ( ' small ' );
$ pricing = $ client -> getPricing ( $ job );
$ pricing -> amount ; // example: 11.5
$ pricing -> currency ; // example: "EUR"在创建工作之前,您可以要求估计到达拾取位置的时间(以秒为单位)。要求ETA是可选的,不会阻止您创建工作。
$ job = new Stuart Job ();
$ job -> addPickup ( ' 46 Boulevard Barbès, 75018 Paris ' );
$ job -> addDropOff ( ' 156 rue de Charonne, 75011 Paris ' )
-> setPackageType ( ' small ' );
$ eta = $ client -> getEta ( $ job );
$ eta -> eta ; // example: 672您也可以自行发送请求,而无需依赖StuartClient 。它允许您使用StuartClient上尚未可用的端点并享受StuartAuthenticator 。
$ apiResponse = $ httpClient -> performGet ( ' /v2/jobs?page=1 ' );
$ apiResponse -> success ();
$ apiResponse -> getBody ();