Skip to content

Commit

Permalink
FIX: allow CURLOPT_CONNECTTIMEOUT to be configured.
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Dec 1, 2013
1 parent df2d59d commit 0d493d4
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions api/RestfulService.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
<?php
/**
* RestfulService class allows you to consume various RESTful APIs.
*
* Through this you could connect and aggregate data of various web services.
* For more info visit wiki documentation - http://doc.silverstripe.org/doku.php?id=restfulservice
*
* @see http://doc.silverstripe.org/framework/en/reference/restfulservice
*
* @package framework
* @subpackage integration
*/
class RestfulService extends ViewableData {

protected $baseURL;
protected $queryString;
protected $errorTag;
protected $checkErrors;
protected $connectTimeout = 5;
protected $cache_expire;
protected $authUsername, $authPassword;
protected $customHeaders = array();
Expand Down Expand Up @@ -213,15 +217,14 @@ public function request($subURL = '', $method = "GET", $data = null, $headers =
*/
public function curlRequest($url, $method, $data = null, $headers = null, $curlOptions = array()) {
$ch = curl_init();
$timeout = 5;
$sapphireInfo = new SapphireInfo();
$useragent = 'SilverStripe/' . $sapphireInfo->Version();
$curlOptions = $curlOptions + (array)$this->config()->default_curl_options;

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->getConnectTimeout());
if(!ini_get('open_basedir')) curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);

Expand Down Expand Up @@ -553,6 +556,31 @@ public function searchAttributes($xml, $node=NULL){

return $output;
}

/**
* Set the connection timeout for the curl request in seconds.
*
* @see http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTCONNECTTIMEOUT
*
* @param int
*
* @return RestfulService
*/
public function setConnectTimeout($timeout) {
$this->connectTimeout = $timeout;

return $this;
}

/**
* Return the connection timeout value.
*
* @return int
*/
public function getConnectTimeout() {
return $this->connectTimeout;
}

}

/**
Expand Down

0 comments on commit 0d493d4

Please sign in to comment.