Skip to content

Commit

Permalink
When in synchronous mode use curl instead of multi curl.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmathai committed Sep 19, 2010
1 parent aaff245 commit c365e61
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
40 changes: 31 additions & 9 deletions EpiCurl.php
Expand Up @@ -32,6 +32,17 @@ 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);
Expand Down Expand Up @@ -117,23 +128,34 @@ private function headerCallback($ch, $header)
$val = preg_replace('/^\W+/','',substr($_header, $colonPos));
$this->responses[$this->getKey($ch)]['headers'][$key] = $val;
}
$this->storeResponse($ch);
return strlen($header);
}

private function storeResponses()
{
while($done = curl_multi_info_read($this->mc))
{
$key = (string)$done['handle'];
$this->stopTimer($key, $done);
$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']);
$this->storeResponse($done);
}
}

private function storeResponse($ch, $isAsynchronous = true)
{
$key = $this->getKey($ch['handle']);
$this->stopTimer($key, $ch);
if($isAsynchronous)
$this->responses[$key]['data'] = curl_multi_getcontent($ch['handle']);
else
$this->responses[$key]['data'] = curl_exec($ch['handle']);

foreach($this->properties as $name => $const)
{
$this->responses[$key][$name] = curl_getinfo($ch['handle'], $const);
}
if($isAsynchronous)
curl_multi_remove_handle($this->mc, $ch['handle']);
curl_close($ch['handle']);
}

private function startTimer($key)
Expand Down
14 changes: 11 additions & 3 deletions EpiOAuth.php
Expand Up @@ -193,12 +193,20 @@ protected function generateSignature($method = null, $url = null, $params = null
return $this->signString($signatureBaseString);
}

protected function executeCurl($ch)
{
if($this->isAsynchronous)
return $this->curl->addCurl($ch);
else
return $this->curl->addEasyCurl($ch);
}

protected function httpDelete($url, $params) {
$this->addDefaultHeaders($url, $params['oauth']);
$ch = $this->curlInit($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->buildHttpQueryRaw($params['request']));
$resp = $this->curl->addCurl($ch);
$resp = $this->executeCurl($ch);
$this->emptyHeaders();
return $resp;
}
Expand All @@ -216,7 +224,7 @@ protected function httpGet($url, $params = null)
}
$this->addDefaultHeaders($url, $params['oauth']);
$ch = $this->curlInit($url);
$resp = $this->curl->addCurl($ch);
$resp = $this->executeCurl($ch);
$this->emptyHeaders();

return $resp;
Expand All @@ -233,7 +241,7 @@ protected function httpPost($url, $params = null, $isMultipart)
curl_setopt($ch, CURLOPT_POSTFIELDS, $params['request']);
else
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->buildHttpQueryRaw($params['request']));
$resp = $this->curl->addCurl($ch);
$resp = $this->executeCurl($ch);
$this->emptyHeaders();

return $resp;
Expand Down
1 change: 0 additions & 1 deletion EpiTwitter.php
Expand Up @@ -247,7 +247,6 @@ class EpiTwitterException extends Exception
public static function raise($response, $debug)
{
$message = $response->data;

switch($response->code)
{
case 400:
Expand Down
5 changes: 4 additions & 1 deletion tests/EpiTwitterTest.php
Expand Up @@ -20,8 +20,11 @@ function setUp()
// key and secret for a test app (don't really care if this is public)
$this->twitterObj = new EpiTwitter($this->consumer_key, $this->consumer_secret, $this->token, $this->secret);
$this->twitterObjUnAuth = new EpiTwitter($this->consumer_key, $this->consumer_secret);
$this->twitterObjBasic = new EpiTwitter();
$this->twitterObjBadAuth = new EpiTwitter('foo', 'bar', 'foo', 'bar');
// these 3 lines turn on asynchronous calls
$this->twitterObj->useAsynchronous(true);
$this->twitterObjUnAuth->useAsynchronous(true);
$this->twitterObjBadAuth->useAsynchronous(true);
}

function testGetAuthenticateurl()
Expand Down

0 comments on commit c365e61

Please sign in to comment.