Skip to content

Instantly share code, notes, and snippets.

@leadbi
Last active January 19, 2018 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leadbi/d03a757a779cae2d4488d738a08325a3 to your computer and use it in GitHub Desktop.
Save leadbi/d03a757a779cae2d4488d738a08325a3 to your computer and use it in GitHub Desktop.
LeadBI API PHP Example
<?php
// create api instance
$api = new LeadBiAPI('your access id', 'your access key');
// example fetch websites
$websites = $api->get('/api/v1/websites');
// example create contact
$websiteId = 1;
$contact = array(
// company
'prospect_id' => 'unknown', // use 'new' to create a new company or use an existing company id (required)
'company_name' => null, // (required only if prospect_id is 'new')
'company_country' => null, // company country iso code (required only if prospect_id is 'new')
// contact info
'first_name' => 'John', // (required)
'last_name' => 'Doe', // (required)
'role' => 'CEO', // (required, empty string allowed)
'email' => json_encode(array('primary_email' => 'john.doe@acme123.com' )), // valid json (required)
'phone' => json_encode(array('primary_phone' => '' )), // // valid json (required), can be empty string
'country' => null, // ISO Code or null
'referral' => 'My Referral', // the name of the referral or source
'tags' => array('tag1', 'tag2'), // array of tags or null
'automation' => 'none' // none or the automation id to be started after the contact is added
);
$newContact = $api->post("/api/v1/visitors/$websiteId/all/create_contact", $contact);
var_dump($newContact);
// end
<?php
/**
* LeadBI API PHP Wrapper
*/
class LeadBiAPI {
/**
* API access id (can be created in Account -> API Keys)
*/
private $accessId;
/**
* API access secret (can be found in Account -> API Keys)
*/
private $accessSecret;
/**
* The api endpoint (default is app.leadbi.com)
*/
private $endpoint;
/**
* Make the requests via https or http (default https)
*/
private $secure;
/**
* Create new LeadBI API object
*/
public function __construct($accessId, $accessSecret, $endpoint = 'app.leadbi.com', $secure=true) {
$this->accessId = $accessId;
$this->accessSecret = $accessSecret;
$this->endpoint = $endpoint;
$this->secure = $secure;
}
/**
* Create curl handler and initialize with the access keys
*/
private function getCurlObject($path){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $this->secure ? "https://$this->endpoint$path" : "http://$this->endpoint$path",
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'Content-Type: application/json',
"X-Access-Id: $this->accessId",
"X-Access-Secret: $this->accessSecret",
),
//CURLOPT_VERBOSE => true, // debug only
//CURLOPT_SSL_VERIFYPEER => false // debug only
));
return $curl;
}
/**
* Make get request
*/
public function get($path){
$ch = $this->getCurlObject($path);
$result = curl_exec($ch);
var_dump($result);
curl_close($ch);
return json_decode($result);
}
/**
* Make post request
*/
public function post($path, $data){
$ch = $this->getCurlObject($path);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result);
}
public function put($path, $data) {
$ch = $this->getCurlObject($path);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result);
}
/**
* Make delete request
*/
public function delete(){
$ch = $this->getCurlObject($path);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result);
}
/**
* Make patch request
*/
public function patch($path, $data){
$ch = $this->getCurlObject($path);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result);
}
}
// end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment