Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docblocks and set curl timeout on the class #84

Merged
merged 3 commits into from
Dec 7, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"require": {
"php": ">=5.3.0",
"lib-curl": "*"
"ext-curl": "*"
},
"autoload": {
"classmap": [
Expand Down
64 changes: 61 additions & 3 deletions includes/ActiveCampaign.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,90 @@

require_once("Connector.class.php");

/**
* Class ActiveCampaign
*/
class ActiveCampaign extends AC_Connector {

/**
* @var
*/
public $url_base;

/**
* @var
*/
public $url;

/**
* @var
*/
public $api_key;

/**
* @var
*/
public $track_email;

/**
* @var
*/
public $track_actid;

/**
* @var
*/
public $track_key;

/**
* @var int
*/
public $version = 1;

/**
* @var bool
*/
public $debug = false;

/**
* @var string
*/
public $curl_response_error = "";

/**
* ActiveCampaign constructor.
*
* @param $url
* @param $api_key
* @param string $api_user
* @param string $api_pass
*/
function __construct($url, $api_key, $api_user = "", $api_pass = "") {
$this->url_base = $this->url = $url;
$this->api_key = $api_key;
parent::__construct($url, $api_key, $api_user, $api_pass);
}

/**
* Set the version on the url
*
* @param $version
*/
function version($version) {
$this->version = (int)$version;
if ($version == 2) {
$this->url_base = $this->url_base . "/2";
}
}

/**
* Make api calls
*
* @param $path
* @param array $post_data
*
* @return mixed
*/
function api($path, $post_data = array()) {
// IE: "contact/view"
$components = explode("/", $path);
Expand Down Expand Up @@ -91,7 +150,8 @@ function api($path, $post_data = array()) {
}

$class = new $class($this->version, $this->url_base, $this->url, $this->api_key);
// IE: $contact->view()

$class->set_curl_timeout($this->get_curl_timeout());

if ($add_tracking) {
$class->track_email = $this->track_email;
Expand Down Expand Up @@ -130,5 +190,3 @@ function api($path, $post_data = array()) {
require_once("Tracking.class.php");
require_once("User.class.php");
require_once("Webhook.class.php");

?>
129 changes: 120 additions & 9 deletions includes/Connector.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,44 @@

require_once(dirname(__FILE__) . "/exceptions/RequestException.php");

/**
* Class AC_Connector
*/
class AC_Connector {

const DEFAULT_TIMEOUT = 30;
/**
* Default curl timeout
*/
const DEFAULT_TIMEOUT = 30;

/**
* @var string
*/
public $url;

/**
* @var
*/
public $api_key;

/**
* @var string
*/
public $output = "json";

/**
* @var int
*/
private $timeout = self::DEFAULT_TIMEOUT;

/**
* AC_Connector constructor.
*
* @param $url
* @param $api_key
* @param string $api_user
* @param string $api_pass
*/
function __construct($url, $api_key, $api_user = "", $api_pass = "") {
// $api_pass should be md5() already
$base = "";
Expand All @@ -31,6 +60,12 @@ function __construct($url, $api_key, $api_user = "", $api_pass = "") {
$this->api_key = $api_key;
}

/**
* Test the api credentials
*
* @return bool|mixed
* @throws \RequestException
*/
public function credentials_test() {
$test_url = "{$this->url}&api_action=user_me&api_output={$this->output}";
$r = $this->curl($test_url);
Expand All @@ -45,7 +80,14 @@ public function credentials_test() {
return $r;
}

// debug function (nicely outputs variables)
/**
* Debug helper function
*
* @param $var
* @param int $continue
* @param string $element
* @param string $extra
*/
public function dbg($var, $continue = 0, $element = "pre", $extra = "") {
echo "<" . $element . ">";
echo "Vartype: " . gettype($var) . "\n";
Expand All @@ -60,14 +102,35 @@ public function dbg($var, $continue = 0, $element = "pre", $extra = "") {
if (!$continue) exit();
}

/**
* Set curl timeout
*
* @param $seconds
*/
public function set_curl_timeout($seconds) {
$this->timeout = $seconds;
}

/**
* Get curl timeout
*
* @return int
*/
public function get_curl_timeout() {
return $this->timeout;
}

/**
* Make the curl request
*
* @param $url
* @param array $params_data
* @param string $verb
* @param string $custom_method
*
* @return mixed
* @throws \RequestException
*/
public function curl($url, $params_data = array(), $verb = "", $custom_method = "") {
if ($this->version == 1) {
// find the method from the URL.
Expand All @@ -82,12 +145,17 @@ public function curl($url, $params_data = array(), $verb = "", $custom_method =
$method = $custom_method;
$url .= "?api_key=" . $this->api_key;
}

$debug_str1 = "";

$request = curl_init();

$debug_str1 .= "\$ch = curl_init();\n";

curl_setopt($request, CURLOPT_HEADER, 0);
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($request, CURLOPT_TIMEOUT, $this->timeout);

$debug_str1 .= "curl_setopt(\$ch, CURLOPT_HEADER, 0);\n";
$debug_str1 .= "curl_setopt(\$ch, CURLOPT_RETURNTRANSFER, true);\n";
$debug_str1 .= "curl_setopt(\$ch, CURLOPT_TIMEOUT, " . $this->timeout . ");\n";
Expand All @@ -109,20 +177,26 @@ public function curl($url, $params_data = array(), $verb = "", $custom_method =
$verb = "GET";
}
}

$debug_str1 .= "curl_setopt(\$ch, CURLOPT_URL, \"" . $url . "\");\n";

if ($this->debug) {
$this->dbg($url, 1, "pre", "Description: Request URL");
}

if ($verb == "POST" || $verb == "PUT" || $verb == "DELETE") {
if ($verb == "PUT") {
curl_setopt($request, CURLOPT_CUSTOMREQUEST, "PUT");

$debug_str1 .= "curl_setopt(\$ch, CURLOPT_CUSTOMREQUEST, \"PUT\");\n";
} elseif ($verb == "DELETE") {
curl_setopt($request, CURLOPT_CUSTOMREQUEST, "DELETE");

$debug_str1 .= "curl_setopt(\$ch, CURLOPT_CUSTOMREQUEST, \"DELETE\");\n";
} else {
$verb = "POST";
curl_setopt($request, CURLOPT_POST, 1);

$debug_str1 .= "curl_setopt(\$ch, CURLOPT_POST, 1);\n";
}
$data = "";
Expand Down Expand Up @@ -172,44 +246,68 @@ public function curl($url, $params_data = array(), $verb = "", $custom_method =
}

$data = rtrim($data, "& ");

curl_setopt($request, CURLOPT_HTTPHEADER, array("Expect:"));

$debug_str1 .= "curl_setopt(\$ch, CURLOPT_HTTPHEADER, array(\"Expect:\"));\n";

if ($this->debug) {
curl_setopt($request, CURLINFO_HEADER_OUT, 1);

$debug_str1 .= "curl_setopt(\$ch, CURLINFO_HEADER_OUT, 1);\n";

$this->dbg($data, 1, "pre", "Description: POST data");
}

curl_setopt($request, CURLOPT_POSTFIELDS, $data);

$debug_str1 .= "curl_setopt(\$ch, CURLOPT_POSTFIELDS, \"" . $data . "\");\n";
}

curl_setopt($request, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($request, CURLOPT_SSL_VERIFYHOST, 0);

$debug_str1 .= "curl_setopt(\$ch, CURLOPT_SSL_VERIFYPEER, false);\n";
$debug_str1 .= "curl_setopt(\$ch, CURLOPT_SSL_VERIFYHOST, 0);\n";

$response = curl_exec($request);

$curl_error = curl_error($request);
if (!$response && $curl_error) {
return $curl_error;

if (! $response && $curl_error) {
$this->throwRequestException($curl_error);
}

$debug_str1 .= "curl_exec(\$ch);\n";

if ($this->debug) {
$this->dbg($response, 1, "pre", "Description: Raw response");
}

$http_code = curl_getinfo($request, CURLINFO_HTTP_CODE);
if (!preg_match("/^[2-3][0-9]{2}/", $http_code)) {
// If not 200 or 300 range HTTP code, return custom error.
return "HTTP code $http_code returned";
}

$debug_str1 .= "\$http_code = curl_getinfo(\$ch, CURLINFO_HTTP_CODE);\n";

if ($this->debug) {
$this->dbg($http_code, 1, "pre", "Description: Response HTTP code");

$request_headers = curl_getinfo($request, CURLINFO_HEADER_OUT);

$debug_str1 .= "\$request_headers = curl_getinfo(\$ch, CURLINFO_HEADER_OUT);\n";

$this->dbg($request_headers, 1, "pre", "Description: Request headers");
}

curl_close($request);

$debug_str1 .= "curl_close(\$ch);\n";

$object = json_decode($response);

if ($this->debug) {
$this->dbg($object, 1, "pre", "Description: Response object (json_decode)");
}
Expand All @@ -220,9 +318,7 @@ public function curl($url, $params_data = array(), $verb = "", $custom_method =
return $response;
}

$requestException = new RequestException;
$requestException->setFailedMessage($response);
throw $requestException;
$this->throwRequestException($response);
}

if ($this->debug) {
Expand All @@ -233,18 +329,33 @@ public function curl($url, $params_data = array(), $verb = "", $custom_method =

if (isset($object->result_code)) {
$object->success = $object->result_code;

if (!(int)$object->result_code) {
$object->error = $object->result_message;
}
}
elseif (isset($object->succeeded)) {
} elseif (isset($object->succeeded)) {
// some calls return "succeeded" only
$object->success = $object->succeeded;

if (!(int)$object->succeeded) {
$object->error = $object->message;
}
}

return $object;
}

/**
* Throw the request exception
*
* @param $message
*
* @throws \RequestException
*/
protected function throwRequestException($message) {
$requestException = new RequestException;
$requestException->setFailedMessage($message);

throw $requestException;
}
}