• Stars
    star
    284
  • Rank 140,280 (Top 3 %)
  • Language
    PHP
  • License
    Other
  • Created about 10 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PHP bindings for the Intercom API

intercom-php

Circle CI packagist Intercom API Version

Official PHP bindings to the Intercom API

Project Updates

Maintenance

We're currently building a new team to provide in-depth and dedicated SDK support.

In the meantime, we'll be operating on limited capacity, meaning all pull requests will be evaluated on a best effort basis and will be limited to critical issues.

We'll communicate all relevant updates as we build this new team and support strategy in the coming months.

Installation

This library supports PHP 7.1 and later

This library uses HTTPlug as HTTP client. HTTPlug 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. This documentation assumes you use the Guzzle6 Client, but you can replace it with any adapter that you prefer.

The recommended way to install intercom-php is through Composer:

composer require intercom/intercom-php php-http/guzzle6-adapter

Clients

Initialize your client using your access token:

use Intercom\IntercomClient;

$client = new IntercomClient('<insert_token_here>');

If you already have an access token you can find it here. If you want to create or learn more about access tokens then you can find more info here.

If you are building a third party application you can get your OAuth token by setting-up-oauth for Intercom.

For most use cases the code snippet above should suffice. However, if needed, you can customize the Intercom client as follows:

Add custom headers

use Intercom\IntercomClient;

$client = new IntercomClient(
    '<insert_token_here>',
    null,
    ['Custom-Header' => 'value']
);

Use a custom HTTP client

This client needs to implement Psr\Http\Client\ClientInterface

$client->setHttpClient($yourHttpClient);

Use a custom request factory

This factory needs to implement Http\Message\RequestFactory

$client->setRequestFactory($yourRequestFactory);

Use a custom URI factory

This factory needs to implement Http\Message\UriFactory

$client->setUriFactory($yourUriFactory); 

API Versions

This library is intended to work with any API Version. By default, the version that you have configured for your App in the Developer Hub will be used. However, you can overwrite that version for a single request or for all the requests using this library by including the Intercom-Version header when initializing the client as follows:

use Intercom\IntercomClient;

$client = new IntercomClient(
    '<insert_token_here>',
    null,
    ['Intercom-Version' => '1.1']
);

For more information about API Versioning, please check the API Versioning Documentation and the API changelog.

Important: Not all the resources supported by this API are supported by all API versions. See the notes below or the API Reference for more information about the resources supported by each API version.

Contacts

This resource is only available in API Versions 2.0 and above

/** Create a contact */
$client->contacts->create([
    'custom_attributes' => ['nickname' => 'Teddy'],
    'email' => '[email protected]',
    'type' => 'user',
]);

/** Update a contact */
$client->contacts->update('570680a8a1bcbca8a90001b9', [
    'custom_attributes' => ['nickname' => 'Teddy'],
    'email' => '[email protected]',
]);

/** Permanently delete a contact */
$client->contacts->deleteContact('570680a8a1bcbca8a90001b9');

/** Get a contact by ID */
$client->contacts->getContact('570680a8a1bcbca8a90001b9');

/** Search for contacts */
$query = ['field' => 'name', 'operator' => '=', 'value' => 'Alice'];
$client->contacts->search([
    'pagination' => ['per_page' => 10],
    'query' => $query,
    'sort' => ['field' => 'name', 'order' => 'ascending'],
]);

/** Get next page of conversation search results */
$client->contacts->nextSearch($query, $response->pages);

/** List all contacts */
$client->contacts->getContacts([]);

Users

This resource is only available in API Versions 1.0 to 1.4. Newer versions use the Contacts resource instead.

/** Create a user */
$client->users->create([
    'custom_attributes' => ['nickname' => 'Teddy'],
    'email' => '[email protected]',
]);

/**
 * Update a user (Note: This method is an alias to the create method. In practice you
 * can use create to update users if you wish)
 */
$client->users->update([
    'custom_attributes' => ['nickname' => 'Teddy'],
    'email' => '[email protected]',
]);

/** Archive a user by ID (i.e. soft delete) */
$client->users->archiveUser('570680a8a1bcbca8a90001b9');

