diff --git a/readme.md b/readme.md index 4f20ff4..ab87ee2 100644 --- a/readme.md +++ b/readme.md @@ -90,17 +90,24 @@ Expected settings are as follows: ```php use Noweh\TwitterApi\Client; -$settings['account_id'] -$settings['access_token'], -$settings['access_token_secret'], -$settings['consumer_key'], -$settings['consumer_secret'], -$settings['bearer_token'], -$settings['free_mode'] = false; // Optional +$settings = [ + 'account_id' => 'YOUR_ACCOUNT_ID', + 'access_token' => 'YOUR_ACCESS_TOKEN', + 'access_token_secret' => 'YOUR_TOKEN_SECRET', + 'consumer_key' => 'YOUR_CONSUMER_KEY', + 'consumer_secret' => 'YOUR_CONSUMER_SECRET', + 'bearer_token' => 'YOUR_BEARER_TOKEN', + 'free_mode' => false, // Optional + 'api_base_uri' => 'https://api.twitter.com/2/', // Optional +]; $client = new Client($settings); ``` +By changing the value of `'api_base_uri'` you can have the requests target a different server, for instance, a simulated one, thus making testing your application in isolation easier. + +For a quick mock server setup you can use [mockoon](https://mockoon.com/). + ### API Functionality All API calls are triggered when the `performRequest()` method is invoked. Depending on the context, this method can either be empty or contain data that will be sent as PostData (refer to examples of each call below). diff --git a/src/AbstractController.php b/src/AbstractController.php index e819024..cb8e2fc 100644 --- a/src/AbstractController.php +++ b/src/AbstractController.php @@ -62,6 +62,9 @@ abstract class AbstractController /** @var array $post_body */ protected array $post_body = []; + /** @var string> $baseUri */ + private string $api_base_uri; + /** * Creates object. Requires an array of settings. * @param array $settings @@ -74,6 +77,12 @@ public function __construct(array $settings) $this->parseSettings($settings); } + private function getAPIBaseURI(): string + { + return $this->api_base_uri; + } + + /** * Perform the request to Twitter API * @param array $postData @@ -90,7 +99,7 @@ public function performRequest(array $postData = [], $withHeaders = false) if ($this->auth_mode === 0) { // Bearer Token // Inject the Bearer token header - $client = new Client(['base_uri' => self::API_BASE_URI]); + $client = new Client(['base_uri' => $this->getAPIBaseURI()]); $headers['Authorization'] = 'Bearer ' . $this->bearer_token; } elseif ($this->auth_mode === 1) { // OAuth 1.0a User Context // Insert Oauth1 middleware @@ -103,7 +112,7 @@ public function performRequest(array $postData = [], $withHeaders = false) ]); $stack->push($middleware); $client = new Client([ - 'base_uri' => self::API_BASE_URI, + 'base_uri' => $this->getAPIBaseURI(), 'handler' => $stack, 'auth' => 'oauth' ]); @@ -205,6 +214,7 @@ private function parseSettings(array $settings): void $this->access_token = $settings['access_token']; $this->access_token_secret = $settings['access_token_secret']; $this->free_mode = $settings['free_mode'] ?? false; + $this->api_base_uri = $settings['api_base_uri'] ?? self::API_BASE_URI; } /**