diff --git a/README.md b/README.md index 0912515..bf83375 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,9 @@ That's it! $ac = new ActiveCampaign(ACTIVECAMPAIGN_URL, ACTIVECAMPAIGN_API_KEY); + // Adjust the default 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); + // Adjust the default 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..f12842c 100644 --- a/includes/Connector.class.php +++ b/includes/Connector.class.php @@ -4,9 +4,12 @@ class AC_Connector { + const DEFAULT_TIMEOUT = 30; + public $url; public $api_key; public $output = "json"; + private $timeout = self::DEFAULT_TIMEOUT; function __construct($url, $api_key, $api_user = "", $api_pass = "") { // $api_pass should be md5() already @@ -57,6 +60,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. @@ -76,8 +87,11 @@ 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"; + $debug_str1 .= "curl_setopt(\$ch, CURLOPT_TIMEOUT, " . $this->timeout . ");\n"; + if ($params_data && $verb == "GET") { if ($this->version == 2) { $url .= "&" . $params_data;