/** Permanently delete a user */
$client->users->permanentlyDeleteUser('570680a8a1bcbca8a90001b9');

/** For more on the difference between archive and permanently deleting a user please see https://developers.intercom.com/reference#archive-a-user. */

/** Get a user by ID */
$client->users->getUser('570680a8a1bcbca8a90001b9');

/** Add companies to a user */
$client->users->create([
    'companies' => [
        [
            'company_id' => '3',
        ]
    ],
    'email' => '[email protected]',
]);

/** Remove companies from a user */
$client->users->create([
    'companies' => [
        [
            'company_id' => '3',
            'remove' => true,
        ]
    ],
    'email' => '[email protected]',
]);

/** Find a single user by email */
$client->users->getUsers([
    'email' => '[email protected]',
]);

/** List all users up to 10k records */
$client->users->getUsers([]);

/**
 * List all users (even above 10k records)
 * The result object contains an array of your user objects and a scroll_param which you can then
 * use to request the next 100 users. Note that the scroll parameter will time out after one minute
 * and you will need to make a new request
 */
$client->users->scrollUsers();

See here for more info on using the scroll parameter

Leads

This resource is only available in API Versions 1.0 to 1.4. Newer versions use the Contacts resource instead.

/**
 * Create a lead
 * See more options here: https://developers.intercom.io/reference#create-lead
 */
$client->leads->create([
    'custom_attributes' => ['nickname' => 'Teddy'],
    'email' => '[email protected]',
]);

/**
 * Update a lead (Note: This method is an alias to the create method.
 * In practice you can use create to update leads if you wish)
 */
$client->leads->update([
    'custom_attributes' => ['nickname' => 'Teddy'],
    'email' => '[email protected]',
]);

/**
 * List leads
 * See more options here: https://developers.intercom.io/reference#list-leads
 */
$client->leads->getLeads([]);

/** Find a lead by ID */
$client->leads->getLead('570680a8a1bcbca8a90000a9');

/** Delete a lead by ID */
$client->leads->deleteLead('570680a8a1bcbca8a90000a9');

/** Convert a Lead to a User */
$client->leads->convertLead([
    'contact' => [
        'user_id' => '8a88a590-e1c3-41e2-a502-e0649dbf721c',
    ],
    'user' => [
        'email' => '[email protected]',
    ],
]);

/**
 * List all leads (even above 10k records)
 * The result object contains an array of your contacts objects and a scroll_param which you can then
 * use to request the next 100 leads. Note that the scroll parameter will time out after one minute
 * and you will need to make a new request
 */
$client->leads->scrollLeads();

See here for more info on using the scroll parameter

Visitors

Retrieve user_id of a visitor via the JavaScript API

/** Update a visitor */
$client->visitors->update([
    'custom_attributes' => ['nickname' => 'Teddy'],
    'user_id' => '8a88a590-e1c3-41e2-a502-e0649dbf721c',
]);

/** Find a visitor by ID */
$client->visitors->getVisitor('570680a8a1bcbca8a90000a9');

/** Find a visitor by User ID */
$client->visitors->getVisitor('', [
    'user_id' => '8a88a590-e1c3-41e2-a502-e0649dbf721c',
]);

/** Delete a visitor by ID */
$client->visitors->deleteVisitor('570680a8a1bcbca8a90000a9');

/** Convert a Visitor to a Lead */
$client->visitors->convertVisitor([
    'type' => 'lead',
    'visitor' => [
        'user_id' => '8a88a590-e1c3-41e2-a502-e0649dbf721c',
    ],
]);

/** Convert a Visitor to a User */
$client->visitors->convertVisitor([
    'type' => 'user',
    'user' => [
        'email' => '[email protected]',
    ],
    'visitor' => [
        'user_id' => '8a88a590-e1c3-41e2-a502-e0649dbf721c',
    ],
]);

Tags

/** List tags */
$client->tags->getTags();

/**
 * Tag users
 * See more options here: https://developers.intercom.io/reference#tag-or-untag-users-companies-leads-contacts
 */
$client->tags->tag([
    'name' => 'Test',
    'users' => [
        ['id' => '1234'],
    ],
]);

Segments

