Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Example wrapper that shows how we would source 300 leads randomly given a condition and then add tags to those 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;
static $response_decoded;
/**
* 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://inboundsoon.dev/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::wp_remote_get( self::$endpoint , self::$data );
BREAK;
case 'wp_remote_post':
self::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::$response_decoded = json_decode( self::$response , true );
self::check_response();
}
/** Checks response for errors **/
public static function check_response() {
if ( is_array( self::$response_decoded ) && isset( self::$response_decoded['error'] ) ) {
echo self::$response_decoded['error'];
echo '<br>';
}
}
/**
* Basic Example - 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();
return self::$response_decoded;
}
/**
* Adds tags to result set
* @param ARRAY $results
*/
public static function add_tags_to_results( $results = array() ) {
/* Sets endpoint to http://www.yoursite.com/inbound-api/v1/leads/modify */
self::$endpoint = 'leads/modify';
foreach ( $results['results'] as $ID => $lead ) {
/* Prepare an example lead dataset - this data will be sent to the API */
self::$data = array(
'ID' => $ID ,
'add_tags' => array( 'super' ) //these tags will be created if they do not exist
);
/* Connect to the API and print data results */
self::connect();
}
}
}
/* To Fire Queries add ?go=1 when accessing a page where this PHP is located */
if (isset($_GET['go']) && $_GET['go']) {
/* Initialize Class */
$Example_API_Calls = new Example_API_Calls();
/* Custom Query - Get 100 Leads randomly with include tags and exclude tags applied */
$results = $Example_API_Calls->get_random_leads() ;
/* Loop through results and add tags to them */
$Example_API_Calls->add_tags_to_results( $results ) ;
echo 'done!';
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment