From 7b27a3f84ae8640968fdc651865f6b2c381f739d Mon Sep 17 00:00:00 2001 From: Watson Zuo Date: Wed, 22 Mar 2023 17:16:28 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20curl=20opt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit support curl opt --- CHANGELOG.md | 4 ++++ README.md | 12 ++++++++++++ src/Couriers.php | 5 +++-- src/EstimatedDeliveryDates.php | 6 +++--- src/LastCheckPoint.php | 6 +++--- src/Notifications.php | 5 +++-- src/Request.php | 14 ++++++++++++-- src/Trackings.php | 6 +++--- 8 files changed, 43 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d5ba00..0da96a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [5.3.0] - 2023-03-22 +### Added +- Add CURL OPT for API request + ## [5.2.0] - 2023-03-17 ### Added - Add API: POST `trackings/{slug}/{tracking_number}/mark-as-completed` diff --git a/README.md b/README.md index b7b0783..acfef4f 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,18 @@ $trackings = new AfterShip\Trackings($key); $last_check_point = new AfterShip\LastCheckPoint($key); ``` +Custom CURL opt +```php +require 'vendor/autoload.php'; + +$api_key = 'AFTERSHIP API KEY'; + +$curl_opt = [CURLOPT_PROXY => 'http://example.com', 'CURLOPT_PROXYPORT' => '8080'] + +$couriers = new AfterShip\Couriers($key, $curl_opt); +$trackings = new AfterShip\Trackings($key, $curl_opt); +$last_check_point = new AfterShip\LastCheckPoint($key, $curl_opt); +``` ## Testing 1. Execute the file: diff --git a/src/Couriers.php b/src/Couriers.php index e398d1a..c136e32 100644 --- a/src/Couriers.php +++ b/src/Couriers.php @@ -15,15 +15,16 @@ class Couriers extends BackwardCompatible * @param string $apiKey The AfterShip API Key. * * @param Requestable|null $request + * @param array|null the request curl opt * @throws AfterShipException */ - public function __construct($apiKey = '', Requestable $request = null) + public function __construct($apiKey = '', $curlOpt = null, Requestable $request = null) { if (empty($apiKey)) { throw new AfterShipException('API Key is missing'); } - $this->request = $request ? $request : new Request($apiKey); + $this->request = $request ? $request : new Request($apiKey, $curlOpt); } /** diff --git a/src/EstimatedDeliveryDates.php b/src/EstimatedDeliveryDates.php index b82c6e5..11ba3f5 100644 --- a/src/EstimatedDeliveryDates.php +++ b/src/EstimatedDeliveryDates.php @@ -8,17 +8,17 @@ class EstimatedDeliveryDates extends BackwardCompatible /** * @param string $apiKey The AfterShip API Key. - * + * @param array|null the request curl opt * @param Requestable $request * @throws AfterShipException */ - public function __construct($apiKey = '', Requestable $request = null) + public function __construct($apiKey = '', $curlOpt = null, Requestable $request = null) { if (empty($apiKey)) { throw new AfterShipException('API Key is missing'); } - $this->request = $request ? $request : new Request($apiKey); + $this->request = $request ? $request : new Request($apiKey, $curlOpt); } /** diff --git a/src/LastCheckPoint.php b/src/LastCheckPoint.php index 44ea8ce..59c8015 100644 --- a/src/LastCheckPoint.php +++ b/src/LastCheckPoint.php @@ -16,17 +16,17 @@ class LastCheckPoint extends BackwardCompatible * The LastCheckPoint constructor. * * @param string $apiKey The AfterShip API Key. - * + * @param array|null the request curl opt * @param Requestable|null $request * @throws AfterShipException */ - public function __construct($apiKey = '', Requestable $request = null) + public function __construct($apiKey = '', $curlOpt = null, Requestable $request = null) { if (empty($apiKey)) { throw new AfterShipException('API Key is missing'); } - $this->request = $request ? $request : new Request($apiKey); + $this->request = $request ? $request : new Request($apiKey, $curlOpt); } /** diff --git a/src/Notifications.php b/src/Notifications.php index afc2205..e81f3f9 100644 --- a/src/Notifications.php +++ b/src/Notifications.php @@ -13,16 +13,17 @@ class Notifications /** * Notifications constructor. * @param $apiKey + * @param array|null the request curl opt * @param Requestable|null $request * @throws AfterShipException */ - public function __construct($apiKey, Requestable $request = null) + public function __construct($apiKey, $curlOpt = null, Requestable $request = null) { if (empty($apiKey)) { throw new AfterShipException('API Key is missing'); } - $this->request = $request ? $request : new Request($apiKey); + $this->request = $request ? $request : new Request($apiKey, $curlOpt); } /** diff --git a/src/Request.php b/src/Request.php index 8e90f4f..d135d5c 100644 --- a/src/Request.php +++ b/src/Request.php @@ -36,12 +36,17 @@ class Request implements Requestable * @var string */ private $encryptionPassword = ''; + /** + * + * @var array + */ + private $curlOpt; /** * Request constructor. * * @param $apiKey */ - function __construct($apiKey) + function __construct($apiKey, $curlOpt) { $apiSecret = ''; $encryptionMethod = ''; @@ -67,6 +72,7 @@ function __construct($apiKey) $this->apiSecret = $apiSecret; $this->encryptionMethod = $encryptionMethod; $this->encryptionPassword = $encryptionPassword; + $this->curlOpt = $curlOpt; } /** @@ -101,11 +107,15 @@ public function send($method, $path, array $data = []) private function call($method, $parameters = []) { $curl = curl_init(); + // user custom curl opt + if (!empty($this->curlOpt)) { + curl_setopt_array($curl, $this->curlOpt); + } $curlParams = [ CURLOPT_RETURNTRANSFER => true, CURLOPT_URL => $parameters['url'], CURLOPT_CUSTOMREQUEST => $method, - CURLOPT_HTTPHEADER => $parameters['headers'] + CURLOPT_HTTPHEADER => $parameters['headers'], ]; if ($method != 'GET') { $curlParams[CURLOPT_POSTFIELDS] = $parameters['body']; diff --git a/src/Trackings.php b/src/Trackings.php index f7d82e0..fce59fd 100644 --- a/src/Trackings.php +++ b/src/Trackings.php @@ -10,17 +10,17 @@ class Trackings extends BackwardCompatible * The Trackings constructor. * * @param string $apiKey The AfterShip API Key. - * + * @param array|null the request curl opt * @param Requestable $request * @throws AfterShipException */ - public function __construct($apiKey = '', Requestable $request = null) + public function __construct($apiKey = '', $curlOpt = null, Requestable $request = null) { if (empty($apiKey)) { throw new AfterShipException('API Key is missing'); } - $this->request = $request ? $request : new Request($apiKey); + $this->request = $request ? $request : new Request($apiKey, $curlOpt); } /** From fe29b8f674901a1c7d00822c7573b37b256725c1 Mon Sep 17 00:00:00 2001 From: Watson Zuo Date: Wed, 22 Mar 2023 17:28:38 +0800 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20curl=20opt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit update curl opt --- src/Couriers.php | 2 +- src/EstimatedDeliveryDates.php | 2 +- src/LastCheckPoint.php | 2 +- src/Notifications.php | 2 +- src/Trackings.php | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Couriers.php b/src/Couriers.php index c136e32..3311098 100644 --- a/src/Couriers.php +++ b/src/Couriers.php @@ -18,7 +18,7 @@ class Couriers extends BackwardCompatible * @param array|null the request curl opt * @throws AfterShipException */ - public function __construct($apiKey = '', $curlOpt = null, Requestable $request = null) + public function __construct($apiKey = '', Requestable $request = null, $curlOpt = null) { if (empty($apiKey)) { throw new AfterShipException('API Key is missing'); diff --git a/src/EstimatedDeliveryDates.php b/src/EstimatedDeliveryDates.php index 11ba3f5..106a250 100644 --- a/src/EstimatedDeliveryDates.php +++ b/src/EstimatedDeliveryDates.php @@ -12,7 +12,7 @@ class EstimatedDeliveryDates extends BackwardCompatible * @param Requestable $request * @throws AfterShipException */ - public function __construct($apiKey = '', $curlOpt = null, Requestable $request = null) + public function __construct($apiKey = '', Requestable $request = null, $curlOpt = null) { if (empty($apiKey)) { throw new AfterShipException('API Key is missing'); diff --git a/src/LastCheckPoint.php b/src/LastCheckPoint.php index 59c8015..10975d4 100644 --- a/src/LastCheckPoint.php +++ b/src/LastCheckPoint.php @@ -20,7 +20,7 @@ class LastCheckPoint extends BackwardCompatible * @param Requestable|null $request * @throws AfterShipException */ - public function __construct($apiKey = '', $curlOpt = null, Requestable $request = null) + public function __construct($apiKey = '', Requestable $request = null, $curlOpt = null) { if (empty($apiKey)) { throw new AfterShipException('API Key is missing'); diff --git a/src/Notifications.php b/src/Notifications.php index e81f3f9..e60c3f9 100644 --- a/src/Notifications.php +++ b/src/Notifications.php @@ -17,7 +17,7 @@ class Notifications * @param Requestable|null $request * @throws AfterShipException */ - public function __construct($apiKey, $curlOpt = null, Requestable $request = null) + public function __construct($apiKey, Requestable $request = null, $curlOpt = null) { if (empty($apiKey)) { throw new AfterShipException('API Key is missing'); diff --git a/src/Trackings.php b/src/Trackings.php index fce59fd..fa6c380 100644 --- a/src/Trackings.php +++ b/src/Trackings.php @@ -14,7 +14,7 @@ class Trackings extends BackwardCompatible * @param Requestable $request * @throws AfterShipException */ - public function __construct($apiKey = '', $curlOpt = null, Requestable $request = null) + public function __construct($apiKey = '', Requestable $request = null, $curlOpt = null) { if (empty($apiKey)) { throw new AfterShipException('API Key is missing'); From 49d20fa40d5e24763f75a171689c1c7af0243791 Mon Sep 17 00:00:00 2001 From: Watson Zuo Date: Wed, 22 Mar 2023 18:02:55 +0800 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20curl=20opt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit update --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index acfef4f..5df474d 100644 --- a/README.md +++ b/README.md @@ -92,9 +92,9 @@ $api_key = 'AFTERSHIP API KEY'; $curl_opt = [CURLOPT_PROXY => 'http://example.com', 'CURLOPT_PROXYPORT' => '8080'] -$couriers = new AfterShip\Couriers($key, $curl_opt); -$trackings = new AfterShip\Trackings($key, $curl_opt); -$last_check_point = new AfterShip\LastCheckPoint($key, $curl_opt); +$couriers = new AfterShip\Couriers($key, null, $curl_opt); +$trackings = new AfterShip\Trackings($key, null, $curl_opt); +$last_check_point = new AfterShip\LastCheckPoint($key, null, $curl_opt); ``` ## Testing