/** List Segments */
$client->segments->getSegments();

/** View a segment */
$client->segments->getSegment('58a707924f6651b07b94376c');

/** View a segment with count */
$client->segments->getSegment('59c124f770e00fd819b9ce81', [
    'include_count' => 'true',
]);

Events

/** Create an event */
$client->events->create([
    'created_at' => 1391691571,
    'email' => '[email protected]',
    'event_name' => 'testing',
    'metadata' => [
        'order_date' => 1392036272,
        'stripe_invoice' => 'inv_3434343434',
    ],
]);

/** View events for a user */
$client->events->getEvents(['email' => '[email protected]']);

Companies

/** Create a company */
$client->companies->create([
    'company_id' => '3',
    'name' => 'foocorp',
]);

/**
 * Update a company
 */
$client->companies->update([
    'id' => '3',
    'name' => 'foocorp',
]);

/** Create or update a company with custom attributes. */
$client->companies->update([
    'custom_attributes' => [
        'short_name' => 'ABC Inc.',
    ],
    'id' => '3',
    'name' => 'foocorp',
]);

/** List Companies */
$client->companies->getCompanies([]);

/** Get a company by ID */
$client->companies->getCompany('531ee472cce572a6ec000006');

/** List users belonging to a company by ID */
$client->companies->getCompanyUsers('531ee472cce572a6ec000006');

/** List users belonging to a company by company_id */
$client->companies->getCompanies([
    'company_id' => '3',
    'type' => 'user',
]);

/**
 * Add companies to a contact with IDs
 * First parameter is contact ID, second is company ID
 */
$client->companies->attachContact('570680a8a1bcbca8a90001b9', '531ee472cce572a6ec000006');

/**
 * Detach company from contact
 * First parameter is contact ID, second is company ID
 */
$client->companies->detachContact('570680a8a1bcbca8a90001b9', '531ee472cce572a6ec000006');

Admins

/** List admins */
$client->admins->getAdmins();

Messages

/**
 * Send a message from an admin to a user
 * See more options here: https://developers.intercom.io/reference#conversations
 */
$client->messages->create([
    'body' => 'Ponies, cute small horses or something more sinister?',
    'from' => [
        'id' => '1234',
        'type' => 'admin',
    ],
    'message_type' => 'inapp',
    'subject' => 'Hey',
    'to' => [
        'email' => '[email protected]',
        'type' => 'user',
    ],
]);

Conversations

/**
 * Create a conversation
 * See more options here: https://developers.intercom.com/intercom-api-reference/reference#create-a-conversation
 */
$client->conversations->create([
    'body' => 'Hello.',
    'from' => [
        'id' => '1234',
        'type' => 'user',
    ],
]);

/**
 * List conversations for an admin
 * See more options here: https://developers.intercom.io/reference#list-conversations
 */
$client->conversations->getConversations([
    'admin_id' => '25610',
    'type' => 'admin',
]);

/** Get a single conversation */
$client->conversations->getConversation('1234');

/** Get a single conversation with plaintext comments */
$client->conversations->getConversation('1234', [
    'display_as' => 'plaintext',
]);

/** Search for conversations (API version >= 2.0) */
$query = ['field' => 'updated_at', 'operator' => '>', 'value' => '1560436784'];
$client->conversations->search([
    'pagination' => ['per_page' => 10],
    'query' => $query,
    'sort' => ['field' => 'updated_at', 'order' => 'ascending'],
]);

/** Get next page of conversation search results (API version >= 2.0) */
$client->conversations->nextSearch($query, $response->pages);

/**
 * Reply to a conversation
 * See more options here: https://developers.intercom.io/reference#replying-to-a-conversation
 */
$client->conversations->replyToConversation('5678', [
    'body' => 'Thanks :)',
    'email' => '[email protected]',
    'message_type' => 'comment',
    'type' => 'user',
]);

/**
 * Reply to a user's last conversation
 * See more options here: https://developers.intercom.com/reference#replying-to-users-last-conversation
 */
$client->conversations->replyToLastConversation([
    'body' => 'Thanks :)',
    'email' => '[email protected]',
    'message_type' => 'comment',
    'type' => 'user',
]);

