|
1 | 1 | # intercom-php |
2 | 2 |
|
3 | | -[](https://codeclimate.com/repos/537da4a7e30ba062b101be9c/feed) [](https://circleci.com/gh/intercom/intercom-php) |
| 3 | +[](https://codeclimate.com/repos/537da4a7e30ba062b101be9c/feed) [](https://circleci.com/gh/intercom/intercom-php) |
5 | 4 |
|
6 | 5 | Official PHP bindings to the Intercom API |
7 | 6 |
|
8 | 7 | ## Installation |
9 | 8 |
|
10 | 9 | This library supports PHP 7.1 and later |
11 | 10 |
|
12 | | -The recommended way to install intercom-php is through [Composer](https://getcomposer.org): |
13 | | - |
14 | | -First, install Composer: |
15 | | - |
16 | | -```sh |
17 | | -curl -sS https://getcomposer.org/installer | php |
18 | | -``` |
| 11 | +This library uses [HTTPPlug](https://github.com/php-http/httplug) as HTTP client. HTTPPlug is an abstraction that allows this library to support many different HTTP Clients. Therefore, you need to provide it with an adapter for the HTTP library you prefer. You can find all the available adapters [in Packagist](https://packagist.org/providers/php-http/client-implementation). This documentation assumes you use the Guzzle6 Client, but you can replace it with any adapter that you prefer. |
19 | 12 |
|
20 | | -Next, install the latest intercom-php: |
| 13 | +The recommended way to install intercom-php is through [Composer](https://getcomposer.org): |
21 | 14 |
|
22 | 15 | ```sh |
23 | | -php composer.phar require intercom/intercom-php |
24 | | -``` |
25 | | - |
26 | | -Finally, you need to require the library in your PHP application: |
27 | | - |
28 | | -```php |
29 | | -require "vendor/autoload.php"; |
| 16 | +composer require intercom/intercom-php php-http/guzzle6-adapter |
30 | 17 | ``` |
31 | 18 |
|
32 | 19 | ## Clients |
33 | 20 |
|
34 | | -For OAuth or Access Tokens use: |
| 21 | +Initialize your client using your access token: |
35 | 22 |
|
36 | 23 | ```php |
37 | 24 | use Intercom\IntercomClient; |
38 | 25 |
|
39 | | -$client = new IntercomClient('<insert_token_here>', null); |
| 26 | +$client = new IntercomClient('<insert_token_here>'); |
40 | 27 | ``` |
41 | 28 |
|
42 | 29 | > If you already have an access token you can find it [here](https://app.intercom.com/a/apps/_/developer-hub). If you want to create or learn more about access tokens then you can find more info [here](https://developers.intercom.com/building-apps/docs/authorization#section-access-tokens). |
43 | 30 | > |
44 | 31 | > If you are building a third party application you can get your OAuth token by [setting-up-oauth](https://developers.intercom.com/building-apps/docs/authorization#section-oauth) for Intercom. |
45 | 32 |
|
| 33 | +For most use cases the code snippet above should suffice. However, if needed, you can customize the Intercom client as follows: |
| 34 | + |
| 35 | +```php |
| 36 | +use Intercom\IntercomClient; |
| 37 | + |
| 38 | +$client = new IntercomClient('<insert_token_here>', null, ['Custom-Header' => 'value']); |
| 39 | + |
| 40 | +$client->setHttpClient($myCustomHttpClient); // $myCustomHttpClient implements Psr\Http\Client\ClientInterface |
| 41 | +$client->setRequestFactory($myCustomRequestFactory); // $myCustomRequestFactory implements Http\Message\RequestFactory |
| 42 | +$client->setUriFactory($myCustomUriFactory); // $myCustomUriFactory implements Http\Message\UriFactory |
| 43 | +``` |
| 44 | + |
46 | 45 | ## Users |
47 | 46 |
|
48 | 47 | ```php |
@@ -458,19 +457,15 @@ while (!empty($resp->scroll_param) && sizeof($resp->users) > 0) { |
458 | 457 |
|
459 | 458 | ## Exceptions |
460 | 459 |
|
461 | | -Exceptions are handled by [Guzzle](https://github.com/guzzle/guzzle). |
| 460 | +Exceptions are handled by HTTPPlug. Every exception thrown implements `Http\Client\Exception`. See the different exceptions that can be thrown [in the HTTPPlug documentation](http://docs.php-http.org/en/latest/httplug/exceptions.html). |
462 | 461 | The Intercom API may return an unsuccessful HTTP response, for example when a resource is not found (404). |
463 | | -If you want to catch errors you can wrap your API call into a try/catch: |
| 462 | +If you want to catch errors you can wrap your API call into a try/catch block: |
464 | 463 |
|
465 | 464 | ```php |
466 | | -use GuzzleHttp\Exception\ClientException; |
467 | | - |
468 | 465 | try { |
469 | 466 | $user = $client->users->getUser("570680a8a1bcbca8a90001b9"); |
470 | | -} catch(ClientException $e) { |
471 | | - $response = $e->getResponse(); |
472 | | - $statusCode = $response->getStatusCode(); |
473 | | - if ($statusCode == '404') { |
| 467 | +} catch(Http\Client\Exception $e) { |
| 468 | + if ($e->getCode() == '404') { |
474 | 469 | // Handle 404 error |
475 | 470 | return; |
476 | 471 | } else { |
|
0 commit comments