public function getContent() { $curl = new Core(); // 链接数据库 require '/www/html/workerman/html/cl_spider/zreading/vendor/indieteq/indieteq-php-my-sql-pdo-database-class/Db.class.php'; $db = new Db(); $list = $db->query("SELECT id, detail_url,source_id from list limit 10"); if ($list) { foreach ($list as $k => $v) { $curl->add(array('url' => $v['detail_url']), 'Content::callback1'); } } $curl->start(); }
<?php require_once '../vendor/autoload.php'; use Ares333\CurlMulti\Core; $url1 = 'http://badurl1'; $url2 = 'http://badurl2'; $curl = new Core(); $curl->maxTry = 1; $curl->opt[CURLOPT_CONNECTTIMEOUT] = 1; $curl->opt[CURLOPT_TIMEOUT] = 1; // cbFail golbal $curl->cbFail = 'cbFailGlobal'; // cbFail for individual task $curl->add(array('url' => $url1), null, 'cbFailTask')->add(array('url' => $url2))->start(); function cbFailTask($err, $args) { echo 'Task Fail: ' . $err['info']['url'] . "\n"; print_r($err['error']); } function cbFailGlobal($err, $args) { echo 'Global Fail: ' . $err['info']['url'] . "\n"; print_r($err['error']); }
<?php require_once '../vendor/autoload.php'; use Ares333\CurlMulti\Core; $curl = new Core(); $curl->opt[CURLOPT_RETURNTRANSFER] = false; $url = 'http://www.baidu.com'; $curl->add(array('url' => $url, 'opt' => array(CURLOPT_RETURNTRANSFER => true)), function ($r, $args) { echo "content length: " . strlen($r['content']); }); $curl->start();
<?php require_once '../vendor/autoload.php'; use Ares333\CurlMulti\Core; use Ares333\CurlMulti\Base; $curl = new Core(); $curl->cbInfo = array(new Base(), 'cbCurlInfo'); $curl->maxThread = 10; $curl->maxThreadType['html'] = 2; $curl->maxThreadType['image'] = 5; $url1 = 'http://www.baidu.com'; $url2 = 'http://www.baidu.com/img/bd_logo1.png'; for ($i = 0; $i < 100; $i++) { $curl->add(array('url' => $url1 . '?wd=' . $i, 'ctl' => array('type' => 'html'))); $curl->add(array('url' => $url2 . '?i=' . $i, 'ctl' => array('type' => 'image'))); } $curl->start();
<?php require_once '../../vendor/autoload.php'; use Ares333\CurlMulti\Core; use Ares333\CurlMulti\Base; $curl = new Core(); $curl->cbInfo = array(new Base(), 'cbCurlInfo'); $curl->maxThread = 2; $curl->cache['enable'] = true; $curl->cache['dir'] = __DIR__ . '/cache'; if (!file_exists($curl->cache['dir'])) { mkdir($curl->cache['dir']); } $url = 'http://www.baidu.com'; for ($i = 0; $i < 100; $i++) { $curl->add(array('url' => $url)); } $curl->start();
<?php require_once '../../vendor/autoload.php'; use Ares333\CurlMulti\Core; $url = array('http://baidu.com', 'http://bing.com'); $curl = new Core(); foreach ($url as $v) { $curl->add(array('url' => $v, 'args' => array('test' => 'this is user arg for ' . $v)), 'cbProcess'); } // start spider $curl->start(); function cbProcess($r, $args) { echo "success, url=" . $r['info']['url'] . "\n"; print_r(array_keys($r)); print_r($args); }
<?php //cbUser is called whenever curl has network traffic require_once '../../vendor/autoload.php'; use Ares333\CurlMulti\Core; $curl = new Core(); $curl->maxThread = 1; $curl->cbUser = '******'; $url = 'http://www.baidu.com'; for ($i = 0; $i < 3; $i++) { $curl->add(array('url' => $url . '?wd=' . $i)); } $curl->start(); function cbUser() { static $i = 0; echo $i++ . "\n"; }
<?php require_once '../../vendor/autoload.php'; use Ares333\CurlMulti\Core; $curl = new Core(); $url = 'http://baidu.com'; $curl->add(array('url' => $url), 'cb1'); // start spider $curl->start(); function cb1($r, $args) { echo $r['info']['url'] . " finished\n"; global $curl; $curl->add(array('url' => 'http://bing.com'), 'cb2'); echo "http://bing.com added\n"; } function cb2($r, $args) { echo $r['info']['url'] . " finished\n"; }
<?php require_once '../../vendor/autoload.php'; use Ares333\CurlMulti\Core; $curl = new Core(); $url = 'http://www.baidu.com/img/bd_logo1.png'; $file = __DIR__ . '/baidu.png'; $fp = fopen($file, 'w'); $curl->add(array('url' => $url, 'opt' => array(CURLOPT_FILE => $fp, CURLOPT_HEADER => false), 'args' => array('file' => $file)), 'cbProcess'); // start spider $curl->start(); function cbProcess($r, $args) { echo "download finished successfully, file={$args['file']}\n"; }
<?php require_once '../../vendor/autoload.php'; use Ares333\CurlMulti\Core; $url = array(); $curl = new Core(); $curl->start(function () { static $i = 0; echo $i++ . "\n"; if ($i >= 7) { return false; } sleep(1); return true; });
<?php require_once '../vendor/autoload.php'; use Ares333\CurlMulti\Core; $curl = new Core(); $curl->maxThread = 3; $curl->cbTask = array('cbTask', 'this is param for cbTask'); $curl->start(); function cbTask($param) { static $i = 0, $j = 0; global $curl; $count = 10; if ($i < $count) { $curl->add(array('url' => 'http://www.baidu.com?wd=' . $i)); $i++; if ($i == $count) { $curl->cbTask = null; } } echo $i . ' tasks added, cbTask called ' . ++$j . " times\n"; }
<?php // execute in order of task added require_once '../vendor/autoload.php'; use Ares333\CurlMulti\Core; $curl = new Core(); $curl->maxThread = 1; $curl->taskPoolType = 'queue'; $url = 'http://www.baidu.com'; for ($i = 0; $i < 10; $i++) { $curl->add(array('url' => $url . '?wd=' . $i, 'args' => array('i' => $i)), 'cbProcess'); echo "{$i} added\n"; } $curl->start(); function cbProcess($r, $args) { echo $args['i'] . " finished\n"; }