/**
 * Mark a conversation as read
 * See API documentation here: https://developers.intercom.io/reference#marking-a-conversation-as-read
 */
$client->conversations->markConversationAsRead('7890');

Counts

/**
 * List counts
 * See more options here: https://developers.intercom.io/reference#getting-counts
 */
$client->counts->getCounts([]);

Notes

/** Create a note */
$client->notes->create([
    'admin_id' => '21',
    'body' => 'Text for my note',
    'user' => [
        'id' => '5310d8e8598c9a0b24000005',
    ]
]);

/** List notes for a user */
$client->notes->getNotes([
  'user_id' => '25',
]);

/** Get a single Note by id */
$client->notes->getNote('42');

Teams

/** List teams */
$client->teams->getTeams();

/** Get a single Team by id */
$client->teams->getTeam('1188');

Rate Limits

Rate limit info is passed via the rate limit headers. You can access this information as follows:

$rate_limit = $client->getRateLimitDetails();
print("{$rate_limit['remaining']} {$rate_limit['limit']} \n");
print_r($rate_limit['reset_at']->format(DateTime::ISO8601));

For more info on rate limits and these headers please see the API reference docs

Pagination

When listing, the Intercom API may return a pagination object:

{
  "pages": {
    "next": "..."
  }
}

You can grab the next page of results using the client:

$client->nextPage($response->pages);

In API versions 2.0 and above subsequent pages for listing contacts can be retreived with:

$client->nextCursor($response->pages);

Scroll

The first time you use the scroll API you can just send a simple GET request. This will return up to 100 records. If you have more than 100 you will need to make another call. To do this you need to use to scroll_parameter returned in the original response. Use this for subsequent responses until you get an empty array of records. This means there are no records and the scroll timer will be reset. For more information on scroll please see the API reference Here is an example of a simple way to use the scroll for multiple calls:

require "vendor/autoload.php";

use Intercom\IntercomClient;

$client = new IntercomClient(getenv('AT'), null);
$resp = $client->users->scrollUsers([]);
$count = 1;
echo "PAGE $count: " . sizeof($resp->users);
echo "\n";
while (!empty($resp->scroll_param) && sizeof($resp->users) > 0) {
    $count = ++$count;
    $resp = $client->users->scrollUsers(["scroll_param" => $resp->scroll_param]);
    echo "PAGE $count: " . sizeof($resp->users);
    echo "\n";
}

Exceptions

Exceptions are handled by HTTPlug. Every exception thrown implements Http\Client\Exception. See the http client exceptions and the client and server errors. The Intercom API may return an unsuccessful HTTP response, for example when a resource is not found (404). If you want to catch errors you can wrap your API call into a try/catch block:

try {
    $user = $client->users->getUser('570680a8a1bcbca8a90001b9');
} catch(Http\Client\Exception $e) {
    if ($e->getCode() == '404') {
        // Handle 404 error
        return;
    } else {
        throw $e;
    }
}

Pull Requests

  • Add tests! Your patch won't be accepted if it doesn't have tests.

  • Document any change in behaviour. Make sure the README and any other relevant documentation are kept up-to-date.

  • Create topic branches. Don't ask us to pull from your master branch.

  • One pull request per feature. If you want to do more than one thing, send multiple pull requests.

  • Send coherent history. Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please squash them before sending them to us.

More Repositories

1

intercom-ios

📱 Intercom for iOS, for integrating Intercom into your iOS application.
Objective-C
357
star
2

intercom-node

Node.js bindings for the Intercom API
TypeScript
344
star
3

intercom-rails

The easiest way to install Intercom in a Rails app.
Ruby
274
star
4

contentful-typescript-codegen

Generate TypeScript interfaces from a Contentful environment
TypeScript
272
star
5

intercom-ruby

Ruby bindings for the Intercom API
Ruby
272
star
6

intercom-android

Intercom for Android, for integrating Intercom into your Android application.
257
star
7

python-intercom

Python wrapper for the Intercom API.
Python
222
star
8

ember-href-to

🔗 A lightweight alternative to {{link-to}}
JavaScript
191
star
9

ember-undo-stack

