|
1 | 1 | # twitch-api-php
|
2 | 2 |
|
3 |
| -A Twitch Kraken API client for PHP. |
| 3 | +# New Twitch API (Helix) |
| 4 | + |
| 5 | +A New Twitch API (Helix) client for PHP. The code for the new API is all contained within `src/NewApi/`. This is because the New API code is meant to be separate from the old Kraken API code, such that in the future, when Kraken is no longer available, the old Kraken code can be removed without affecting the new API code. Additionally, keeping them separate allows for existing code using the Kraken part of this library to continue to function, untouched by the new code. |
| 6 | + |
| 7 | +The New Twitch API client is still being developed and is currently incomplete. The endpoints that are implemented are usable, but not all endpoints have been implemented yet. If an endpoint you need is missing, incomplete, or not working correctly, please report it or fix it if you can and create a PR for it. |
| 8 | + |
| 9 | +## Usage |
| 10 | + |
| 11 | +Everything stems from the `NewTwitchApi` class. However, if you want to individually instantiate `UsersApi`, `OauthApi`, etc. you are free to do so. |
| 12 | + |
| 13 | +The API calls generally return an object implementing `ResponseInterface`. Since you are getting the full `Response` object, you'll need to handle its contents, e.g. by decoding then into an object with `json_decode()`. This library does not assume this is what you want to do, so it does not do this for you automatically. This library simply acts as a middleman between your code and Twitch, providing you with the raw responses the Twitch API returns. |
| 14 | + |
| 15 | +The individual API classes that can be called from `NewTwitchApi` correspond to the [New Twitch API documentation](https://dev.twitch.tv/docs/api/). `OauthApi` is for Oauth calls. `WebhooksSubscriptionApi` is for subscribing/unsubscribing to webhooks. The rest of the API classes are based on the resources listed [here](https://dev.twitch.tv/docs/api/reference/). The methods in the classes generally correspond to the endpoints for each resource. The naming convention was chosen to try and match the Twitch documentation. Each primary endpoint method (not convenience or helper methods) should have an `@link` annotation with a URL to that endpoint's specific documentation. |
| 16 | + |
| 17 | +## Examples |
| 18 | + |
| 19 | +Getting a user's information via their access token: |
| 20 | + |
| 21 | +```php |
| 22 | +// Assuming you already have the access token. |
| 23 | +$accessToken = 'the token'; |
| 24 | + |
| 25 | +// The Guzzle client used can be the included `HelixGuzzleClient` class, for convenience. |
| 26 | +// You can also use a mock, fake, or other double for testing, of course. |
| 27 | +$helixGuzzleClient = new HelixGuzzleClient(); |
| 28 | + |
| 29 | +// Instantiate NewTwitchApi. Can be done in a service layer and injected as well. |
| 30 | +$newTwitchApi = new NewTwitchApi($helixGuzzleClient, $clientId, $clientSecret); |
| 31 | + |
| 32 | +try { |
| 33 | + // Make the API call. A ResponseInterface object is returned. |
| 34 | + $response = $newTwitchApi->getUsersApi()->getUserByAccessToken($accessToken); |
| 35 | +} (catch GuzzleException $e) { |
| 36 | + // Handle error appropriately for your application |
| 37 | +} |
| 38 | + |
| 39 | +// Get and decode the actual content sent by Twitch. |
| 40 | +$responseContent = json_decode($response->getBody()->getContents()); |
| 41 | + |
| 42 | +// Return the first (or only) user. |
| 43 | +return $responseContent->data[0]; |
| 44 | +``` |
| 45 | + |
| 46 | +### CLI Test Client |
| 47 | + |
| 48 | +In order to make testing the New Twitch API code easier, there is an interactive CLI script that can be run. This is found at `bin/new-api-cli-test-client.php`. |
| 49 | + |
| 50 | +To run it, execute `./bin/new-api-cli-test-client.php <client-id> <client-secret>`, passing in your client ID and secret, respectively. The script will interactively walk you through the rest. You'll be prompted for which API endpoint you'd like to call. Then, you'll be prompted for any parameters that are available for that call. After the API call is made, you're presented with the URI of the request followed by the body of the response. |
| 51 | + |
| 52 | +Here's an example of the CLI client in action, getting the game information for Minecraft and then validating an invalid access token. |
| 53 | + |
| 54 | +```bash |
| 55 | +$ ./bin/new-api-cli-test-client.php REDACTED_CLIENT_ID REDACTED_CLIENT_SECRET |
| 56 | +Twitch API Testing Tool |
| 57 | + |
| 58 | +Which endpoint would you like to call? |
| 59 | +0) Quit |
| 60 | +1) Validate an Access Token |
| 61 | +2) Refresh an Access Token |
| 62 | +3) Get Games |
| 63 | +4) Get Streams |
| 64 | +5) Get Users |
| 65 | +6) Get Users Follows |
| 66 | +Choice: 3 |
| 67 | + |
| 68 | +Get Games |
| 69 | +IDs (separated by commas): |
| 70 | +Names (separated by commas): Minecraft |
| 71 | + |
| 72 | +games?name=Minecraft |
| 73 | + |
| 74 | +{ |
| 75 | + "data": [ |
| 76 | + { |
| 77 | + "id": "27471", |
| 78 | + "name": "Minecraft", |
| 79 | + "box_art_url": "https:\/\/static-cdn.jtvnw.net\/ttv-boxart\/Minecraft-{width}x{height}.jpg" |
| 80 | + } |
| 81 | + ] |
| 82 | +} |
| 83 | + |
| 84 | +Which endpoint would you like to call? |
| 85 | +0) Quit |
| 86 | +1) Validate an Access Token |
| 87 | +2) Refresh an Access Token |
| 88 | +3) Get Games |
| 89 | +4) Get Streams |
| 90 | +5) Get Users |
| 91 | +6) Get Users Follows |
| 92 | +Choice: 1 |
| 93 | + |
| 94 | +Validate an Access Token |
| 95 | +Access token: foobar |
| 96 | + |
| 97 | +/oauth2/validate |
| 98 | + |
| 99 | +{ |
| 100 | + "status": 401, |
| 101 | + "message": "invalid access token" |
| 102 | +} |
| 103 | + |
| 104 | +Which endpoint would you like to call? |
| 105 | +0) Quit |
| 106 | +1) Validate an Access Token |
| 107 | +2) Refresh an Access Token |
| 108 | +3) Get Games |
| 109 | +4) Get Streams |
| 110 | +5) Get Users |
| 111 | +6) Get Users Follows |
| 112 | +Choice: 0 |
| 113 | + |
| 114 | +Quit |
| 115 | +$ |
| 116 | +``` |
| 117 | + |
| 118 | +## Developer Tools |
| 119 | + |
| 120 | +### PHP Coding Standards Fixer |
| 121 | + |
| 122 | +[PHP Coding Standards Fixer](https://cs.sensiolabs.org/) (`php-cs-fixer`) has been added, specifically for the New Twitch API code. A configuration file for it can be found in `.php_cs.dist`. The ruleset is left at default (PSR-2 at this time). The configuration file mostly just limits it's scope to only the New Twitch API code. |
| 123 | + |
| 124 | +You can run the fixer with `vendor/bin/php-cs-fixer fix`. However, the easiest way to run the fixer is with the provided git hook. |
| 125 | + |
| 126 | +### Git pre-commit Hook |
| 127 | + |
| 128 | +In `bin/git/hooks`, you'll find a `pre-commit` hook that you can add to git that will automatically run the `php-cs-fixer` everytime you commit. The result is that, after the commit is made, any changes that fixer has made are left as unstaged changes. You can review them, then add and commit them. |
| 129 | + |
| 130 | +To install the hook, go to `.git/hooks` and `ln -s ../../bin/git/hooks/pre-commit`. |
| 131 | + |
| 132 | +## API Documentation |
| 133 | + |
| 134 | +The New Twitch API docs can be found [here](https://dev.twitch.tv/docs/api/). |
| 135 | + |
| 136 | +## License |
| 137 | + |
| 138 | +Distributed under the [MIT](LICENSE) license. |
| 139 | + |
| 140 | +--- |
| 141 | +--- |
| 142 | +--- |
| 143 | + |
| 144 | +# Kraken |
| 145 | + |
| 146 | +A Twitch Kraken API client for PHP. This is the old API, which is deprecated and will be deleted soon. Please use Helix instead. If something is missing from the Helix API, please add it or request it. |
| 147 | + |
| 148 | +The documentation below is left for legacy purposes, until Kraken support is removed. |
4 | 149 |
|
5 | 150 | [](https://travis-ci.org/nicklaw5/twitch-api-php)
|
6 | 151 |
|
|
0 commit comments