php多线程抓取网页(PHP5.3以上版本,使用pthreads抓取信息测试例子)

优采云 发布时间: 2021-11-26 12:11

  php多线程抓取网页(PHP5.3以上版本,使用pthreads抓取信息测试例子)

  只有在php5.3及以后的版本中才能真正使用多线程程序。以前是fake curl实现的多线程工作。给大家介绍几个多线程爬取的资料。测试例子,希望对大家有帮助。

  PHP 5.3 及以上,使用pthreads PHP扩展,可以让PHP真正支持多线程。多线程是处理重复的循环任务,可以大大缩短程序的执行时间。

  PHP 扩展下载:

  PHP 手册文档:

  1、Linux的扩展编译安装,编辑参数--enable-maintainer-zts为必选项:

  cd /Data/tgz/php-5.3.8

./configure --prefix=/Data/apps/php --with-config-file-path=/Data/apps/php/etc --with-mysql=/Data/apps/mysql --with-mysqli=/Data/apps/mysql/bin/mysql_config --with-iconv-dir --with-freetype-dir=/Data/apps/libs --with-jpeg-dir=/Data/apps/libs --with-png-dir=/Data/apps/libs --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt=/Data/apps/libs --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-opcache --with-pdo-mysql --enable-maintainer-zts

make clean

make

make install

unzip pthreads-master.zip

cd pthreads-master

/Data/apps/php/bin/phpize

./configure --with-php-config=/Data/apps/php/bin/php-config

make

make install

  添加扩展:

  vi /Data/apps/php/etc/php.ini

  extension = "pthreads.so"

  一段PHP多线程的代码示例,以及For循环,抓取百度搜索页面,代码如下:

  以采集数据为例,代码如下:

  教程链接:

  欢迎转载~但请保留教程地址

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线