From b1d33e185f74338426645ce2163de1c6d60277ff Mon Sep 17 00:00:00 2001 From: yaozongyou Date: Tue, 21 Feb 2017 17:44:07 +0800 Subject: [PATCH] bugfix: Remove php strict warning by using integer as array key instead of curl handle. --- qcloudcos/libcurl_wrapper.php | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/qcloudcos/libcurl_wrapper.php b/qcloudcos/libcurl_wrapper.php index d39b654..6c5975e 100644 --- a/qcloudcos/libcurl_wrapper.php +++ b/qcloudcos/libcurl_wrapper.php @@ -21,11 +21,13 @@ class HttpResponse { // A simple wrapper for libcurl using multi interface to do transfers in parallel. class LibcurlWrapper { + private $sequence; // integer: sequence id for each request. private $curlMultiHandle; // curl handle: curl multi handle. private $curlHandleInfo; // array: array of active curl handle. private $idleCurlHandle; // array: idle curl handle which can be reused. public function __construct() { + $this->sequence = 0; $this->curlMultiHandle = curl_multi_init(); $this->idleCurlHandle = array(); } @@ -39,6 +41,8 @@ public function __destruct() { } public function startSendingRequest($httpRequest, $done) { + $this->sequence += 1; + if (count($this->idleCurlHandle) !== 0) { $curlHandle = array_pop($this->idleCurlHandle); } else { @@ -69,8 +73,10 @@ public function startSendingRequest($httpRequest, $done) { curl_multi_add_handle($this->curlMultiHandle, $curlHandle); - $this->curlHandleInfo[$curlHandle]['done'] = $done; - $this->curlHandleInfo[$curlHandle]['request'] = $httpRequest; + + $this->curlHandleInfo[$this->sequence]['handle'] = $curlHandle; + $this->curlHandleInfo[$this->sequence]['done'] = $done; + $this->curlHandleInfo[$this->sequence]['request'] = $httpRequest; } public function performSendingRequest() { @@ -108,8 +114,17 @@ public function performSendingRequest() { private function processResult($info) { $result = $info['result']; $handle = $info['handle']; - $request = $this->curlHandleInfo[$handle]['request']; - $done = $this->curlHandleInfo[$handle]['done']; + $sequence = 0; + + foreach ($this->curlHandleInfo as $key => $info) { + if ($info['handle'] === $handle) { + $sequence = $key; + break; + } + } + + $request = $this->curlHandleInfo[$sequence]['request']; + $done = $this->curlHandleInfo[$sequence]['done']; $response = new HttpResponse(); if ($result !== CURLE_OK) { @@ -138,7 +153,7 @@ private function processResult($info) { call_user_func($done, $request, $response); } - unset($this->curlHandleInfo[$handle]); + unset($this->curlHandleInfo[$sequence]); curl_multi_remove_handle($this->curlMultiHandle, $handle); array_push($this->idleCurlHandle, $handle);