🔁 An undo/redo stack for Ember.js objects
JavaScript
115
star
10

intercom-react-native

React Native wrapper to bridge our iOS and Android SDK
Java
100
star
11

intercom-cordova

Cordova/PhoneGap plugin for Intercom
Objective-C
95
star
12

intercom-go

Go Bindings For Intercom
Go
69
star
13

intercom-java

Java bindings for the Intercom API
Java
61
star
14

intercom-dotnet

Intercom API client library for .NET
C#
59
star
15

hippie_csv

✌️ Tolerant, liberal CSV parsing
Ruby
57
star
16

ember-computed-template-string

⚡ One-line string computed properties
JavaScript
47
star
17

cocoapods-mangle

A CocoaPods plugin which mangles the symbols of your dependencies
Ruby
44
star
18

intercom-elixir

Elixir helpers for generating the Intercom snippet and interacting with the Intercom API
Elixir
32
star
19

speakeasy

A tool for creating Go web servers which can be embedded in iOS and Android apps.
Go
29
star
20

intercom-wordpress

Intercom integration with Wordpress
PHP
28
star
21

intercom-twilio-demo

Demonstrating basic Intercom/Twilio functionality
Ruby
26
star
22

state_of_the_nation

An easy way to model state changing over time with ActiveRecord.
Ruby
24
star
23

requisite

Ruby
18
star
24

gocore

A library with a set of standardised functions for applications written in Go at Intercom
Go
10
star
25

intercom-api-postman-collection

9
star
26

passport-intercom

Passport strategy for Intercom OAuth 2
JavaScript
8
star
27

Messenger-Quickstart

Quick start code to get you using Intercom locally
HTML
8
star
28

lease

Lease is a general DynamoDB-based lease implementation
Go
8
star
29

example-basic-javascript-install

Sample project with the Intercom Messenger installed with basic JavaScript
HTML
7
star
30

passport-intercom-example-app

Example web app using passport-intercom
JavaScript
7
star
31

Intercom-OpenAPI

An OpenAPI description for Intercom's REST API
JavaScript
6
star
32

omniauth-intercom

Intercom strategy for OmniAuth
Ruby
6
star
33

oauth2-intercom

Intercom Provider for the OAuth 2.0 Client
PHP
6
star
34

ember-cli-detergent

detect and block blacklisted javascript methods in your code.
JavaScript
6
star
35

example-single-page-app-install

Example single page application with Intercom
JavaScript
5
star
36

expan-intercom

A/B testing library for Python
Python
5
star
37

dropwizard-guice

Use Guice with Dropwizard
Java
5
star
38

ember-hacker-news

JavaScript
5
star
39

intercom-php-rewrite

Work in progress PHP client using Guzzle 6
PHP
4
star
40

prfeed

A way to automate some of the things we do in the code review process (e.g., manual reposting) and improve the visibility of outstanding pull requests.
Ruby
4
star
41

MediaStreamRecorder

Fork of https://github.com/streamproc/MediaStreamRecorder
JavaScript
3
star
42

platform-series-importing-users

Ruby
3
star
43

example-php

Example PHP code for Intercom
PHP
3
star
44

intercom-install-examples

Collection of example applications with the Intercom Messenger installed
PHP
3
star
45

oauth-setup-tutorial

Simple Example of Setting up OAuth in Intercom
Ruby
2
star
46

minicom-public

Skeletal layout of an interview prototype
Ruby
2
star
47

docker-sdk

Repo of Dockerfiles for Intercom Docker SDK images
1
star
48

frontend-marketing-test

CSS
1
star
49

migration_reporter

Provides a rake task to report Active Record migration details to a given URL.
Ruby
1
star
50

example-go

Example go code for Intercom API
Go
1
star
51

pagerduty-guice

A Guice module for PagerDuty with incident key debouncing
Java
1
star
52

ember-computed-template-string-parser

Parses string templates and produces Ember computed properties
JavaScript
1
star
53

identity-verification-code-samples

Sample code snippets for setting up Identity Verification in the Intercom Messenger
1
star
54

example-node

Example node code for the Node Docker image
JavaScript
1
star
55

example-ruby

Example ruby code for the Intercom API
Ruby
1
star