예제 #1
0
use PhpMultiCurl\Task\Http as HttpTask;
use PhpMultiCurl\Thread\CurlThreadError;
$urls = ['http://google.com', 'http://ya.ru', 'http://php.net', 'http://habr.ru', 'http://error.loc'];
$value1 = 'foo';
$value2 = 'bar';
$onLoad = function (array $responce, BaseTask $task) {
    var_dump(date('H:i:s'));
    $data = $task->getData();
    if ($responce['http_code'] == 200) {
        var_dump('downloaded ' . $data['url']);
        var_dump('content: ' . str_replace(["\r\n", "\n"], '', substr($responce['response_content'], 0, 512)));
    } else {
        var_dump('error_page');
    }
    flush();
};
$onError = function (CurlThreadError $error) {
    var_dump(date('H:i:s'));
    var_dump($error->getCode());
    var_dump($error->getMessage());
    flush();
};
$queue = new TasksQueue();
foreach ($urls as $url) {
    $task = new HttpTask($url);
    $task->setOnLoad($onLoad)->setOnError($onError)->setData(['arg1' => $value1, 'arg2' => $value2, 'url' => $url]);
    $task->setCurlOptions([CURLOPT_HTTPHEADER => ["Accept-Language: en-us,en;q=0.5", "Accept-Charset: utf-8;q=0.7,*;q=0.7", "Cache-Control: max-age=0", "Pragma: ", "Keep-Alive: 300", "Connection: keep-alive"], CURLOPT_REFERER => "http://www.google.com", CURLOPT_USERAGENT => "Googlebot/2.1 (+http://www.google.com/bot.html)", CURLOPT_FOLLOWLOCATION => TRUE, CURLOPT_AUTOREFERER => TRUE, CURLOPT_MAXREDIRS => 10, CURLOPT_CONNECTTIMEOUT => 30, CURLOPT_TIMEOUT => 30, CURLOPT_DNS_CACHE_TIMEOUT => 1, CURLOPT_SSL_VERIFYHOST => FALSE, CURLOPT_SSL_VERIFYPEER => FALSE]);
    $queue->enqueue($task);
}
$phpMultiCurl = new PhpMultiCurl();
$phpMultiCurl->executeTasks($queue);
예제 #2
0
<?php

require '../vendor/autoload.php';
use PhpMultiCurl\Helper\Queue as TasksQueue;
use PhpMultiCurl\PhpMultiCurl;
use PhpMultiCurl\Task\Http as HttpTask;
$queue = new TasksQueue();
$callback = function (array $responce, HttpTask $task) {
    var_dump('parent ' . $responce['http_code'] . ' ' . $task->getUrl());
    global $queue;
    $task = new HttpTask('http://github.com');
    $task->setOnLoad(function ($responce, HttpTask $task) {
        var_dump('child ' . $responce['http_code'] . ' ' . $task->getUrl());
    });
    $queue->enqueue($task);
};
$task = new HttpTask('http://php.net');
$task->setOnLoad($callback);
$queue->enqueue($task);
$phpMultiCurl = new PhpMultiCurl();
$phpMultiCurl->executeTasks($queue);
예제 #3
0
<?php

require '../vendor/autoload.php';
use PhpMultiCurl\Helper\Queue as TasksQueue;
use PhpMultiCurl\PhpMultiCurl;
use PhpMultiCurl\Task\Http as HttpTask;
use PhpMultiCurl\Thread\CurlThreadError;
if (isset($_GET['sleep'])) {
    sleep($_GET['sleep']);
    echo $_GET['sleep'];
    exit;
}
$onLoad = function (array $responce) {
    var_dump($responce['response_content']);
};
$onError = function (CurlThreadError $error) {
    var_dump($error->getCode());
    echo $error;
};
$queue = new TasksQueue();
for ($i = 0; $i < 5; $i++) {
    $task = new HttpTask('http://localhost/phpmulticurl/examples/example0.php?sleep=' . 2 * $i);
    $task->setOnLoad($onLoad)->setOnError($onError);
    $queue->enqueue($task);
}
$task = new HttpTask('http://hostname_does_not_exist/');
$queue->enqueue($task->setOnError($onError));
$phpMultiCurl = new PhpMultiCurl();
$phpMultiCurl->setNumberOfThreads(2);
$phpMultiCurl->executeTasks($queue);