From 5909248bf42ef7d5be9845740868ec78217b3036 Mon Sep 17 00:00:00 2001 From: Rob Henley Date: Wed, 16 Aug 2017 17:01:44 -0500 Subject: [PATCH 1/2] Add the ability to set a optional timeout --- README.md | 6 ++++++ includes/ActiveCampaign.class.php | 4 +++- includes/Connector.class.php | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0912515..54dff1a 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,9 @@ That's it! $ac = new ActiveCampaign(ACTIVECAMPAIGN_URL, ACTIVECAMPAIGN_API_KEY); + // Optionally set a curl timeout + $ac->set_curl_timeout(10); + $account = $ac->api("account/view"); Or just include everything in the same PHP file: @@ -64,6 +67,9 @@ Or just include everything in the same PHP file: require_once("includes/ActiveCampaign.class.php"); $ac = new ActiveCampaign(ACTIVECAMPAIGN_URL, ACTIVECAMPAIGN_API_KEY); + // Optionally set a curl timeout + $ac->set_curl_timeout(10); + $account = $ac->api("account/view"); See our [examples file](examples.php) for more in-depth samples. diff --git a/includes/ActiveCampaign.class.php b/includes/ActiveCampaign.class.php index 56b7fde..b9f3e79 100644 --- a/includes/ActiveCampaign.class.php +++ b/includes/ActiveCampaign.class.php @@ -93,6 +93,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; $class->track_actid = $this->track_actid; @@ -131,4 +133,4 @@ function api($path, $post_data = array()) { require_once("User.class.php"); require_once("Webhook.class.php"); -?> \ No newline at end of file +?> diff --git a/includes/Connector.class.php b/includes/Connector.class.php index 81f68e8..d3899c7 100644 --- a/includes/Connector.class.php +++ b/includes/Connector.class.php @@ -7,6 +7,7 @@ class AC_Connector { public $url; public $api_key; public $output = "json"; + private $timeout = NULL; function __construct($url, $api_key, $api_user = "", $api_pass = "") { // $api_pass should be md5() already @@ -57,6 +58,14 @@ public function dbg($var, $continue = 0, $element = "pre", $extra = "") { if (!$continue) exit(); } + public function set_curl_timeout($seconds) { + $this->timeout = $seconds; + } + + public function get_curl_timeout() { + return $this->timeout; + } + public function curl($url, $params_data = array(), $verb = "", $custom_method = "") { if ($this->version == 1) { // find the method from the URL. @@ -78,6 +87,12 @@ public function curl($url, $params_data = array(), $verb = "", $custom_method = curl_setopt($request, CURLOPT_RETURNTRANSFER, true); $debug_str1 .= "curl_setopt(\$ch, CURLOPT_HEADER, 0);\n"; $debug_str1 .= "curl_setopt(\$ch, CURLOPT_RETURNTRANSFER, true);\n"; + + if (!is_null($this->timeout)) { + curl_setopt($request, CURLOPT_TIMEOUT, $this->timeout); + $debug_str1 .= "curl_setopt(\$ch, CURLOPT_TIMEOUT, " . $this->timeout . ");\n"; + } + if ($params_data && $verb == "GET") { if ($this->version == 2) { $url .= "&" . $params_data; From a70882164cd7d00bffffb7153221bf8a58de00af Mon Sep 17 00:00:00 2001 From: Rob Henley Date: Mon, 4 Dec 2017 12:47:59 -0600 Subject: [PATCH 2/2] Explicitly set a default cURL timeout of 30 seconds --- README.md | 4 ++-- includes/Connector.class.php | 11 +++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 54dff1a..bf83375 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ That's it! $ac = new ActiveCampaign(ACTIVECAMPAIGN_URL, ACTIVECAMPAIGN_API_KEY); - // Optionally set a curl timeout + // Adjust the default cURL timeout $ac->set_curl_timeout(10); $account = $ac->api("account/view"); @@ -67,7 +67,7 @@ Or just include everything in the same PHP file: require_once("includes/ActiveCampaign.class.php"); $ac = new ActiveCampaign(ACTIVECAMPAIGN_URL, ACTIVECAMPAIGN_API_KEY); - // Optionally set a curl timeout + // Adjust the default cURL timeout $ac->set_curl_timeout(10); $account = $ac->api("account/view"); diff --git a/includes/Connector.class.php b/includes/Connector.class.php index d3899c7..f12842c 100644 --- a/includes/Connector.class.php +++ b/includes/Connector.class.php @@ -4,10 +4,12 @@ class AC_Connector { + const DEFAULT_TIMEOUT = 30; + public $url; public $api_key; public $output = "json"; - private $timeout = NULL; + private $timeout = self::DEFAULT_TIMEOUT; function __construct($url, $api_key, $api_user = "", $api_pass = "") { // $api_pass should be md5() already @@ -85,13 +87,10 @@ public function curl($url, $params_data = array(), $verb = "", $custom_method = $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"; - - if (!is_null($this->timeout)) { - curl_setopt($request, CURLOPT_TIMEOUT, $this->timeout); - $debug_str1 .= "curl_setopt(\$ch, CURLOPT_TIMEOUT, " . $this->timeout . ");\n"; - } + $debug_str1 .= "curl_setopt(\$ch, CURLOPT_TIMEOUT, " . $this->timeout . ");\n"; if ($params_data && $verb == "GET") { if ($this->version == 2) {