From 028d59ffc3b10329985deb769c1faf88d0af34d6 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Mon, 12 Apr 2010 03:39:14 +0000 Subject: [PATCH] ENHANCEMENT Added RestfulService::set_default_proxy() and RestfulService->setProxy() (#4637, thanks hamish) (from r97192) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@102432 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- api/RestfulService.php | 46 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/api/RestfulService.php b/api/RestfulService.php index 62481c476..06df5ddca 100644 --- a/api/RestfulService.php +++ b/api/RestfulService.php @@ -14,6 +14,26 @@ class RestfulService extends ViewableData { protected $cache_expire; protected $authUsername, $authPassword; protected $customHeaders = array(); + protected $proxy; + protected static $default_proxy; + + /** + * Sets default proxy settings for outbound RestfulService connections + * + * @param string $proxy The URL of the proxy to use. + * @param int $port Proxy port + * @param string $user The proxy auth user name + * @param string $password The proxy auth password + * @param boolean $socks Set true to use socks5 proxy instead of http + */ + static function set_default_proxy($proxy, $port = 80, $user = "", $password = "", $socks = false) { + self::$default_proxy = array( + CURLOPT_PROXY => $proxy, + CURLOPT_PROXYUSERPWD => "{$user}:{$password}", + CURLOPT_PROXYPORT => $port, + CURLOPT_PROXYTYPE => ($socks ? CURLPROXY_SOCKS5 : CURLPROXY_HTTP) + ); + } /** * Creates a new restful service. @@ -23,6 +43,7 @@ class RestfulService extends ViewableData { function __construct($base, $expiry=3600){ $this->baseURL = $base; $this->cache_expire = $expiry; + $this->proxy = self::$default_proxy; parent::__construct(); } @@ -33,6 +54,24 @@ class RestfulService extends ViewableData { function setQueryString($params=NULL){ $this->queryString = http_build_query($params,'','&'); } + + /** + * Set proxy settings for this RestfulService instance + * + * @param string $proxy The URL of the proxy to use. + * @param int $port Proxy port + * @param string $user The proxy auth user name + * @param string $password The proxy auth password + * @param boolean $socks Set true to use socks5 proxy instead of http + */ + function setProxy($proxy, $port = 80, $user = "", $password = "", $socks = false) { + $this->proxy = array( + CURLOPT_PROXY => $proxy, + CURLOPT_PROXYUSERPWD => "{$user}:{$password}", + CURLOPT_PROXYPORT => $port, + CURLOPT_PROXYTYPE => ($socks ? CURLPROXY_SOCKS5 : CURLPROXY_HTTP) + ); + } /** * Set basic authentication @@ -111,7 +150,12 @@ class RestfulService extends ViewableData { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } - + + // Apply proxy settings + if(is_array($this->proxy)) { + curl_setopt_array($ch, $this->proxy); + } + // Run request curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $responseBody = curl_exec($ch);