Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add curl options CURLOPT_TIMEOUT #353

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Qiniu/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,21 @@ final class Config
public $useCdnDomains;
// Zone Cache
private $regionCache;
// Timeout
public $options = array(
'timeout' => 60
);

// 构造函数
public function __construct(Region $z = null)
public function __construct(Region $z = null, $options = array())
{
$this->zone = $z;
$this->useHTTPS = false;
$this->useCdnDomains = false;
$this->regionCache = array();
if (!empty($options)) {
$this->options = array_merge($this->options, $options);
}
}

public function getUpHost($accessKey, $bucket)
Expand Down
22 changes: 12 additions & 10 deletions src/Qiniu/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@

final class Client
{
public static function get($url, array $headers = array())
public static function get($url, array $headers = array(), array $options = array())
{
$request = new Request('GET', $url, $headers);
$request = new Request('GET', $url, $headers, $options);
return self::sendRequest($request);
}

public static function delete($url, array $headers = array())
public static function delete($url, array $headers = array(), array $options = array())
{
$request = new Request('DELETE', $url, $headers);
$request = new Request('DELETE', $url, $headers, $options);
return self::sendRequest($request);
}

public static function post($url, $body, array $headers = array())
public static function post($url, $body, array $headers = array(), array $options = array())
{
$request = new Request('POST', $url, $headers, $body);
$request = new Request('POST', $url, $headers, $body, $options);
return self::sendRequest($request);
}

public static function PUT($url, $body, array $headers = array())
public static function PUT($url, $body, array $headers = array(), array $options = array())
{
$request = new Request('PUT', $url, $headers, $body);
$request = new Request('PUT', $url, $headers, $body, $options);
return self::sendRequest($request);
}

Expand All @@ -38,7 +38,8 @@ public static function multipartPost(
$fileName,
$fileBody,
$mimeType = null,
array $headers = array()
array $headers = array(),
array $options = array()
) {
$data = array();
$mimeBoundary = md5(microtime());
Expand All @@ -65,7 +66,7 @@ public static function multipartPost(
// var_dump($data);exit;
$contentType = 'multipart/form-data; boundary=' . $mimeBoundary;
$headers['Content-Type'] = $contentType;
$request = new Request('POST', $url, $headers, $body);
$request = new Request('POST', $url, $headers, $body, $options);
return self::sendRequest($request);
}

Expand Down Expand Up @@ -97,6 +98,7 @@ public static function sendRequest($request)
CURLOPT_NOBODY => false,
CURLOPT_CUSTOMREQUEST => $request->method,
CURLOPT_URL => $request->url,
CURLOPT_TIMEOUT => $request->options['timeout'],
);
// Handle open_basedir & safe mode
if (!ini_get('safe_mode') && !ini_get('open_basedir')) {
Expand Down
9 changes: 8 additions & 1 deletion src/Qiniu/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ final class Request
public $body;
public $method;

public function __construct($method, $url, array $headers = array(), $body = null)
public $options = array(
'timeout' => 60
);

public function __construct($method, $url, array $headers = array(), $body = null, $options = array())
{
$this->method = strtoupper($method);
$this->url = $url;
$this->headers = $headers;
$this->body = $body;
if (!empty($options)) {
$this->options = array_merge($this->options, $options);
}
}
}
4 changes: 2 additions & 2 deletions src/Qiniu/Storage/FormUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static function put(

$upHost = $config->getUpHost($accessKey, $bucket);

$response = Client::multipartPost($upHost, $fields, 'file', $fname, $data, $mime);
$response = Client::multipartPost($upHost, $fields, 'file', $fname, $data, $mime, $config->options);
if (!$response->ok()) {
return array(null, new Error($upHost, $response));
}
Expand Down Expand Up @@ -114,7 +114,7 @@ public static function putFile(

$upHost = $config->getUpHost($accessKey, $bucket);

$response = Client::post($upHost, $fields, $headers);
$response = Client::post($upHost, $fields, $headers, $config->options);
if (!$response->ok()) {
return array(null, new Error($upHost, $response));
}
Expand Down
8 changes: 8 additions & 0 deletions tests/Qiniu/Tests/FormUpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ protected function setUp()
$this->cfg = new Config();
}

public function testConfig()
{
$this->assertSame($this->cfg->options, array('timeout' => 60));

$cfg = new Config(null, array('timeout' => 600));
$this->assertSame($cfg->options, array('timeout' => 600));
}

public function testData()
{
$token = $this->auth->uploadToken($this->bucketName);
Expand Down