Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #58 from 1415003719/http-proxy
Browse files Browse the repository at this point in the history
[ABU-22000] feat: curl opt
  • Loading branch information
Bossa573 authored Mar 23, 2023
2 parents 4ba2d35 + 49d20fa commit 8f76a01
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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, null, $curl_opt);
$trackings = new AfterShip\Trackings($key, null, $curl_opt);
$last_check_point = new AfterShip\LastCheckPoint($key, null, $curl_opt);
```

## Testing
1. Execute the file:
Expand Down
5 changes: 3 additions & 2 deletions src/Couriers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '', Requestable $request = null, $curlOpt = 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);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/EstimatedDeliveryDates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '', Requestable $request = null, $curlOpt = 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);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/LastCheckPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '', Requestable $request = null, $curlOpt = 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);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, Requestable $request = null, $curlOpt = 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);
}

/**
Expand Down
14 changes: 12 additions & 2 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand All @@ -67,6 +72,7 @@ function __construct($apiKey)
$this->apiSecret = $apiSecret;
$this->encryptionMethod = $encryptionMethod;
$this->encryptionPassword = $encryptionPassword;
$this->curlOpt = $curlOpt;
}

/**
Expand Down Expand Up @@ -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'];
Expand Down
6 changes: 3 additions & 3 deletions src/Trackings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '', Requestable $request = null, $curlOpt = 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);
}

/**
Expand Down

0 comments on commit 8f76a01

Please sign in to comment.