http_clone.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. use Ares333\Curl\HttpClone;
  3. require_once '_inc.php';
  4. $dir = __DIR__ . '/output/clone';
  5. $cacheDir = __DIR__ . '/output/cache';
  6. global $argv;
  7. if (! isset($argv[1])) {
  8. $argv[1] = '1';
  9. }
  10. class HttpCloneDemo extends HttpClone
  11. {
  12. public $blockedFileRemoveCache = true;
  13. function onProcess($r, $args)
  14. {
  15. // Check invalid images.Url may be block by firewall.
  16. $checkExt = array(
  17. 'jpg',
  18. 'gif',
  19. 'png'
  20. );
  21. if ($this->blockedFileRemoveCache && in_array(pathinfo($r['info']['url'], PATHINFO_EXTENSION), $checkExt)) {
  22. if (false !== strpos($r['info']['content_type'], 'text')) {
  23. $r['info']['http_code'] = 403;
  24. if (isset($r['cacheFile']) && is_file($r['cacheFile'])) {
  25. unlink($r['cacheFile']);
  26. }
  27. }
  28. }
  29. return parent::onProcess($r, $args);
  30. }
  31. }
  32. $clone = new HttpCloneDemo($dir);
  33. $clone->setCurl();
  34. $clone->getCurl()->opt[CURLOPT_CONNECTTIMEOUT] = 3;
  35. $clone->getCurl()->opt[CURLOPT_ENCODING] = 'gzip,deflate';
  36. $clone->getCurl()->cache['enable'] = true;
  37. $clone->getCurl()->cache['expire'] = 86400 * 30;
  38. $clone->getCurl()->cache['enableDownload'] = true;
  39. $clone->getCurl()->cache['dir'] = $cacheDir;
  40. $clone->getCurl()->cache['compress'] = 6;
  41. $clone->expire = 0;
  42. switch ($argv[1]) {
  43. case '1':
  44. $clone->getCurl()->maxThread = 1;
  45. $clone->getCurl()->opt[CURLOPT_TIMEOUT] = 5;
  46. $clone->add('http://www.laruence.com/manual/');
  47. $clone->blacklist = array(
  48. 'http://img.tongji.linezing.com/728491/tongji.gif',
  49. 'http://js.tongji.linezing.com/728491/tongji.js',
  50. 'http://www.laruence.com/manual/css/images/dialog-information.png',
  51. 'http://www.laruence.com/manual/css/images/dialog-warning.png'
  52. );
  53. break;
  54. case '2':
  55. $clone->getCurl()->maxThread = 2;
  56. $clone->getCurl()->opt[CURLOPT_TIMEOUT] = 30;
  57. $clone->download['video'] = true;
  58. $clone->add('http://www.handubaby.com', 5);
  59. break;
  60. case '3':
  61. $clone->getCurl()->maxThread = 2;
  62. $clone->getCurl()->opt[CURLOPT_TIMEOUT] = 30;
  63. $clone->add('http://www.bjszxx.cn/', 3);
  64. break;
  65. }
  66. $clone->start();