Permalink
Browse files

Update EpiCurl.php

  • Loading branch information...
dlnetworks committed Feb 28, 2014
1 parent 65ffcdd commit d29e428cb4080ca8f7ce9050fd1d3874cc0cda32
Showing with 62 additions and 24 deletions.
  1. +62 −24 EpiCurl.php
View
@@ -1,14 +1,4 @@
<?php
/*
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
class EpiCurl
{
const timeout = 3;
@@ -23,6 +13,8 @@ class EpiCurl
private $requests = array();
private $responses = array();
private $properties = array();
private static $timers = array();
function __construct()
{
if(self::$singleton == 0)
@@ -40,13 +32,25 @@ function __construct()
);
}
public function addEasyCurl($ch)
{
$key = $this->getKey($ch);
$this->requests[$key] = $ch;
curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'headerCallback'));
$done = array('handle' => $ch);
$this->storeResponse($done, false);
$this->startTimer($key);
return new EpiCurlManager($key);
}
public function addCurl($ch)
{
$key = $this->getKey($ch);
$this->requests[$key] = $ch;
curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'headerCallback'));
$code = curl_multi_add_handle($this->mc, $ch);
$this->startTimer($key);
// (1)
if($code === CURLM_OK || $code === CURLM_CALL_MULTI_PERFORM)
@@ -75,15 +79,15 @@ public function getResult($key = null)
$innerSleepInt = $outerSleepInt = 1;
while($this->running && ($this->execStatus == CURLM_OK || $this->execStatus == CURLM_CALL_MULTI_PERFORM))
{
usleep($outerSleepInt);
$outerSleepInt = max(1, ($outerSleepInt*$this->sleepIncrement));
usleep(intval($outerSleepInt));
$outerSleepInt = intval(max(1, ($outerSleepInt*$this->sleepIncrement)));
$ms=curl_multi_select($this->mc, 0);
if($ms > 0)
{
do{
$this->execStatus = curl_multi_exec($this->mc, $this->running);
usleep($innerSleepInt);
$innerSleepInt = max(1, ($innerSleepInt*$this->sleepIncrement));
usleep(intval($innerSleepInt));
$innerSleepInt = intval(max(1, ($innerSleepInt*$this->sleepIncrement)));
}while($this->execStatus==CURLM_CALL_MULTI_PERFORM);
$innerSleepInt = 1;
}
@@ -99,6 +103,16 @@ public function getResult($key = null)
return false;
}
public static function getSequence()
{
return new EpiSequence(self::$timers);
}
public static function getTimers()
{
return self::$timers;
}
private function getKey($ch)
{
return (string)$ch;
@@ -121,15 +135,39 @@ private function storeResponses()
{
while($done = curl_multi_info_read($this->mc))
{
$key = (string)$done['handle'];
$this->storeResponse($done);
}
}
private function storeResponse($done, $isAsynchronous = true)
{
$key = $this->getKey($done['handle']);
$this->stopTimer($key, $done);
if($isAsynchronous)
$this->responses[$key]['data'] = curl_multi_getcontent($done['handle']);
foreach($this->properties as $name => $const)
{
$this->responses[$key][$name] = curl_getinfo($done['handle'], $const);
}
curl_multi_remove_handle($this->mc, $done['handle']);
curl_close($done['handle']);
else
$this->responses[$key]['data'] = curl_exec($done['handle']);
foreach($this->properties as $name => $const)
{
$this->responses[$key][$name] = curl_getinfo($done['handle'], $const);
}
if($isAsynchronous)
curl_multi_remove_handle($this->mc, $done['handle']);
curl_close($done['handle']);
}
private function startTimer($key)
{
self::$timers[$key]['start'] = microtime(true);
}
private function stopTimer($key, $done)
{
self::$timers[$key]['end'] = microtime(true);
self::$timers[$key]['api'] = curl_getinfo($done['handle'], CURLINFO_EFFECTIVE_URL);
self::$timers[$key]['time'] = curl_getinfo($done['handle'], CURLINFO_TOTAL_TIME);
self::$timers[$key]['code'] = curl_getinfo($done['handle'], CURLINFO_HTTP_CODE);
}
static function getInstance()
@@ -149,19 +187,19 @@ class EpiCurlManager
private $key;
private $epiCurl;
function __construct($key)
public function __construct($key)
{
$this->key = $key;
$this->epiCurl = EpiCurl::getInstance();
}
function __get($name)
public function __get($name)
{
$responses = $this->epiCurl->getResult($this->key);
return isset($responses[$name]) ? $responses[$name] : null;
}
function __isset($name)
public function __isset($name)
{
$val = self::__get($name);
return empty($val);

0 comments on commit d29e428

Please sign in to comment.