Last active
August 29, 2015 14:06
-
-
Save atwellpub/549edf346b7926024e8c to your computer and use it in GitHub Desktop.
This example seeks to help developers understand how to access the /analytics/track-link/ endpoint.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This example seeks to help developers understand how to access the /analytics/track-link/ 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 $data; | |
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; | |
} | |
} | |
/** | |
* 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 Query: Gets a tracked link | |
* @endpoint analytics/track-link | |
*/ | |
public static function track_link() { | |
/* Sets endpoint to http://www.yoursite.com/inbound-api/v1/lists/modify */ | |
self::$endpoint = 'analytics/track-link'; | |
/* Prepare an example lead dataset - this data will be sent to the API */ | |
self::$data = array( | |
'id' => 96997, //lead ID | |
'tracking_id' => 'api_tests', //unique alphnumeric string | |
'url' => 'http://www.inboundnow.com' , //url to final destination | |
'add_tags' => array( 'special tag 1' , 'special tag 2'), | |
'remove_tags' => array( 'tag-3'), | |
'add_lists' => array( 110 , 111, 112, 113, 114 ), | |
'remove_lists' => array( 114 ), | |
/* custom data can be interecepted and used through the inbound_track_link hook */ | |
'custom_data' => array( | |
'widget_color' => 'blue', | |
'widget_price' => '100.00' | |
) | |
); | |
/* Connect to the API and print data results */ | |
self::connect(); | |
/* Intercept response */ | |
$link = json_decode( self::$response , true ); | |
if (isset($link['url'])) { | |
echo $link['url']; | |
} | |
} | |
} | |
/* Initialize Class */ | |
$Example_API_Calls = new Example_API_Calls(); | |
/* Generate a tracked link */ | |
$Example_API_Calls->track_link(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment