From edddc1a68909357078bc08d4f7859e7fb2addc28 Mon Sep 17 00:00:00 2001 From: Webster Gordon Date: Fri, 18 Jan 2013 15:47:55 -0500 Subject: [PATCH 01/11] Added check on submit_form to see if parameters are arrays --- class.forms.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/class.forms.php b/class.forms.php index daf3f57..00de87a 100644 --- a/class.forms.php +++ b/class.forms.php @@ -37,7 +37,17 @@ class HubSpot_Forms extends HubSpot_Baseclient{ **/ public function submit_form($portalId, $guid, $form_fields, $hs_context){ $url_base = 'https://forms.hubspot.com/uploads/form/v2/'.$portalId.'/'.$guid; - $form_fields['hs_context'] = json_encode($hs_context); + if(is_array($form_fields)){ + if(is_array($hs_context)){ + $form_fields['hs_context'] = json_encode($hs_context); + } + else{ + return "Please make sure you are passing hs_context as an array"; + } + } + else{ + return "Please make sure you are passing form_fields as an array"; + } $param_string = '&'.http_build_query($form_fields,'','&'); try{ return json_decode($this->execute_post_request($this->get_forms_request_url($url_base,null),$param_string,TRUE)); From d6d44589983f58899e56e34712bdf78a4a215bb7 Mon Sep 17 00:00:00 2001 From: Webster Gordon Date: Thu, 24 Jan 2013 13:23:46 -0500 Subject: [PATCH 02/11] Added support for oauth tokens --- class.baseclient.php | 174 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 141 insertions(+), 33 deletions(-) diff --git a/class.baseclient.php b/class.baseclient.php index 6e8d6e7..73a0b7f 100644 --- a/class.baseclient.php +++ b/class.baseclient.php @@ -16,16 +16,22 @@ * language governing permissions and limitations under the * License. */ +include('class.exception.php'); + class HubSpot_BaseClient { // HubSpot_BaseClient class to be extended by specific hapi clients // Declare variables protected $HAPIKey; + protected $ACCESS_TOKEN; protected $API_PATH; + protected $REFRESH_TOKEN; + protected $CLIENT_ID; protected $API_VERSION; protected $isTest = false; protected $PATH_DIV = '/'; - protected $KEY_PARAM = '?hapikey='; + protected $API_KEY_PARAM = '?hapikey='; + protected $TOKEN_PARAM = '?access_token='; protected $PROD_DOMAIN = 'https://api.hubapi.com'; protected $QA_DOMAIN = 'https://hubapiqa.com'; protected $userAgent; // new @@ -62,8 +68,15 @@ class HubSpot_BaseClient { * * @param $HAPIKey: String value of HubSpot API Key for requests **/ - public function __construct($HAPIKey,$userAgent="haPiHP default UserAgent") { // new - $this->HAPIKey = $HAPIKey; + public function __construct($HAPIKey=null, $access_token=null, $refresh_token=null,$client_id=null,$userAgent="haPiHP default UserAgent") { // new + + if($HAPIKey AND $access_token){ + throw new Exception("Cannot use hapikey and OAuth token", 1); + } + else{ + $this->HAPIKey = $HAPIKey; + $this->ACCESS_TOKEN = $access_token; + } $this->userAgent = $userAgent; // new } @@ -127,12 +140,12 @@ public function set_is_test($testing) { * @returns: String value of domain, including https protocol **/ protected function get_domain() { - if ($this->isTest == true){ - return $this->QA_DOMAIN; - } else { - return $this->PROD_DOMAIN; - } - } + if ($this->isTest == true){ + return $this->QA_DOMAIN; + } else { + return $this->PROD_DOMAIN; + } + } /** * Creates the url to be used for the api request @@ -144,12 +157,39 @@ protected function get_domain() { **/ protected function get_request_url($endpoint,$params) { $paramstring = $this->array_to_params($params); - return $this->get_domain() . $this->PATH_DIV . - $this->get_api() . $this->PATH_DIV . - $this->get_api_version() . $this->PATH_DIV . - $endpoint . - $this->KEY_PARAM . $this->HAPIKey . - $paramstring; + if($this->HAPIKey){ + return $this->get_domain() . $this->PATH_DIV . + $this->get_api() . $this->PATH_DIV . + $this->get_api_version() . $this->PATH_DIV . + $endpoint . + $this->KEY_PARAM . $this->HAPIKey . + $paramstring; + } + else{ + if($this->check_auth()>=400){ + try { + $refreshed_token = $this->refresh_access_token($this->REFRESH_TOKEN,$this->CLIENT_ID); + $this->ACCESS_TOKEN = $refreshed_token['access_token']; + return $this->get_domain() . $this->PATH_DIV . + $this->get_api() . $this->PATH_DIV . + $this->get_api_version() . $this->PATH_DIV . + $endpoint . + $this->TOKEN_PARAM . $this->ACCESS_TOKEN . + $paramstring; + + } catch (HubSpot_Exception $e) { + print_r('Unable to refresh the OAuth token. Please provide a valid access_token or refresh_token'); + } + } + else{ + return $this->get_domain() . $this->PATH_DIV . + $this->get_api() . $this->PATH_DIV . + $this->get_api_version() . $this->PATH_DIV . + $endpoint . + $this->TOKEN_PARAM . $this->ACCESS_TOKEN . + $paramstring; + } + } } /** @@ -162,9 +202,16 @@ protected function get_request_url($endpoint,$params) { **/ protected function get_forms_request_url($url_base,$params) { $paramstring = $this->array_to_params($params); - return $url_base . - $this->KEY_PARAM . $this->HAPIKey . - $paramstring; + if($this->ACCESS_TOKEN){ + return $url_base . + $this->TOKEN_PARAM . $this->ACCESS_TOKEN . + $paramstring; + } + else{ + return $url_base . + $this->KEY_PARAM . $this->HAPIKey . + $paramstring; + } } /** @@ -215,17 +262,17 @@ protected function execute_post_request($url, $body, $formenc=FALSE) { //new curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); // new if ($formenc) // new curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); // new - $output = curl_exec($ch); - $errno = curl_errno($ch); - $error = curl_error($ch); - $this->setLastStatusFromCurl($ch); - curl_close($ch); - if ($errno > 0) { - throw new HubSpot_Exception ('cURL error: ' + $error); - } else { - return $output; + $output = curl_exec($ch); + $errno = curl_errno($ch); + $error = curl_error($ch); + $this->setLastStatusFromCurl($ch); + curl_close($ch); + if ($errno > 0) { + throw new HubSpot_Exception ('cURL error: ' + $error); + } else { + return $output; + } } - } /** * Executes HTTP POST request with JSON as the POST body @@ -401,11 +448,11 @@ protected function array_to_params($params) { $paramstring = ''; if ($params != null) { foreach ($params as $parameter => $value) { - $paramstring = $paramstring . '&' . $parameter . '=' . urlencode($value); - } - } - return $paramstring; - } + $paramstring = $paramstring . '&' . $parameter . '=' . urlencode($value); + } + } + return $paramstring; + } /** * Utility function used to determine if variable is empty @@ -433,4 +480,65 @@ protected function setLastStatusFromCurl($ch) $this->lastStatus = (isset($info['http_code'])) ? $info['http_code'] : null; } + /** + * Quick check of access_token + * + * + * @return: Response code for request + * + **/ + protected function check_auth(){ + $url = 'https://api.hubapi.com/contacts/v1/properties/email?access_token='.$this->ACCESS_TOKEN; + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); + $output = curl_exec($ch); + $response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + return $response_code; + } + /** + * Refresh OAuth token + * + * @param refresh_token: The refresh token for your portal + * @param client_id: Unique ID for your app, generated when the app is registered + * + * @return: Body of request result + * + * @throws HubSpot_Exception + **/ + protected function refresh_access_token($refresh_token, $client_id){ + if($refresh_token AND $client_id){ + $url = 'https://api.hubapi.com/auth/v1/refresh'; + $body = 'refresh_token='.$refresh_token.'&client_id='.$client_id.'&grant_type=refresh_token'; + $ch = curl_init(); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, $body); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); // new + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); + $output = curl_exec($ch); + $apierr = curl_errno($ch); + $errmsg = curl_error($ch); + $response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + $output_array = json_decode($output); + if ($response_code<400) { + return $output_array; + } else { + throw new HubSpot_Exception("cURL error: " + $errmsg); + + } + } + else{ + throw new HubSpot_Exception("Please provide a refresh_token and client_id"); + } + + +} + + } From 18b4582f5544a07d9bad59a42f11dc9c3c4bba11 Mon Sep 17 00:00:00 2001 From: Webster Gordon Date: Thu, 24 Jan 2013 13:24:16 -0500 Subject: [PATCH 03/11] fixed misnamed function Properties wrapper --- class.properties.php | 2 +- example.php | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/class.properties.php b/class.properties.php index bb9c7b9..070fe51 100644 --- a/class.properties.php +++ b/class.properties.php @@ -120,7 +120,7 @@ public function delete_property($name){ $endpoint = 'properties/'.$name; try{ - return $this->new_execute_delete_request($this->get_request_url($endpoint,null)); + return $this->execute_delete_request($this->get_request_url($endpoint,null)); } catch(HubSpot_Exception $e){ print_r('Unable to delete property: '.$e); diff --git a/example.php b/example.php index 5fd6982..5c9000b 100644 --- a/example.php +++ b/example.php @@ -389,13 +389,13 @@ $deleted_list = $lists->delete_list($list_id); print_r($deleted_list); - +*/ //Exercise Properties API - $properties = new HubSpot_Properties($HAPIKey); + $properties = new HubSpot_Properties(null,'demooooo-oooo-oooo-oooo-oooooooooooo',null,null); //Get all Properties $all_props = $properties->get_all_properties(); - print_r($all_props); + print_r($all_props); //Create new Property $new_prop_info = array('label'=>'Favorite Boston NBA Team','name'=>'favbostonnbateam','description'=>'Your favorite NBA team in the Boston Area', @@ -433,9 +433,10 @@ $deleted_group = $properties->delete_property_group('newpropgroup'); print_r($deleted_group); -*/ +/* + //Exercise Social Media API - $social = new HubSpot_SocialMedia($HAPIKey); + $social = new HubSpot_SocialMedia(null,'demooooo-oooo-oooo-oooo-oooooooooooo'); //Get Publishing Channels $channels = $social->get_publishing_channels(); @@ -462,5 +463,5 @@ $broadcast_guid = $new_broadcast->{'broadcastGuid'}; $deleted_broadcast = $social->cancel_broadcast($broadcast_guid); print_r($deleted_broadcast); - +*/ ?> From 3c6a0f226d6b3f9a3b90216fb35f2f5f8065e9de Mon Sep 17 00:00:00 2001 From: Webster Gordon Date: Thu, 24 Jan 2013 13:29:01 -0500 Subject: [PATCH 04/11] Also updated comments in baseclient to go with OAuth support --- class.baseclient.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/class.baseclient.php b/class.baseclient.php index 73a0b7f..2235c00 100644 --- a/class.baseclient.php +++ b/class.baseclient.php @@ -66,7 +66,11 @@ class HubSpot_BaseClient { /** * Constructor. * - * @param $HAPIKey: String value of HubSpot API Key for requests + *@param HAPIKey: String value of HubSpot API Key for requests + * access_token: String value of Hubspot OAuth Token + * refresh_token: String value of refresh token given initially for OAuth + * client_id: Unique ID for your registered app + * **/ public function __construct($HAPIKey=null, $access_token=null, $refresh_token=null,$client_id=null,$userAgent="haPiHP default UserAgent") { // new @@ -148,7 +152,7 @@ protected function get_domain() { } /** - * Creates the url to be used for the api request + * Creates the url to be used for the api request. If OAuth token is provided but invalid, will attempt a refresh. * * @param endpoint: String value for the endpoint to be used (appears after version in url) * @param params: Array containing query parameters and values From c91c0145da258b89a52d16514d082af2a2817033 Mon Sep 17 00:00:00 2001 From: Webster Gordon Date: Fri, 25 Jan 2013 09:24:05 -0500 Subject: [PATCH 05/11] no real change here --- class.baseclient.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/class.baseclient.php b/class.baseclient.php index 2235c00..1f80fbc 100644 --- a/class.baseclient.php +++ b/class.baseclient.php @@ -79,7 +79,9 @@ public function __construct($HAPIKey=null, $access_token=null, $refresh_token=nu } else{ $this->HAPIKey = $HAPIKey; + print_r("HAPIKey:".$this->HAPIKey); $this->ACCESS_TOKEN = $access_token; + print_r("token:".$this->ACCESS_TOKEN); } $this->userAgent = $userAgent; // new } @@ -161,16 +163,20 @@ protected function get_domain() { **/ protected function get_request_url($endpoint,$params) { $paramstring = $this->array_to_params($params); + echo $this->HAPIKey; if($this->HAPIKey){ + print_r("Trying to use hapikey"); return $this->get_domain() . $this->PATH_DIV . $this->get_api() . $this->PATH_DIV . $this->get_api_version() . $this->PATH_DIV . $endpoint . - $this->KEY_PARAM . $this->HAPIKey . + $this->API_KEY_PARAM . $this->HAPIKey . $paramstring; } else{ + print "went to else"; if($this->check_auth()>=400){ + print_r("Auth check failed"); try { $refreshed_token = $this->refresh_access_token($this->REFRESH_TOKEN,$this->CLIENT_ID); $this->ACCESS_TOKEN = $refreshed_token['access_token']; @@ -493,6 +499,7 @@ protected function setLastStatusFromCurl($ch) **/ protected function check_auth(){ $url = 'https://api.hubapi.com/contacts/v1/properties/email?access_token='.$this->ACCESS_TOKEN; + print_r($url); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); From c76f8c1639b3ac24079186fe241ecbe1e3b0ca81 Mon Sep 17 00:00:00 2001 From: Webster Gordon Date: Fri, 25 Jan 2013 09:57:20 -0500 Subject: [PATCH 06/11] Baseclient now takes an array as a parameter. User should pass either 'HAPIKey' or 'access_token' in the array --- class.baseclient.php | 22 +++++++++++++--------- example.php | 17 +++++++++-------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/class.baseclient.php b/class.baseclient.php index 2235c00..01235cf 100644 --- a/class.baseclient.php +++ b/class.baseclient.php @@ -66,20 +66,24 @@ class HubSpot_BaseClient { /** * Constructor. * - *@param HAPIKey: String value of HubSpot API Key for requests - * access_token: String value of Hubspot OAuth Token - * refresh_token: String value of refresh token given initially for OAuth - * client_id: Unique ID for your registered app + *@param auth: array with the following optional parameters. Either an API key or and OAuth token must be used for authentication. + * HAPIKey: String value of HubSpot API Key for requests + * access_token: String value of Hubspot OAuth Token + * refresh_token: String value of refresh token given initially for OAuth + * client_id: Unique ID for your registered app * **/ - public function __construct($HAPIKey=null, $access_token=null, $refresh_token=null,$client_id=null,$userAgent="haPiHP default UserAgent") { // new + public function __construct($auth,$userAgent="haPiHP default UserAgent") { // new - if($HAPIKey AND $access_token){ + if(isset($auth['HAPIKey']) AND isset($auth['access_token'])){ throw new Exception("Cannot use hapikey and OAuth token", 1); } else{ - $this->HAPIKey = $HAPIKey; - $this->ACCESS_TOKEN = $access_token; + $auth += array('HAPIKey'=>null,'access_token'=>null,'refresh_token'=>null,'client_id'=>null); + $this->HAPIKey = $auth['HAPIKey']; + $this->ACCESS_TOKEN = $auth['access_token']; + $this->REFRESH_TOKEN = $auth['refresh_token']; + $this->CLIENT_ID = $auth['client_id']; } $this->userAgent = $userAgent; // new } @@ -166,7 +170,7 @@ protected function get_request_url($endpoint,$params) { $this->get_api() . $this->PATH_DIV . $this->get_api_version() . $this->PATH_DIV . $endpoint . - $this->KEY_PARAM . $this->HAPIKey . + $this->API_KEY_PARAM . $this->HAPIKey . $paramstring; } else{ diff --git a/example.php b/example.php index 5c9000b..56359db 100644 --- a/example.php +++ b/example.php @@ -31,6 +31,7 @@ require_once 'class.socialmedia.php'; $HAPIKey = 'demo'; +$access_token = 'demooooo-oooo-oooo-oooo-oooooooooooo'; /* //Exercise Blog API $blogs = new HubSpot_Blog($HAPIKey); @@ -327,9 +328,9 @@ //Delete a Form $deleted_form = $forms->delete_form($new_form_guid); print_r($deleted_form); - +*/ //Exercise Lists API - $lists = new HubSpot_Lists($HAPIKey); + $lists = new HubSpot_Lists(array('access_token'=>$access_token)); //Create a contact List $list_array = array('name'=>'Tweeters','dynamic'=>false,'portalId'=>'62515','filters'=> @@ -337,7 +338,7 @@ $new_list = $lists->create_list($list_array); $list_id = $new_list->{'listId'}; print_r($new_list); - + //Update a contact List $updated_list_array = array('name'=>'Tweeters and Hubspotters','dynamic'=>false,'portalId'=>'62515','filters'=> array(array(array('operator'=>'IS_NOT_EMPTY','property'=>'twitterhandle','type'=>'string'), @@ -389,9 +390,9 @@ $deleted_list = $lists->delete_list($list_id); print_r($deleted_list); -*/ + //Exercise Properties API - $properties = new HubSpot_Properties(null,'demooooo-oooo-oooo-oooo-oooooooooooo',null,null); + $properties = new HubSpot_Properties(array('HAPIKey'=>$HAPIKey)); //Get all Properties $all_props = $properties->get_all_properties(); @@ -433,10 +434,10 @@ $deleted_group = $properties->delete_property_group('newpropgroup'); print_r($deleted_group); -/* - + + /* //Exercise Social Media API - $social = new HubSpot_SocialMedia(null,'demooooo-oooo-oooo-oooo-oooooooooooo'); + $social = new HubSpot_SocialMedia(array('access_token'=>'demooooo-oooo-oooo-oooo-oooooooooooo')); //Get Publishing Channels $channels = $social->get_publishing_channels(); From bf00132acbd2216f1b97c98ae16956bae69b6b6b Mon Sep 17 00:00:00 2001 From: Webster Gordon Date: Tue, 30 Apr 2013 17:25:09 -0400 Subject: [PATCH 07/11] don't include hapikey in form submission call --- class.baseclient.php | 12 ++---------- class.forms.php | 2 +- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/class.baseclient.php b/class.baseclient.php index 01235cf..1dead4f 100644 --- a/class.baseclient.php +++ b/class.baseclient.php @@ -210,16 +210,8 @@ protected function get_request_url($endpoint,$params) { **/ protected function get_forms_request_url($url_base,$params) { $paramstring = $this->array_to_params($params); - if($this->ACCESS_TOKEN){ - return $url_base . - $this->TOKEN_PARAM . $this->ACCESS_TOKEN . - $paramstring; - } - else{ - return $url_base . - $this->KEY_PARAM . $this->HAPIKey . - $paramstring; - } + return $url_base . + $paramstring; } /** diff --git a/class.forms.php b/class.forms.php index 00de87a..d9e53cd 100644 --- a/class.forms.php +++ b/class.forms.php @@ -48,7 +48,7 @@ public function submit_form($portalId, $guid, $form_fields, $hs_context){ else{ return "Please make sure you are passing form_fields as an array"; } - $param_string = '&'.http_build_query($form_fields,'','&'); + $param_string = '?'.http_build_query($form_fields,'','&'); try{ return json_decode($this->execute_post_request($this->get_forms_request_url($url_base,null),$param_string,TRUE)); } From d42547d5c8f7803c60ba624cc9c9a6706940b1e6 Mon Sep 17 00:00:00 2001 From: Webster Gordon Date: Wed, 1 May 2013 09:45:27 -0400 Subject: [PATCH 08/11] update to comments --- class.lists.php | 2 +- class.socialmedia.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/class.lists.php b/class.lists.php index 59cd7fa..6c33655 100644 --- a/class.lists.php +++ b/class.lists.php @@ -296,7 +296,7 @@ public function remove_contacts_from_list($vids,$id){ return $this->execute_JSON_post_request($this->get_request_url($endpoint,null),json_encode($request_body)); } catch(HubSpot_Exception $e){ - print_r("Unable to add contacts: ".$e); + print_r("Unable to remove contacts: ".$e); } } diff --git a/class.socialmedia.php b/class.socialmedia.php index e07ddae..a91ac91 100644 --- a/class.socialmedia.php +++ b/class.socialmedia.php @@ -131,15 +131,15 @@ public function create_broadcast($broadcast){ /** - * Delete specific Broadcast Message + * Cancel specific Broadcast Message * *@param guid: Unique ID for the broadcast * - *@return returns response body for DELETE request + *@return returns response body for HTTP DELETE request * *@throws HubSpot_Exception **/ - public function delete_broadcast($guid){ + public function cancel_broadcast($guid){ $endpoint = 'broadcasts/'.$guid; try{ From e2a1fca983d0b29e454f53525c59ca2b3be9ae8c Mon Sep 17 00:00:00 2001 From: Webster Gordon Date: Mon, 3 Mar 2014 10:23:13 -0500 Subject: [PATCH 09/11] formatting fixes --- class.baseclient.php | 729 ++++++++++++++++++++----------------------- 1 file changed, 339 insertions(+), 390 deletions(-) diff --git a/class.baseclient.php b/class.baseclient.php index 15bf359..7c93df6 100644 --- a/class.baseclient.php +++ b/class.baseclient.php @@ -1,29 +1,24 @@ >>>>>> 0db6ee9df449a4387b5d4da2e0ada2694df44d54 // HubSpot_BaseClient class to be extended by specific hapi clients // Declare variables @@ -69,37 +64,36 @@ class HubSpot_BaseClient const STATUS_NOT_FOUND = 404; /** -<<<<<<< HEAD - * Constructor. - * - *@param HAPIKey: String value of HubSpot API Key for requests - * access_token: String value of Hubspot OAuth Token - * refresh_token: String value of refresh token given initially for OAuth - * client_id: Unique ID for your registered app - * - **/ - public function __construct($HAPIKey=null, $access_token=null, $refresh_token=null,$client_id=null,$userAgent="haPiHP default UserAgent") { // new - - if($HAPIKey AND $access_token){ - throw new Exception("Cannot use hapikey and OAuth token", 1); + * Constructor. + * + * @param HAPIKey: String value of HubSpot API Key for requests + * access_token: String value of Hubspot OAuth Token + * refresh_token: String value of refresh token given initially for OAuth + * client_id: Unique ID for your registered app + * + * */ + public function __construct( $HAPIKey=null, $access_token=null, $refresh_token=null, $client_id=null, $userAgent="haPiHP default UserAgent" ) { // new + + if ( $HAPIKey and $access_token ) { + throw new Exception( "Cannot use hapikey and OAuth token", 1 ); } - else{ + else { $this->HAPIKey = $HAPIKey; - print_r("HAPIKey:".$this->HAPIKey); + print_r( "HAPIKey:".$this->HAPIKey ); $this->ACCESS_TOKEN = $access_token; - print_r("token:".$this->ACCESS_TOKEN); + print_r( "token:".$this->ACCESS_TOKEN ); } - $this->userAgent = $userAgent; // new -======= - * Constructor. - * - * @param $HAPIKey: String value of HubSpot API Key for requests - */ - public function __construct($HAPIKey,$userAgent="haPiHP default UserAgent") - { + $this->userAgent = $userAgent; + + } + /* + * Constructor. + * + * @param $HAPIKey: String value of HubSpot API Key for requests + */ + public function __construct( $HAPIKey, $userAgent="haPiHP default UserAgent" ) { $this->HAPIKey = $HAPIKey; $this->userAgent = $userAgent; ->>>>>>> 0db6ee9df449a4387b5d4da2e0ada2694df44d54 } /** @@ -107,8 +101,7 @@ public function __construct($HAPIKey,$userAgent="haPiHP default UserAgent") * * @return integer */ - public function getLastStatus() - { + public function getLastStatus() { return (int)$this->lastStatus; } @@ -120,10 +113,9 @@ public function getLastStatus() * * @throws HubSpot_Exception */ - protected function get_api() - { - if ( $this->isBlank($this->API_PATH) ) - throw new HubSpot_Exception('API_PATH must be defined'); + protected function get_api() { + if ( $this->isBlank( $this->API_PATH ) ) + throw new HubSpot_Exception( 'API_PATH must be defined' ); else return $this->API_PATH; } @@ -136,10 +128,9 @@ protected function get_api() * * @throws HubSpot_Exception */ - protected function get_api_version() - { - if ( $this->isBlank($this->API_VERSION) ) - throw new HubSpot_Exception('API_VERSION must be defined'); + protected function get_api_version() { + if ( $this->isBlank( $this->API_VERSION ) ) + throw new HubSpot_Exception( 'API_VERSION must be defined' ); else return $this->API_VERSION; } @@ -148,243 +139,227 @@ protected function get_api_version() * Allows developer to set testing flag to true in order to * execute api requests against hubapiqa.com * - * @param $testing: Boolean + * @param unknown $testing: Boolean */ - public function set_is_test($testing) { - if ( $testing == TRUE ) - { + public function set_is_test( $testing ) { + if ( $testing == TRUE ) { $this->isTest = TRUE; } } /** -<<<<<<< HEAD - * Returns the hapi domain to use for requests based on isTesting - * - * @returns: String value of domain, including https protocol - **/ + * Returns the hapi domain to use for requests based on isTesting + * + * @returns: String value of domain, including https protocol + * */ protected function get_domain() { - if ($this->isTest == true){ - return $this->QA_DOMAIN; - } else { - return $this->PROD_DOMAIN; - } - } + if ( $this->isTest == true ) { + return $this->QA_DOMAIN; + } else { + return $this->PROD_DOMAIN; + } + } /** - * Creates the url to be used for the api request. If OAuth token is provided but invalid, will attempt a refresh. - * - * @param endpoint: String value for the endpoint to be used (appears after version in url) - * @param params: Array containing query parameters and values - * - * @returns String - **/ - protected function get_request_url($endpoint,$params) { - $paramstring = $this->array_to_params($params); + * Creates the url to be used for the api request. If OAuth token is provided but invalid, will attempt a refresh. + * + * @param endpoint: String value for the endpoint to be used (appears after version in url) + * @param params: Array containing query parameters and values + * + * @returns String + * */ + protected function get_request_url( $endpoint, $params ) { + $paramstring = $this->array_to_params( $params ); echo $this->HAPIKey; - if($this->HAPIKey){ - print_r("Trying to use hapikey"); + if ( $this->HAPIKey ) { + print_r( "Trying to use hapikey" ); return $this->get_domain() . $this->PATH_DIV . - $this->get_api() . $this->PATH_DIV . - $this->get_api_version() . $this->PATH_DIV . - $endpoint . - $this->API_KEY_PARAM . $this->HAPIKey . - $paramstring; + $this->get_api() . $this->PATH_DIV . + $this->get_api_version() . $this->PATH_DIV . + $endpoint . + $this->API_KEY_PARAM . $this->HAPIKey . + $paramstring; } - else{ + else { print "went to else"; - if($this->check_auth()>=400){ - print_r("Auth check failed"); + if ( $this->check_auth()>=400 ) { + print_r( "Auth check failed" ); try { - $refreshed_token = $this->refresh_access_token($this->REFRESH_TOKEN,$this->CLIENT_ID); + $refreshed_token = $this->refresh_access_token( $this->REFRESH_TOKEN, $this->CLIENT_ID ); $this->ACCESS_TOKEN = $refreshed_token['access_token']; return $this->get_domain() . $this->PATH_DIV . + $this->get_api() . $this->PATH_DIV . + $this->get_api_version() . $this->PATH_DIV . + $endpoint . + $this->TOKEN_PARAM . $this->ACCESS_TOKEN . + $paramstring; + + } catch ( HubSpot_Exception $e ) { + print_r( 'Unable to refresh the OAuth token. Please provide a valid access_token or refresh_token' ); + } + } + else { + return $this->get_domain() . $this->PATH_DIV . $this->get_api() . $this->PATH_DIV . $this->get_api_version() . $this->PATH_DIV . $endpoint . $this->TOKEN_PARAM . $this->ACCESS_TOKEN . $paramstring; - - } catch (HubSpot_Exception $e) { - print_r('Unable to refresh the OAuth token. Please provide a valid access_token or refresh_token'); - } - } - else{ - return $this->get_domain() . $this->PATH_DIV . - $this->get_api() . $this->PATH_DIV . - $this->get_api_version() . $this->PATH_DIV . - $endpoint . - $this->TOKEN_PARAM . $this->ACCESS_TOKEN . - $paramstring; } } -======= + /* * Returns the hapi domain to use for requests based on isTesting * * @returns: String value of domain, including https protocol */ - protected function get_domain() - { - if ( $this->isTest == TRUE ) - return $this->QA_DOMAIN; - else - return $this->PROD_DOMAIN; - } - - /** - * Creates the url to be used for the api request - * - * @param endpoint: String value for the endpoint to be used (appears after version in url) - * @param params: Array containing query parameters and values - * - * @returns String - */ - protected function get_request_url($endpoint, $params) - { - $paramstring = $this->array_to_params($params); - - return $this->get_domain() . $this->PATH_DIV.$this->get_api() . $this->PATH_DIV . $this->get_api_version() . - $this->PATH_DIV . $endpoint . $this->KEY_PARAM . $this->HAPIKey . $paramstring; ->>>>>>> 0db6ee9df449a4387b5d4da2e0ada2694df44d54 - } - - /** - * Creates the url to be used for the api request for Forms API - * - * @param endpoint: String value for the endpoint to be used (appears after version in url) - * @param params: Array containing query parameters and values - * - * @return String - */ - protected function get_forms_request_url($url_base,$params) - { - $paramstring = $this->array_to_params($params); -<<<<<<< HEAD - if($this->ACCESS_TOKEN){ - return $url_base . - $this->TOKEN_PARAM . $this->ACCESS_TOKEN . - $paramstring; + protected function get_domain() { + if ( $this->isTest == TRUE ) + return $this->QA_DOMAIN; + else + return $this->PROD_DOMAIN; } - else{ - return $url_base . - $this->KEY_PARAM . $this->HAPIKey . - $paramstring; + + /** + * Creates the url to be used for the api request + * + * @param endpoint: String value for the endpoint to be used (appears after version in url) + * @param params: Array containing query parameters and values + * + * @returns String + */ + protected function get_request_url( $endpoint, $params ) { + $paramstring = $this->array_to_params( $params ); + + return $this->get_domain() . $this->PATH_DIV.$this->get_api() . $this->PATH_DIV . $this->get_api_version() . + $this->PATH_DIV . $endpoint . $this->KEY_PARAM . $this->HAPIKey . $paramstring; } -======= - return $url_base . $this->KEY_PARAM . $this->HAPIKey . $paramstring; ->>>>>>> 0db6ee9df449a4387b5d4da2e0ada2694df44d54 - } + /** + * Creates the url to be used for the api request for Forms API + * + * @param endpoint: String value for the endpoint to be used (appears after version in url) + * @param params: Array containing query parameters and values + * + * @return String + */ + protected function get_forms_request_url( $url_base, $params ) { + $paramstring = $this->array_to_params( $params ); + if ( $this->ACCESS_TOKEN ) { + return $url_base . + $this->TOKEN_PARAM . $this->ACCESS_TOKEN . + $paramstring; + } + else { + return $url_base . + $this->KEY_PARAM . $this->HAPIKey . + $paramstring; + } - /** - * Executes HTTP GET request - * - * @param URL: String value for the URL to GET - * - * @return Body of request result - * - * @throws HubSpot_Exception - */ - protected function execute_get_request($url) - { - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); // new - $output = curl_exec($ch); - $errno = curl_errno($ch); - $error = curl_error($ch); - $this->setLastStatusFromCurl($ch); - curl_close($ch); + return $url_base . $this->KEY_PARAM . $this->HAPIKey . $paramstring; + } - if ( $errno > 0 ) - throw new HubSpot_Exception('cURL error: ' . $error); - else - return $output; - } + /** + * Executes HTTP GET request + * + * @param URL: String value for the URL to GET + * + * @return Body of request result + * + * @throws HubSpot_Exception + */ + protected function execute_get_request( $url ) { + $ch = curl_init(); + curl_setopt( $ch, CURLOPT_URL, $url ); + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); + curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); + curl_setopt( $ch, CURLOPT_USERAGENT, $this->userAgent ); // new + $output = curl_exec( $ch ); + $errno = curl_errno( $ch ); + $error = curl_error( $ch ); + $this->setLastStatusFromCurl( $ch ); + curl_close( $ch ); + + if ( $errno > 0 ) + throw new HubSpot_Exception( 'cURL error: ' . $error ); + else + return $output; + } - /** - * Executes HTTP POST request - * - * @param URL: String value for the URL to POST to - * @param fields: Array containing names and values for fields to post - * - * @return Body of request result - * - * @throws HubSpot_Exception - */ - protected function execute_post_request($url, $body, $formenc = FALSE) - { - // intialize cURL and send POST data - $ch = curl_init(); - curl_setopt($ch, CURLOPT_POST, true); - curl_setopt($ch, CURLOPT_POSTFIELDS, $body); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); // new -<<<<<<< HEAD - if ($formenc) // new - curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); // new - $output = curl_exec($ch); - $errno = curl_errno($ch); - $error = curl_error($ch); - $this->setLastStatusFromCurl($ch); - curl_close($ch); - if ($errno > 0) { - throw new HubSpot_Exception ('cURL error: ' + $error); + /** + * Executes HTTP POST request + * + * @param URL: String value for the URL to POST to + * @param fields: Array containing names and values for fields to post + * + * @return Body of request result + * + * @throws HubSpot_Exception + */ + protected function execute_post_request( $url, $body, $formenc = FALSE ) { + // intialize cURL and send POST data + $ch = curl_init(); + curl_setopt( $ch, CURLOPT_POST, true ); + curl_setopt( $ch, CURLOPT_POSTFIELDS, $body ); + curl_setopt( $ch, CURLOPT_URL, $url ); + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); + curl_setopt( $ch, CURLOPT_USERAGENT, $this->userAgent ); // new + if ( $formenc ) // new + curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/x-www-form-urlencoded' ) ); // new + $output = curl_exec( $ch ); + $errno = curl_errno( $ch ); + $error = curl_error( $ch ); + $this->setLastStatusFromCurl( $ch ); + curl_close( $ch ); + if ( $errno > 0 ) { + throw new HubSpot_Exception ( 'cURL error: ' + $error ); } else { return $output; } } -======= - if ($formenc) - { - curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); + if ( $formenc ) { + curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/x-www-form-urlencoded' ) ); } - $output = curl_exec($ch); - $errno = curl_errno($ch); - $error = curl_error($ch); - $this->setLastStatusFromCurl($ch); - curl_close($ch); + $output = curl_exec( $ch ); + $errno = curl_errno( $ch ); + $error = curl_error( $ch ); + $this->setLastStatusFromCurl( $ch ); + curl_close( $ch ); if ( $errno > 0 ) - throw new HubSpot_Exception('cURL error: ' . $error); + throw new HubSpot_Exception( 'cURL error: ' . $error ); else return $output; } ->>>>>>> 0db6ee9df449a4387b5d4da2e0ada2694df44d54 /** * Executes HTTP POST request with JSON as the POST body * - * @param URL String value for the URL to POST to - * @param fields Array containing names and values for fields to post + * @param URL String value for the URL to POST to + * @param fields Array containing names and values for fields to post * * @return Body of request result * * @throws HubSpot_Exception */ - protected function execute_JSON_post_request($url, $body) - { + protected function execute_JSON_post_request( $url, $body ) { // intialize cURL and send POST data $ch = curl_init(); - curl_setopt($ch, CURLOPT_POST, true); - curl_setopt($ch, CURLOPT_POSTFIELDS, $body); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); // new - curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); // new - $output = curl_exec($ch); - $errno = curl_errno($ch); - $error = curl_error($ch); - $this->setLastStatusFromCurl($ch); - curl_close($ch); + curl_setopt( $ch, CURLOPT_POST, true ); + curl_setopt( $ch, CURLOPT_POSTFIELDS, $body ); + curl_setopt( $ch, CURLOPT_URL, $url ); + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); + curl_setopt( $ch, CURLOPT_USERAGENT, $this->userAgent ); // new + curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json' ) ); // new + $output = curl_exec( $ch ); + $errno = curl_errno( $ch ); + $error = curl_error( $ch ); + $this->setLastStatusFromCurl( $ch ); + curl_close( $ch ); if ( $errno > 0 ) - throw new HubSpot_Exception('cURL error: ' . $error); + throw new HubSpot_Exception( 'cURL error: ' . $error ); else return $output; } @@ -392,31 +367,30 @@ protected function execute_JSON_post_request($url, $body) /** * Executes HTTP POST request with XML as the POST body * - * @param URL String value for the URL to POST to - * @param fields Array containing names and values for fields to post + * @param URL String value for the URL to POST to + * @param fields Array containing names and values for fields to post * * @return Body of request result * * @throws HubSpot_Exception */ - protected function execute_xml_post_request($url, $body) - { + protected function execute_xml_post_request( $url, $body ) { // intialize cURL and send POST data $ch = curl_init(); - curl_setopt($ch, CURLOPT_POST, true); - curl_setopt($ch, CURLOPT_POSTFIELDS, $body); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); - curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/atom+xml')); - $output = curl_exec($ch); - $errno = curl_errno($ch); - $error = curl_error($ch); - $this->setLastStatusFromCurl($ch); - curl_close($ch); + curl_setopt( $ch, CURLOPT_POST, true ); + curl_setopt( $ch, CURLOPT_POSTFIELDS, $body ); + curl_setopt( $ch, CURLOPT_URL, $url ); + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); + curl_setopt( $ch, CURLOPT_USERAGENT, $this->userAgent ); + curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/atom+xml' ) ); + $output = curl_exec( $ch ); + $errno = curl_errno( $ch ); + $error = curl_error( $ch ); + $this->setLastStatusFromCurl( $ch ); + curl_close( $ch ); if ( $errno > 0 ) - throw new HubSpot_Exception('cURL error: ' . $error); + throw new HubSpot_Exception( 'cURL error: ' . $error ); else return $output; } @@ -424,31 +398,30 @@ protected function execute_xml_post_request($url, $body) /** * Executes HTTP PUT request * - * @param URL String value for the URL to PUT to - * @param body String value of the body of the PUT request + * @param URL String value for the URL to PUT to + * @param body String value of the body of the PUT request * * @return Body of request result * * @throws HubSpot_Exception */ - protected function execute_put_request($url, $body) - { + protected function execute_put_request( $url, $body ) { $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); - curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($body))); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); - curl_setopt($ch, CURLOPT_POSTFIELDS,$body); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); - $result = curl_exec($ch); - $apierr = curl_errno($ch); - $errmsg = curl_error($ch); - $this->setLastStatusFromCurl($ch); - curl_close($ch); + curl_setopt( $ch, CURLOPT_URL, $url ); + curl_setopt( $ch, CURLOPT_USERAGENT, $this->userAgent ); + curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen( $body ) ) ); + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); + curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "PUT" ); + curl_setopt( $ch, CURLOPT_POSTFIELDS, $body ); + curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 ); + $result = curl_exec( $ch ); + $apierr = curl_errno( $ch ); + $errmsg = curl_error( $ch ); + $this->setLastStatusFromCurl( $ch ); + curl_close( $ch ); if ( $apierr > 0 ) - throw new HubSpot_Exception('cURL error: ' . $errmsg); + throw new HubSpot_Exception( 'cURL error: ' . $errmsg ); else return $result; } @@ -456,31 +429,30 @@ protected function execute_put_request($url, $body) /** * Executes HTTP PUT request with XML as the PUT body * - * @param URL String value for the URL to PUT to - * @param body String value of the body of the PUT request + * @param URL String value for the URL to PUT to + * @param body String value of the body of the PUT request * * @return Body of request result * * @throws HubSpot_Exception */ - protected function execute_xml_put_request($url, $body) - { + protected function execute_xml_put_request( $url, $body ) { $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); - curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/atom+xml','Content-Length: ' . strlen($body))); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); - curl_setopt($ch, CURLOPT_POSTFIELDS,$body); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); - $result = curl_exec($ch); - $apierr = curl_errno($ch); - $errmsg = curl_error($ch); - $this->setLastStatusFromCurl($ch); - curl_close($ch); + curl_setopt( $ch, CURLOPT_URL, $url ); + curl_setopt( $ch, CURLOPT_USERAGENT, $this->userAgent ); + curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/atom+xml', 'Content-Length: ' . strlen( $body ) ) ); + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); + curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "PUT" ); + curl_setopt( $ch, CURLOPT_POSTFIELDS, $body ); + curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 ); + $result = curl_exec( $ch ); + $apierr = curl_errno( $ch ); + $errmsg = curl_error( $ch ); + $this->setLastStatusFromCurl( $ch ); + curl_close( $ch ); if ( $apierr > 0 ) - throw new HubSpot_Exception('cURL error: ' . $errmsg); + throw new HubSpot_Exception( 'cURL error: ' . $errmsg ); else return $result; } @@ -488,31 +460,30 @@ protected function execute_xml_put_request($url, $body) /** * Executes HTTP DELETE request * - * @param URL String value for the URL to DELETE to - * @param body String value of the body of the DELETE request + * @param URL String value for the URL to DELETE to + * @param body String value of the body of the DELETE request * * @return Body of request result * * @throws HubSpot_Exception */ - protected function execute_delete_request($url, $body) - { + protected function execute_delete_request( $url, $body ) { $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); - curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($body))); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); - curl_setopt($ch, CURLOPT_POSTFIELDS,$body); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); - $result = curl_exec($ch); - $apierr = curl_errno($ch); - $errmsg = curl_error($ch); - $this->setLastStatusFromCurl($ch); - curl_close($ch); + curl_setopt( $ch, CURLOPT_URL, $url ); + curl_setopt( $ch, CURLOPT_USERAGENT, $this->userAgent ); + curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen( $body ) ) ); + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); + curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "DELETE" ); + curl_setopt( $ch, CURLOPT_POSTFIELDS, $body ); + curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 ); + $result = curl_exec( $ch ); + $apierr = curl_errno( $ch ); + $errmsg = curl_error( $ch ); + $this->setLastStatusFromCurl( $ch ); + curl_close( $ch ); if ( $apierr > 0 ) - throw new HubSpot_Exception('cURL error: ' . $errmsg); + throw new HubSpot_Exception( 'cURL error: ' . $errmsg ); else return $result; } @@ -520,55 +491,38 @@ protected function execute_delete_request($url, $body) /** * Converts an array into url friendly list of parameters * - * @param array params Multidimensional array of parameters (name=>value) + * @param array params Multidimensional array of parameters (name=>value) * * @return String of url friendly parameters (&name=value&foo=bar) */ - protected function array_to_params($params) - { + protected function array_to_params( $params ) { $paramstring = ''; -<<<<<<< HEAD - if ($params != null) { - foreach ($params as $parameter => $value) { - $paramstring = $paramstring . '&' . $parameter . '=' . urlencode($value); - } - } - return $paramstring; - } -======= - - if ( $params != NULL ) - { - foreach ( $params as $parameter => $value ) - { - if ( is_array($value) ) - { - foreach ( $value as $subparam ) - { - $paramstring = $paramstring . '&' . $parameter . '=' . urlencode($subparam); + + + if ( $params != NULL ) { + foreach ( $params as $parameter => $value ) { + if ( is_array( $value ) ) { + foreach ( $value as $subparam ) { + $paramstring = $paramstring . '&' . $parameter . '=' . urlencode( $subparam ); } } - else - { - $paramstring = $paramstring . '&' . $parameter . '=' . urlencode($value); + else { + $paramstring = $paramstring . '&' . $parameter . '=' . urlencode( $value ); } } } - return $paramstring; } ->>>>>>> 0db6ee9df449a4387b5d4da2e0ada2694df44d54 /** * Utility function used to determine if variable is empty * - * @param s Variable to be evaluated + * @param s Variable to be evaluated * * @returns Boolean */ - protected function isBlank ($s) - { - if ( (trim($s)=='') OR ($s == NULL) ) + protected function isBlank( $s ) { + if ( ( trim( $s )=='' ) or ( $s == NULL ) ) return true; else return false; @@ -579,75 +533,70 @@ protected function isBlank ($s) * * @param resource $ch */ - protected function setLastStatusFromCurl($ch) - { - $info = curl_getinfo($ch); - $this->lastStatus = (isset($info['http_code'])) ? $info['http_code'] : null; + protected function setLastStatusFromCurl( $ch ) { + $info = curl_getinfo( $ch ); + $this->lastStatus = ( isset( $info['http_code'] ) ) ? $info['http_code'] : null; } -<<<<<<< HEAD /** - * Quick check of access_token - * - * - * @return: Response code for request - * - **/ - protected function check_auth(){ + * Quick check of access_token + * + * + * @return: Response code for request + * + * */ + protected function check_auth() { $url = 'https://api.hubapi.com/contacts/v1/properties/email?access_token='.$this->ACCESS_TOKEN; - print_r($url); + print_r( $url ); $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); - $output = curl_exec($ch); - $response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); - curl_close($ch); + curl_setopt( $ch, CURLOPT_URL, $url ); + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); + curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); + curl_setopt( $ch, CURLOPT_USERAGENT, $this->userAgent ); + $output = curl_exec( $ch ); + $response_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); + curl_close( $ch ); return $response_code; } /** - * Refresh OAuth token - * - * @param refresh_token: The refresh token for your portal - * @param client_id: Unique ID for your app, generated when the app is registered - * - * @return: Body of request result - * - * @throws HubSpot_Exception - **/ - protected function refresh_access_token($refresh_token, $client_id){ - if($refresh_token AND $client_id){ + * Refresh OAuth token + * + * @param refresh_token: The refresh token for your portal + * @param client_id: Unique ID for your app, generated when the app is registered + * + * @return: Body of request result + * + * @throws HubSpot_Exception + * */ + protected function refresh_access_token( $refresh_token, $client_id ) { + if ( $refresh_token and $client_id ) { $url = 'https://api.hubapi.com/auth/v1/refresh'; $body = 'refresh_token='.$refresh_token.'&client_id='.$client_id.'&grant_type=refresh_token'; $ch = curl_init(); - curl_setopt($ch, CURLOPT_POST, true); - curl_setopt($ch, CURLOPT_POSTFIELDS, $body); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); // new - curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); - $output = curl_exec($ch); - $apierr = curl_errno($ch); - $errmsg = curl_error($ch); - $response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); - curl_close($ch); - $output_array = json_decode($output); - if ($response_code<400) { - return $output_array; - } else { - throw new HubSpot_Exception("cURL error: " + $errmsg); - - } - } - else{ - throw new HubSpot_Exception("Please provide a refresh_token and client_id"); - } + curl_setopt( $ch, CURLOPT_POST, true ); + curl_setopt( $ch, CURLOPT_POSTFIELDS, $body ); + curl_setopt( $ch, CURLOPT_URL, $url ); + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); + curl_setopt( $ch, CURLOPT_USERAGENT, $this->userAgent ); // new + curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/x-www-form-urlencoded' ) ); + $output = curl_exec( $ch ); + $apierr = curl_errno( $ch ); + $errmsg = curl_error( $ch ); + $response_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); + curl_close( $ch ); + $output_array = json_decode( $output ); + if ( $response_code<400 ) { + return $output_array; + } else { + throw new HubSpot_Exception( "cURL error: " + $errmsg ); + } + } + else { + throw new HubSpot_Exception( "Please provide a refresh_token and client_id" ); + } -} + } -======= ->>>>>>> 0db6ee9df449a4387b5d4da2e0ada2694df44d54 } From ea007944f163ca0b85eae1e47a1ed66fbf0f71e1 Mon Sep 17 00:00:00 2001 From: Webster Gordon Date: Mon, 3 Mar 2014 16:34:31 -0500 Subject: [PATCH 10/11] cleaned up git diffs --- class.baseclient.php | 73 +++++--------------------------------------- 1 file changed, 8 insertions(+), 65 deletions(-) diff --git a/class.baseclient.php b/class.baseclient.php index 68a391e..97bc7b1 100644 --- a/class.baseclient.php +++ b/class.baseclient.php @@ -64,7 +64,6 @@ class HubSpot_BaseClient { const STATUS_NOT_FOUND = 404; /** -<<<<<<< HEAD * Constructor. * * @param HAPIKey: String value of HubSpot API Key for requests @@ -79,39 +78,10 @@ public function __construct( $HAPIKey=null, $access_token=null, $refresh_token=n throw new Exception( "Cannot use hapikey and OAuth token", 1 ); } else { -======= - * Constructor. - * - *@param auth: array with the following optional parameters. Either an API key or and OAuth token must be used for authentication. - * HAPIKey: String value of HubSpot API Key for requests - * access_token: String value of Hubspot OAuth Token - * refresh_token: String value of refresh token given initially for OAuth - * client_id: Unique ID for your registered app - * - **/ - public function __construct($auth,$userAgent="haPiHP default UserAgent") { // new - - if(isset($auth['HAPIKey']) AND isset($auth['access_token'])){ - throw new Exception("Cannot use hapikey and OAuth token", 1); - } - else{ -<<<<<<< HEAD - $auth += array('HAPIKey'=>null,'access_token'=>null,'refresh_token'=>null,'client_id'=>null); - $this->HAPIKey = $auth['HAPIKey']; - $this->ACCESS_TOKEN = $auth['access_token']; - $this->REFRESH_TOKEN = $auth['refresh_token']; - $this->CLIENT_ID = $auth['client_id']; -======= ->>>>>>> array_param $this->HAPIKey = $HAPIKey; print_r( "HAPIKey:".$this->HAPIKey ); $this->ACCESS_TOKEN = $access_token; -<<<<<<< HEAD print_r( "token:".$this->ACCESS_TOKEN ); -======= - print_r("token:".$this->ACCESS_TOKEN); ->>>>>>> master ->>>>>>> array_param } $this->userAgent = $userAgent; @@ -237,12 +207,11 @@ protected function get_request_url( $endpoint, $params ) { $paramstring; } } -<<<<<<< HEAD /* - * Returns the hapi domain to use for requests based on isTesting - * - * @returns: String value of domain, including https protocol - */ + * Returns the hapi domain to use for requests based on isTesting + * + * @returns: String value of domain, including https protocol + */ protected function get_domain() { if ( $this->isTest == TRUE ) return $this->QA_DOMAIN; @@ -264,23 +233,7 @@ protected function get_request_url( $endpoint, $params ) { return $this->get_domain() . $this->PATH_DIV.$this->get_api() . $this->PATH_DIV . $this->get_api_version() . $this->PATH_DIV . $endpoint . $this->KEY_PARAM . $this->HAPIKey . $paramstring; } -======= - } - /** - * Creates the url to be used for the api request for Forms API - * - * @param endpoint: String value for the endpoint to be used (appears after version in url) - * @param params: Array containing query parameters and values - * - * @returns String - **/ - protected function get_forms_request_url($url_base,$params) { - $paramstring = $this->array_to_params($params); - return $url_base . - $paramstring; - } ->>>>>>> array_param /** * Creates the url to be used for the api request for Forms API @@ -288,22 +241,12 @@ protected function get_forms_request_url($url_base,$params) { * @param endpoint: String value for the endpoint to be used (appears after version in url) * @param params: Array containing query parameters and values * - * @return String - */ + * @returns String + * */ protected function get_forms_request_url( $url_base, $params ) { $paramstring = $this->array_to_params( $params ); - if ( $this->ACCESS_TOKEN ) { - return $url_base . - $this->TOKEN_PARAM . $this->ACCESS_TOKEN . - $paramstring; - } - else { - return $url_base . - $this->KEY_PARAM . $this->HAPIKey . - $paramstring; - } - - return $url_base . $this->KEY_PARAM . $this->HAPIKey . $paramstring; + return $url_base . + $paramstring; } /** From f8d58ce045caa76e38fb247dd3523e6ddc270a9c Mon Sep 17 00:00:00 2001 From: Webster Gordon Date: Tue, 4 Mar 2014 11:33:44 -0500 Subject: [PATCH 11/11] final round of cleanup --- class.baseclient.php | 183 ++++++++++++++++--------------------------- 1 file changed, 66 insertions(+), 117 deletions(-) diff --git a/class.baseclient.php b/class.baseclient.php index 97bc7b1..fc90052 100644 --- a/class.baseclient.php +++ b/class.baseclient.php @@ -34,7 +34,7 @@ class HubSpot_BaseClient { protected $TOKEN_PARAM = '?access_token='; protected $PROD_DOMAIN = 'https://api.hubapi.com'; protected $QA_DOMAIN = 'https://hubapiqa.com'; - protected $userAgent; // new + protected $userAgent; /** * The HTTP status of the most recent request @@ -72,7 +72,7 @@ class HubSpot_BaseClient { * client_id: Unique ID for your registered app * * */ - public function __construct( $HAPIKey=null, $access_token=null, $refresh_token=null, $client_id=null, $userAgent="haPiHP default UserAgent" ) { // new + public function __construct( $HAPIKey=null, $access_token=null, $refresh_token=null, $client_id=null, $userAgent="haPiHP default UserAgent" ) { if ( $HAPIKey and $access_token ) { throw new Exception( "Cannot use hapikey and OAuth token", 1 ); @@ -86,15 +86,6 @@ public function __construct( $HAPIKey=null, $access_token=null, $refresh_token=n $this->userAgent = $userAgent; } - /* - * Constructor. - * - * @param $HAPIKey: String value of HubSpot API Key for requests - */ - public function __construct( $HAPIKey, $userAgent="haPiHP default UserAgent" ) { - $this->HAPIKey = $HAPIKey; - $this->userAgent = $userAgent; - } /** * Gets the status code from the most recent curl request @@ -207,111 +198,37 @@ protected function get_request_url( $endpoint, $params ) { $paramstring; } } - /* - * Returns the hapi domain to use for requests based on isTesting - * - * @returns: String value of domain, including https protocol - */ - protected function get_domain() { - if ( $this->isTest == TRUE ) - return $this->QA_DOMAIN; - else - return $this->PROD_DOMAIN; - } - - /** - * Creates the url to be used for the api request - * - * @param endpoint: String value for the endpoint to be used (appears after version in url) - * @param params: Array containing query parameters and values - * - * @returns String - */ - protected function get_request_url( $endpoint, $params ) { - $paramstring = $this->array_to_params( $params ); - - return $this->get_domain() . $this->PATH_DIV.$this->get_api() . $this->PATH_DIV . $this->get_api_version() . - $this->PATH_DIV . $endpoint . $this->KEY_PARAM . $this->HAPIKey . $paramstring; - } - - - /** - * Creates the url to be used for the api request for Forms API - * - * @param endpoint: String value for the endpoint to be used (appears after version in url) - * @param params: Array containing query parameters and values - * - * @returns String - * */ - protected function get_forms_request_url( $url_base, $params ) { - $paramstring = $this->array_to_params( $params ); - return $url_base . - $paramstring; - } - - /** - * Executes HTTP GET request - * - * @param URL: String value for the URL to GET - * - * @return Body of request result - * - * @throws HubSpot_Exception - */ - protected function execute_get_request( $url ) { - $ch = curl_init(); - curl_setopt( $ch, CURLOPT_URL, $url ); - curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); - curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); - curl_setopt( $ch, CURLOPT_USERAGENT, $this->userAgent ); // new - $output = curl_exec( $ch ); - $errno = curl_errno( $ch ); - $error = curl_error( $ch ); - $this->setLastStatusFromCurl( $ch ); - curl_close( $ch ); - - if ( $errno > 0 ) - throw new HubSpot_Exception( 'cURL error: ' . $error ); - else - return $output; - } - - /** - * Executes HTTP POST request - * - * @param URL: String value for the URL to POST to - * @param fields: Array containing names and values for fields to post - * - * @return Body of request result - * - * @throws HubSpot_Exception - */ - protected function execute_post_request( $url, $body, $formenc = FALSE ) { - // intialize cURL and send POST data - $ch = curl_init(); - curl_setopt( $ch, CURLOPT_POST, true ); - curl_setopt( $ch, CURLOPT_POSTFIELDS, $body ); - curl_setopt( $ch, CURLOPT_URL, $url ); - curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); - curl_setopt( $ch, CURLOPT_USERAGENT, $this->userAgent ); // new - if ( $formenc ) // new - curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/x-www-form-urlencoded' ) ); // new - $output = curl_exec( $ch ); - $errno = curl_errno( $ch ); - $error = curl_error( $ch ); - $this->setLastStatusFromCurl( $ch ); - curl_close( $ch ); - if ( $errno > 0 ) { - throw new HubSpot_Exception ( 'cURL error: ' + $error ); - } else { - return $output; - } - } + } - if ( $formenc ) { - curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/x-www-form-urlencoded' ) ); - } + /** + * Creates the url to be used for the api request for Forms API + * + * @param endpoint: String value for the endpoint to be used (appears after version in url) + * @param params: Array containing query parameters and values + * + * @returns String + * */ + protected function get_forms_request_url( $url_base, $params ) { + $paramstring = $this->array_to_params( $params ); + return $url_base . + $paramstring; + } + /** + * Executes HTTP GET request + * + * @param URL: String value for the URL to GET + * + * @return Body of request result + * + * @throws HubSpot_Exception + */ + protected function execute_get_request( $url ) { + $ch = curl_init(); + curl_setopt( $ch, CURLOPT_URL, $url ); + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); + curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); + curl_setopt( $ch, CURLOPT_USERAGENT, $this->userAgent ); $output = curl_exec( $ch ); $errno = curl_errno( $ch ); $error = curl_error( $ch ); @@ -324,6 +241,38 @@ protected function execute_post_request( $url, $body, $formenc = FALSE ) { return $output; } + /** + * Executes HTTP POST request + * + * @param URL: String value for the URL to POST to + * @param fields: Array containing names and values for fields to post + * + * @return Body of request result + * + * @throws HubSpot_Exception + */ + protected function execute_post_request( $url, $body, $formenc = FALSE ) { + // intialize cURL and send POST data + $ch = curl_init(); + curl_setopt( $ch, CURLOPT_POST, true ); + curl_setopt( $ch, CURLOPT_POSTFIELDS, $body ); + curl_setopt( $ch, CURLOPT_URL, $url ); + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); + curl_setopt( $ch, CURLOPT_USERAGENT, $this->userAgent ); + if ( $formenc ) + curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/x-www-form-urlencoded' ) ); + $output = curl_exec( $ch ); + $errno = curl_errno( $ch ); + $error = curl_error( $ch ); + $this->setLastStatusFromCurl( $ch ); + curl_close( $ch ); + if ( $errno > 0 ) { + throw new HubSpot_Exception ( 'cURL error: ' + $error ); + } else { + return $output; + } + } + /** * Executes HTTP POST request with JSON as the POST body * @@ -341,8 +290,8 @@ protected function execute_JSON_post_request( $url, $body ) { curl_setopt( $ch, CURLOPT_POSTFIELDS, $body ); curl_setopt( $ch, CURLOPT_URL, $url ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); - curl_setopt( $ch, CURLOPT_USERAGENT, $this->userAgent ); // new - curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json' ) ); // new + curl_setopt( $ch, CURLOPT_USERAGENT, $this->userAgent ); + curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json' ) ); $output = curl_exec( $ch ); $errno = curl_errno( $ch ); $error = curl_error( $ch ); @@ -568,7 +517,7 @@ protected function refresh_access_token( $refresh_token, $client_id ) { curl_setopt( $ch, CURLOPT_POSTFIELDS, $body ); curl_setopt( $ch, CURLOPT_URL, $url ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); - curl_setopt( $ch, CURLOPT_USERAGENT, $this->userAgent ); // new + curl_setopt( $ch, CURLOPT_USERAGENT, $this->userAgent ); curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/x-www-form-urlencoded' ) ); $output = curl_exec( $ch ); $apierr = curl_errno( $ch );