Skip to content

Instantly share code, notes, and snippets.

@mylk
Created August 14, 2016 17:04
Show Gist options
  • Save mylk/f53bc83cb28e8019048bdda040b4580a to your computer and use it in GitHub Desktop.
Save mylk/f53bc83cb28e8019048bdda040b4580a to your computer and use it in GitHub Desktop.
A basic Intercom API wrapper
<?php
/**
* Basic Intercom API wrapper, in case you don't want to use the official,
* or you don't meet the requirements (ex. your PHP is < 5.6)
*
* Official:
* https://packagist.org/packages/intercom/intercom-php
*/
/*
* Installation (Symfony):
* =======================
* # services.yml
* services:
* # ...
* acme.intercom_client:
* class: Acme\AcmeBundle\Service\IntercomClientService
* calls: [ [setDependencies, [%intercom_api.url%, %intercom_api.app_id%, %intercom_api.key%]] ]
*
* # parameters.yml
* parameters:
* # ...
* intercom_api.url: "https://api.intercom.io"
* intercom_api.app_id: "XXX"
* intercom_api.key: "XXX"
*
* Usage (Symfony):
* ================
* $intercomClient = $this->get("acme.intercom_client");
* $intercomClient->getUsers($data);
* $intercomClient->upsertUser($data);
* $intercomClient->deleteUser($data);
*/
namespace Acme\AcmeBundle\Service;
use Symfony\Component\HttpFoundation\Response;
class IntercomClientService
{
private $apiUrl;
private $credentials;
public function setDependencies($apiUrl, $appId, $apiKey)
{
$this->apiUrl = $apiUrl;
$this->credentials = \sprintf("%s:%s", $appId, $apiKey);
}
private function get($type, $data = array())
{
$url = $this->apiUrl . "/" . $type . "?" . \http_build_query($data);
$curlClient = \curl_init();
\curl_setopt_array($curlClient, array(
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => array(
"Accept: application/json"
),
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => $this->credentials,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false
));
$responseJson = \curl_exec($curlClient);
$responseCode = \curl_getinfo($curlClient, CURLINFO_HTTP_CODE);
\curl_close($curlClient);
$response = \json_decode($responseJson, true);
// throw an exception if response is not ok, or response is null / not JSON
if (
Response::HTTP_OK !== $responseCode
|| !$response
) {
$this->throwApiException($response);
}
return $response;
}
private function post($type, $data)
{
$apiUrl = $this->apiUrl . "/" . $type;
$dataJson = \json_encode($data);
$curlClient = \curl_init();
\curl_setopt_array($curlClient, array(
CURLOPT_URL => $apiUrl,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $dataJson,
CURLOPT_HTTPHEADER => array(
"Accept: application/json",
"Content-Type: application/json",
"Content-Length: " . \strlen($dataJson)
),
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => $this->credentials,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false
));
$responseJson = \curl_exec($curlClient);
$responseCode = \curl_getinfo($curlClient, CURLINFO_HTTP_CODE);
\curl_close($curlClient);
$response = \json_decode($responseJson, true);
// throw an exception if response is not ok or created or response is null / not JSON
if (
!\in_array($responseCode, array(Response::HTTP_OK, Response::HTTP_CREATED))
|| !$response
) {
$this->throwApiException($response);
}
return $response;
}
private function delete($type, $data = array())
{
$url = $this->apiUrl . "/" . $type . "?" . \http_build_query($data);
$curlClient = \curl_init();
\curl_setopt_array($curlClient, array(
CURLOPT_URL => $url,
CURLOPT_CUSTOMREQUEST => "DELETE",
CURLOPT_HTTPHEADER => array(
"Accept: application/json"
),
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => $this->credentials,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false
));
$responseJson = \curl_exec($curlClient);
$responseCode = \curl_getinfo($curlClient, CURLINFO_HTTP_CODE);
\curl_close($curlClient);
$response = \json_decode($responseJson, true);
// throw an exception if response is not ok, or response is null / not JSON
if (
Response::HTTP_OK !== $responseCode
|| !$response
) {
$this->throwApiException($response);
}
return $response;
}
public function getUsers($data)
{
return $this->get("users", $data);
}
public function upsertUser($data)
{
return $this->post("users", $data);
}
public function deleteUser($data)
{
return $this->delete("users", $data);
}
private function throwApiException($response)
{
$errors = array();
if (
isset($response["type"])
&& "error.list" === $response["type"]
&& isset($response["errors"])
) {
foreach ($response["errors"] as $error) {
$errors[] = $error["message"];
}
} else {
$errors[] = "Unknown error";
}
throw new \Exception(\implode(". ", $errors));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment