mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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
This commit is contained in:
parent
3fe5af85a4
commit
028d59ffc3
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user