Пример #1
0
<?php

// autoload
function __autoload($clazz)
{
    $file = str_replace('_', '/', $clazz);
    require "/usr/share/pear/{$file}.php";
}
require __DIR__ . '/../src/AsyncHttpClient.php';
function callbackFunc($result)
{
    echo "Result len:";
    echo strlen($result['response']);
    //$response = Zend_Http_Response::fromString($result['response']);
    //echo $response->getBody();
    echo "\n";
}
$base = event_base_new();
$uri = "http://www.baidu.com/";
$config = array('callback' => 'callbackFunc', 'eventbase' => $base);
for ($i = 0; $i < 3; $i++) {
    $client = new AsyncHttpClient($uri, $config);
    $client->request();
}
event_base_loop($base);
echo "done\n";
 function onClose($cli)
 {
     echo "Server close\n";
     //如果容许跳转且小于最大跳转次数
     if (($this->status['uri'] == '302' || $this->status['uri'] == '301') && $this->auto_redirects && $this->redirect_count < $this->max_redirects) {
         $this->errorLog(__LINE__, "jump count is " . $this->redirect_count . "  *****************");
         $this->errorLog(__LINE__, "respHeader is " . print_r($this->respHeader, true) . "  *****************");
         $jumpUrl = $this->respHeader['Location'];
         $newclass = new AsyncHttpClient($jumpUrl, 'GET');
         $newclass->redirect_count = $this->redirect_count + 1;
         $newclass->auto_redirects = true;
         if ($this->useProxy == true) {
             $newclass->setUserProxy($this->proxyIp, $this->proxyPort);
         }
         $newclass->execute();
         $newclass->onReady('nest', $this);
     } else {
         //如果是正常的情况 无跳转 直接调用
         if (!is_callable($this->onReadyCallback)) {
             call_user_func(array($this->onReadyCallback, 'nest'), $this, $this->body, $this->respHeader);
         } else {
             call_user_func($this->onReadyCallback, $this, $this->body, $this->respHeader);
         }
     }
     unset($cli);
 }