mirror of
https://github.com/processwire/processwire.git
synced 2025-08-11 01:04:16 +02:00
Add @MoritzLost PR #179 which has various improvements for the WireHttp::sendCURL() method
This commit is contained in:
@@ -276,7 +276,7 @@ class WireHttp extends Wire {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
$this->hasCURL = function_exists('curl_init') && !ini_get('safe_mode') && !ini_get('open_basedir');
|
$this->hasCURL = function_exists('curl_init') && !ini_get('safe_mode'); // && !ini_get('open_basedir');
|
||||||
$this->hasFopen = ini_get('allow_url_fopen');
|
$this->hasFopen = ini_get('allow_url_fopen');
|
||||||
$this->resetRequest();
|
$this->resetRequest();
|
||||||
$this->resetResponse();
|
$this->resetResponse();
|
||||||
@@ -586,6 +586,7 @@ class WireHttp extends Wire {
|
|||||||
$this->resetResponse();
|
$this->resetResponse();
|
||||||
$this->lastSendType = 'curl';
|
$this->lastSendType = 'curl';
|
||||||
$timeout = isset($options['timeout']) ? (float) $options['timeout'] : $this->getTimeout();
|
$timeout = isset($options['timeout']) ? (float) $options['timeout'] : $this->getTimeout();
|
||||||
|
$postMethods = array('POST', 'PUT', 'DELETE', 'PATCH'); // methods for CURLOPT_POSTFIELDS
|
||||||
$proxy = '';
|
$proxy = '';
|
||||||
|
|
||||||
if(!empty($options['proxy'])) {
|
if(!empty($options['proxy'])) {
|
||||||
@@ -600,9 +601,10 @@ class WireHttp extends Wire {
|
|||||||
|
|
||||||
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
|
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
|
||||||
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
|
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
|
||||||
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
|
|
||||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||||||
curl_setopt($curl, CURLOPT_USERAGENT, $this->getUserAgent());
|
curl_setopt($curl, CURLOPT_USERAGENT, $this->getUserAgent());
|
||||||
|
|
||||||
|
if(!ini_get('open_basedir')) curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
|
||||||
|
|
||||||
if(version_compare(PHP_VERSION, '5.6') >= 0) {
|
if(version_compare(PHP_VERSION, '5.6') >= 0) {
|
||||||
// CURLOPT_SAFE_UPLOAD value is default true (setopt not necessary)
|
// CURLOPT_SAFE_UPLOAD value is default true (setopt not necessary)
|
||||||
@@ -621,27 +623,28 @@ class WireHttp extends Wire {
|
|||||||
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
|
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
if($method == 'POST') {
|
if($method === 'POST') {
|
||||||
curl_setopt($curl, CURLOPT_POST, true);
|
curl_setopt($curl, CURLOPT_POST, true);
|
||||||
} else if($method == 'PUT') {
|
} else if($method === 'GET') {
|
||||||
curl_setopt($curl, CURLOPT_PUT, true);
|
curl_setopt($curl, CURLOPT_HTTPGET, true);
|
||||||
} else if($method == 'HEAD') {
|
} else if($method === 'HEAD') {
|
||||||
curl_setopt($curl, CURLOPT_NOBODY, true);
|
curl_setopt($curl, CURLOPT_NOBODY, true);
|
||||||
} else {
|
} else {
|
||||||
curl_setopt($curl, CURLOPT_HTTPGET, true);
|
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
|
||||||
}
|
}
|
||||||
|
// note: CURLOPT_PUT removed because it also requires CURLOPT_INFILE and CURLOPT_INFILESIZE.
|
||||||
|
|
||||||
if($proxy) curl_setopt($curl, CURLOPT_PROXY, $proxy);
|
if($proxy) curl_setopt($curl, CURLOPT_PROXY, $proxy);
|
||||||
|
|
||||||
if(!empty($this->data)) {
|
if(!empty($this->data)) {
|
||||||
if($method === 'POST') {
|
if(in_array($method, $postMethods)) {
|
||||||
curl_setopt($curl, CURLOPT_POSTFIELDS, $this->data);
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $this->data);
|
||||||
} else {
|
} else {
|
||||||
$content = http_build_query($this->data);
|
$content = http_build_query($this->data);
|
||||||
if(strlen($content)) $url .= (strpos($url, '?') === false ? '?' : '&') . $content;
|
if(strlen($content)) $url .= (strpos($url, '?') === false ? '?' : '&') . $content;
|
||||||
}
|
}
|
||||||
} else if(!empty($this->rawData)) {
|
} else if(!empty($this->rawData)) {
|
||||||
if($method === 'POST') {
|
if(in_array($method, $postMethods)) {
|
||||||
curl_setopt($curl, CURLOPT_POSTFIELDS, $this->rawData);
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $this->rawData);
|
||||||
} else {
|
} else {
|
||||||
throw new WireException("Raw data option with CURL not supported for $method");
|
throw new WireException("Raw data option with CURL not supported for $method");
|
||||||
|
Reference in New Issue
Block a user