Skip to content

Instantly share code, notes, and snippets.

@atwellpub
Last active September 1, 2020 20:04
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 atwellpub/ee2403b4f85ca1ef473a to your computer and use it in GitHub Desktop.
Save atwellpub/ee2403b4f85ca1ef473a to your computer and use it in GitHub Desktop.
Inbound API PHP wrapper examples. /leads/
<?php
/* This example seeks to help developers understand how to access the /leads/ endpoint.
*
*/
if ( !class_exists( 'Example_API_Calls' )) {
class Example_API_Calls {
static $key;
static $token;
static $base_api_url;
static $complete_api_url;
static $endpoint;
static $query_description;
static $data;
static $show_results;
static $response;
/**
* initiates class
*/
public function __construct() {
/* Define API Credentials for testing - used by connect() method below */
self::$key = '91866f1c792f003fc986e3783ca71e88';
self::$token = 'eaa9be109fc204dee7dfd7d7a94184d9';
self::$base_api_url = 'http://www.yoursite.com/inbound-api/v1/';
}
/**
* Determines connect method and makes a request to the API
*
*
*/
public static function connect() {
$connect = (isset($_GET['connect'])) ? $_GET['connect'] : 'curl';
switch ( $connect ) {
case 'curl' :
self::curl_connect( self::$endpoint , self::$data );
BREAK;
case 'wp_remote_get' :
self::$response = wp_remote_get( self::$endpoint , self::$data );
BREAK;
case 'wp_remote_post':
self::$response = wp_remote_post( $endpoint , self::$data );
BREAK;
}
}
/**
* Uses CURL to connect to Inbound API
*/
public static function curl_connect( $endpoint , $data ) {
$api_url = add_query_arg( array('key' => self::$key , 'token' => self::$token ), self::$base_api_url . $endpoint );
$ch = curl_init( $api_url );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
self::$response = curl_exec($ch);
self::check_response( $endpoint , $data );
}
/**
* Returns data from the API call for review.
*/
public static function check_response() {
echo 'API Key: '. self::$key .'<br>';
echo 'API Token: '. self::$token .'<br>';
echo 'API Endpoint: '. self::$endpoint .'<br>';
echo '<b>Response</b>:<br>';
echo '<div class="toggle" id="'.$id.'">';
echo '<pre>';
echo self::$response;
echo '</pre>';
echo '</div><br><br>';
}
/**
* Example API Calls - Gets all leads - Runs More than one query for the sake of example
* @endpoint leads
*/
public static function get_all_leads() {
/* Sets endpoint to http://www.yoursite.com/inbound-api/v1/leads/ */
self::$endpoint = 'leads';
/* Set paramaters - Will return first 10 results */
self::$data = array(
'results_per_page' => 10
);
/* Connect to API and get results */
self::connect( );
/* Running this new data will help us to return page 2 */
self::$data = array(
'results_per_page' => 10,
'page' => 2
);
/* Connect to API and get results */
self::connect();
}
/**
* Example API Call - Performs an email lookup
* @endpoint leads
*/
public static function get_lead_by_email( $email ) {
/* Sets endpoint to http://www.yoursite.com/inbound-api/v1/leads/ */
self::$endpoint = 'leads';
/* Tell API what to lookup */
self::$data = array(
'email' => $email
);
/* Connect to API and get Results */
self::connect();
}
/**
* API Example Call: Performs a lead lookup by ID
* @endpoint leads
*/
public static function get_lead_by_ID( $ID ) {
/* Sets endpoint to http://www.yoursite.com/inbound-api/v1/leads/ */
self::$endpoint = 'leads';
/* Tell API what to lookup */
self::$data = $ID;
/* Connect to API and get Results */
self::connect();
}
/**
* API Example Call: Performs a lead lookup by tag(s)
* @endpoint leads
*/
public static function get_lead_by_tags() {
/* Sets endpoint to http://www.yoursite.com/inbound-api/v1/leads/ */
self::$endpoint = 'leads';
/* TEST 1 */
/* Tell API what to lookup - Tag inclusion search */
self::$data = array(
'include_tags' => array( 'hello' , 'tag-1' )
);
/* Connect to API and get Results */
self::connect();
/* TEST 2 */
/* Tag exclusion search */
self::$data = array(
'exclude_tags' => array( 'hello' , 'tag-1' )
);
self::$query_description = 'Search lead db for leads without these tags: ' . implode( ',' , self::$data['exclude_tags']);
self::connect();
/* TEST 3 */
/* Tag inclusion search with exclusion search */
self::$data = array(
'include_tags' => array( 'hello' , 'tag-1' ),
'exclude_tags' => array( 'tag-2' )
);
/* Connect to API and get Results */
self::connect();
}
/**
* Custom Query - Get 100 Leads randomly with include tags and exclude tags applied
*/
public static function get_random_leads() {
/* Sets endpoint to http://www.yoursite.com/inbound-api/v1/leads/ */
self::$endpoint = 'leads';
/* Tag inclusion search with exclusion search */
self::$data = array(
'results_per_page ' => '300',
'page' => '1',
'include_tags' => array( 'hello' ),
'exclude_tags' => array( 'tag-3' ),
'orderby' => 'rand',
'order' => 'DESC',
);
/* Connect to API and get Results */
self::connect();
}
}
/* Initialize Class */
$Example_API_Calls = new Example_API_Calls();
/* Get all leads */
$Example_API_Calls->get_all_leads();
/* Get leads by email address */
$Example_API_Calls->get_lead_by_email( 'hudson.atwell@gmail.com' );
/* Get all leads */
$Example_API_Calls->get_lead_by_ID( $ID );
/* Get leads by tag - see inside get_lead_by_tags to change lookup rules */
$Example_API_Calls->get_lead_by_tags( );
/* Custom Query - Get 100 Leads randomly with include tags and exclude tags applied */
$Example_API_Calls->get_random_leads() ;
}
@atwellpub
Copy link
Author

remove self:: from wp_remote_post and wp_remote_get, add result to static variable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment