Chinaunix首页 | 论坛 | 博客
  • 博客访问: 107922
  • 博文数量: 38
  • 博客积分: 559
  • 博客等级: 中士
  • 技术积分: 250
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-08 16:50
文章分类

全部博文(38)

文章存档

2011年(38)

分类:

2011-04-21 11:03:35

原文地址:PHP多线程编程 作者:togofree

from: http://www.cnblogs.com/niniwzw/archive/2010/01/18/1651082.html

PHP多线程编程(一)

    虽然PHP 中,多线程用的比较的少。但是毕竟可能是会用到了。我最近就遇到这样一个问题,用户提交几百个url以后,要读出这个url 中的标题。

当然,你不希望用户等待的太久,10s 钟应该给出个答案。但是,本身,你要获取一个url 的标题,少的要 0.1s ,多的要好几秒。

显然,采用单个线程的方式是不行的。

 

    我的第一个设计方案是这样的:

   1. 用我前面提供的代码提供一个简单的服务器:  http://www.cnblogs.com/niniwzw/archive/2009/09/27/1575002.html

   这个服务器的作用是:提供一个url,然后,就读取标题。这里,可以每次读128个字节,看看有没有读到title,如果读到title了就停止读了。

   这样可以省带宽。

 

   2. 在客户端,同时打开1百个 socket ,访问这个服务器。如果提供的url数目超过100,那么就多次运行。

   这个方案,基本上能够满足要求,读比较快的网页如:google.com 100次,也只要1s 左右。但是,通过测试,发现,有一定

   的概率在打开链接的时候被阻塞。(有时候会阻塞个1s左右,然后继续往下open)可能打开了太多的链接了,会出很大的问题。

 

   当然,这是一个很差的解决方案:建立tcp 链接本身的消耗非常的大。因为可靠有序传输的要求,要维持一个数据结构,而且,系统还要开辟一定的缓存给客户端和服务器端,

   用户缓存数据。如果建立上百个链接,就可能占用很大的内存。作为一个系统的服务,应该尽量的简单,就是,我叫你做什么事情,你做好以后,结果给我就可以了。

  

    一般来说,PHP要进行多线程编程,比较常见的是:

    1. 要进行大量的网络耗时的操作

    2. 要做大量的运算,并且,系统有多个cpu,为了让用户有更快的体验,把一个任务,分成几个小任务,最后合并。

   

    所以,应该尽量不要在调用的地方有太多复杂的逻辑,把逻辑内置在服务中。

 

   我的第二个设计方案是这样的:

   同样用上面的服务器,只是,这个服务器功能变了,接收不超过100个的url,然后打开100个子线程,下载title。最后合并,返回给客户端。

具体怎么编写这个服务器,在下一个部分讲。

   这个一测试,发现效率高了很多。而且也十分的稳定。下载一百下google 大概 0.7s。基本上不会超过1s,而原来的那个方案,经常超过5s(20%的可能性)

 

   当然,如果这样的设计方案只是一个很简单的解决方案。如果有很多人使用你的服务的情况下,肯定不能这样做。

   PHP做企业级别的开发,一个比较复杂的问题,就是多线程怎么处理。还有就是往往采用数组 会引起内存急剧膨胀。一般,数组处理10万条数据已经是极限,

在小网站开发很少会用到一次读取如此大的数据量,要是遇到了,最好通过C 扩展进行解决,否则,一次会损耗 几百M 的内存,10个人用就拖死你。

 

PHP多线程编程(二)管道通信

一个线程如果是个人英雄主义,那么多线程就是集体主义。(不严格区分多进程 和 多线程的差别)

你不再是一个独行侠,而是一个指挥家。

独来独往,非常自由自在,但是,很多时候,不如众人拾柴火焰高。

这就是我对多线程的理解。多线程编程的主要问题是:通信 和 同步问题。

更多PHP 多线程编程的背景知识见:

PHP多线程编程(一)

在PHP 中,如果光用pcntl ,实现比较简单的通信问题都是很困难的。

 

下面介绍管道通信:

1. 管道可以认为是一个队列,不同的线程都可以往里面写东西,也都可以从里面读东西。写就是

在队列末尾添加,读就是在队头删除。

 

2. 管道一般有大小,默认一般是4K,也就是内容超过4K了,你就只能读,不能往里面写了。

 

3. 默认情况下,管道写入以后,就会被阻止,直到读取他的程序读取把数据读完。而读取线程也会被阻止,

   直到有进程向管道写入数据。当然,你可以改变这样的默认属性,用stream_set_block  函数,设置成非阻断模式。

 

下面是我分装的一个管道的类(这个类命名有问题,没有统一,没有时间改成统一的了,我一般先写测试代码,最后分装,所以命名上可能不统一):

  1. <?php
  2. class Pipe
  3. {
  4.     public $fifoPath;
  5.     private $w_pipe;
  6.     private $r_pipe;

  7.     /**
  8.      * 自动创建一个管道
  9.      *
  10.      * @param string $name 管道名字
  11.      * @param int $mode 管道的权限,默认任何用户组可以读写
  12.      */
  13.     function __construct($name = 'pipe', $mode = 0666)
  14.     {
  15.         $fifoPath = "/tmp/$name." . posix_getpid();
  16.         if (!file_exists($fifoPath)) {
  17.             if (!posix_mkfifo($fifoPath, $mode)) {
  18.                 error("create new pipe ($name) error.");
  19.                 return false;
  20.             }
  21.         } else {
  22.             error( "pipe ($name) has exit.");
  23.             return false;
  24.         }
  25.         $this->fifoPath = $fifoPath;
  26.     }
  27.    
  28. ///////////////////////////////////////////////////

  29. // 写管道函数开始

  30. ///////////////////////////////////////////////////

  31.     function open_write()
  32.     {
  33.         $this->w_pipe = fopen($this->fifoPath, 'w');
  34.         if ($this->w_pipe == NULL) {
  35.             error("open pipe {$this->fifoPath} for write error.");
  36.             return false;
  37.         }
  38.         return true;
  39.     }

  40.     function write($data)
  41.     {
  42.         return fwrite($this->w_pipe, $data);
  43.     }

  44.     function write_all($data)
  45.     {
  46.         $w_pipe = fopen($this->fifoPath, 'w');
  47.         fwrite($w_pipe, $data);
  48.         fclose($w_pipe);
  49.     }

  50.     function close_write()
  51.     {
  52.         return fclose($this->w_pipe);
  53.     }
  54. /////////////////////////////////////////////////////////

  55. /// 读管道相关函数开始

  56. ////////////////////////////////////////////////////////

  57.     function open_read()
  58.     {
  59.         $this->r_pipe = fopen($this->fifoPath, 'r');
  60.         if ($this->r_pipe == NULL) {
  61.             error("open pipe {$this->fifoPath} for read error.");
  62.             return false;
  63.         }
  64.         return true;
  65.     }

  66.     function read($byte = 1024)
  67.     {
  68.         return fread($this->r_pipe, $byte);
  69.     }

  70.     function read_all()
  71.     {
  72.         $r_pipe = fopen($this->fifoPath, 'r');
  73.         $data = '';
  74.         while (!feof($r_pipe)) {
  75.             //echo "read one K\n";

  76.             $data .= fread($r_pipe, 1024);
  77.         }
  78.         fclose($r_pipe);
  79.         return $data;
  80.     }

  81.     function close_read()
  82.     {
  83.         return fclose($this->r_pipe);
  84.     }
  85. ////////////////////////////////////////////////////

  86.     /**
  87.      * 删除管道
  88.      *
  89.      * @return boolean is success
  90.      */
  91.     function rm_pipe()
  92.     {
  93.         return unlink($this->fifoPath);
  94.     }
  95. }
  96. ?>

有了这个类,就可以实现简单的管道通信了,因为这个教程是多线程编程系列教程的一个部分。

这个管道类的应用部分,将放到第三部分。

 

PHP多线程编程(三)多线程抓取网页的演示

要理解这个部分的代码,请阅读:

用 Socket 和 Pcntl 实现一个多线程服务器(一)

PHP多线程编程(一)

PHP多线程编程(二)管道通信

 

我们知道,从父进程到子经常的数据传递相对比较容易一些,但是从子进程传递到父进程就比较的困难。

有很多办法实现进程交互,在php中比较方便的是 管道通信。当然,还可以通过 socket_pair 进行通信。

 

首先是服务器为了应对每一个请求要做的事情(发送一个url 序列,url序列用\t 分割。而结束标记是 \n)

  1. function clientHandle($msgsock, $obj)
  2. {
  3.     $nbuf = '';
  4.     socket_set_block($msgsock);
  5.     do {
  6.         if (false === ($buf = @socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
  7.             $obj->error("socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)));
  8.             break;
  9.         }
  10.         $nbuf .= $buf;

  11.         if (substr($nbuf, -1) != "\n") {
  12.             continue;
  13.         }
  14.         $nbuf = trim($nbuf);
  15.         if ($nbuf == 'quit') {
  16.             break;
  17.         }
  18.         if ($nbuf == 'shutdown') {
  19.             break;
  20.         }
  21.         $url = explode("\t", $nbuf);
  22.         $nbuf = '';

  23.         $talkback = serialize(read_ntitle($url));
  24.         socket_write($msgsock, $talkback, strlen($talkback));
  25.         debug("write to the client\n");
  26.         break;
  27.     } while (true);
  28. }

上面代码比较关键的一个部分是 read_ntitle,这个函数实现多线程的读取标题。

 

代码如下:(为每一个url fork 一个线程,然后打开管道 ,读取到的标题写入到管道里面去,主线程一直的在读取管道数据,直到所有的数据读取完毕,最后删除管道)

  1. function read_ntitle($arr)
  2. {
  3.     $pipe = new Pipe("multi-read");
  4.     foreach ($arr as $k => $item)
  5.     {
  6.         $pids[$k] = pcntl_fork();
  7.         if(!$pids[$k])
  8.         {
  9.              $pipe->open_write();
  10.              $pid = posix_getpid();
  11.              $content = base64_encode(read_title($item));
  12.              $pipe->write("$k,$content\n");
  13.              $pipe->close_write();
  14.              debug("$k: write success!\n");
  15.              exit;
  16.         }
  17.     }
  18.     debug("read begin!\n");
  19.     $data = $pipe->read_all();
  20.     debug("read end!\n");

  21.     $pipe->rm_pipe();
  22.     return parse_data($data);
  23. }
  24. parse_data 代码如下,非常的简单,就不说了。
  25. function parse_data($data)
  26. {
  27.     $data = explode("\n", $data);
  28.     $new = array();
  29.     foreach ($data as $value)
  30.     {
  31.         $value = explode(",", $value);
  32.         if (count($value) == 2) {
  33.             $value[1] = base64_decode($value[1]);
  34.             $new[intval($value[0])] = $value[1];
  35.         }
  36.     }
  37.     ksort($new, SORT_NUMERIC);
  38.     return $new;
  39. }

上面代码中,还有一个函数read_title 比较有技巧。为了兼容性,我没有采用curl,而是直接采用socket 通信。

在下载到 title 标签后,就停止读取内容,以节省时间。代码如下:

  1. function read_title($url)
  2. {
  3.     $url_info = parse_url($url);
  4.     if (!isset($url_info['host']) || !isset($url_info['scheme'])) {
  5.      return false;
  6.     }
  7.     $host = $url_info['host'];
  8.     
  9.  $port = isset($url_info['port']) ? $url_info['port'] : null;
  10.  $path = isset($url_info['path']) ? $url_info['path'] : "/";
  11.  if(isset($url_info['query'])) $path .= "?".$url_info['query'];
  12.  if(empty($port)){
  13.   $port = 80;
  14.  }
  15.  if ($url_info['scheme'] == 'https'){
  16.   $port = 443;
  17.  }
  18.  if ($url_info['scheme'] == 'http') {
  19.   $port = 80;
  20.  }
  21.     $out = "GET $path HTTP/1.1\r\n";
  22.     $out .= "Host: $host\r\n";
  23.     $out .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.7)\r\n";
  24.     $out .= "Connection: Close\r\n\r\n";
  25.     $fp = fsockopen($host, $port, $errno, $errstr, 5);
  26.     if ($fp == NULL) {
  27.      error("get title from $url, error. $errno: $errstr \n");
  28.      return false;
  29.     }
  30.     fwrite($fp, $out);
  31.     $content = '';
  32.     while (!feof($fp)) {
  33.         $content .= fgets($fp, 1024);
  34.         if (preg_match("/(.*?)<\/title>/is"</SPAN><SPAN style="COLOR: #0000cc">,</SPAN> $<SPAN style="COLOR: #ff0000">content</SPAN><SPAN style="COLOR: #0000cc">,</SPAN> $matches<SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">)</SPAN> <SPAN style="COLOR: #0000cc">{</SPAN><BR></LI> <LI>             fclose<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000cc">$</SPAN>fp<SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>            return encode_to_utf8<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000cc">$</SPAN>matches<SPAN style="COLOR: #0000cc">[</SPAN>1<SPAN style="COLOR: #0000cc">]</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000cc">}</SPAN><BR></LI> <LI>    <SPAN style="COLOR: #0000cc">}</SPAN><BR></LI> <LI>    fclose<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000cc">$</SPAN>fp<SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>    return false<SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI><SPAN style="COLOR: #0000cc">}</SPAN><BR></LI> <LI><BR></LI> <LI>function encode_to_utf8<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000cc">$</SPAN>string<SPAN style="COLOR: #0000cc">)</SPAN> <BR></LI> <LI><SPAN style="COLOR: #0000cc">{</SPAN><BR></LI> <LI>     return mb_convert_encoding<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000cc">$</SPAN>string<SPAN style="COLOR: #0000cc">,</SPAN> <SPAN style="COLOR: #ff00ff">"UTF-8"</SPAN><SPAN style="COLOR: #0000cc">,</SPAN> mb_detect_encoding<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000cc">$</SPAN>string<SPAN style="COLOR: #0000cc">,</SPAN> <SPAN style="COLOR: #ff00ff">"UTF-8, GB2312, ISO-8859-1"</SPAN><SPAN style="COLOR: #0000cc">,</SPAN> true<SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI><SPAN style="COLOR: #0000cc">}</SPAN></SPAN></LI></OL></DIV> <P>这里,我只是检测了 三种最常见的编码。</P> <P>其他的代码都很简单,这些代码都是测试用的,如果你要做这样一个服务器,一定要进行优化处理。特别是,要防止一次打开太多的线程,你要做更多的处理。</P> <P>很多时候,我们抱怨php 不支持多线程,实际上,php是支持多线程的。当然,没有那么多的进程通信的选项,而多线程的核心就在于线程的通信与同步。</P> <P>在web开发中,这样的多线程基本上是不会使用的,因为有很严重的性能问题。要实现比较简单的多线程,高负载,必须借助其扩展。</P> <P> </P> <P><STRONG>PHP多进程(四) 内部多进程</STRONG></P> <P>上面一个系列的教程:</P> <P><A id=homepage1_HomePageDays_ctl00_DayList_ctl38_TitleUrl class=postTitle2 href="http://www.cnblogs.com/niniwzw/archive/2009/09/27/1575002.html" target=_blank><FONT color=#399ab2>用 Socket 和 Pcntl 实现一个多进程服务器(一)</FONT></A> </P> <P><A id=homepage1_HomePageDays_DaysList_ctl02_DayItem_DayList_ctl00_TitleUrl class=postTitle2 href="http://www.cnblogs.com/niniwzw/archive/2010/01/18/1651082.html" target=_blank><FONT color=#399ab2>PHP多进程编程(一)</FONT></A> </P> <P><A id=homepage1_HomePageDays_DaysList_ctl01_DayItem_DayList_ctl00_TitleUrl class=postTitle2 href="http://www.cnblogs.com/niniwzw/archive/2010/01/20/1652801.html" target=_blank><FONT color=#399ab2>PHP多进程编程(二)管道通信</FONT></A> </P> <P><A id=ctl04_TitleUrl class=postTitle2 href="http://www.cnblogs.com/niniwzw/archive/2010/01/29/1659474.html" target=_blank><FONT color=#000000>PHP多进程编程(三)多进程抓取网页的演示</FONT></A> </P> <P> </P> <P>说的都是只兼容unix 服务器的多进程,下面来讲讲在window 和 unix 都兼容的多进程(这里是泛指,下面的curl实际上是通过IO复用实现的)。</P> <P>    通过扩展实现多线程的典型例子是CURL,CURL 支持多线程的抓取网页的功能。</P> <P>这部分过于抽象,所以,我先给出一个CURL并行抓取多个网页内容的一个分装类。这个类实际上很实用,</P> <P>详细分析这些函数的内部实现将在下一个教程里面描述。</P> <P>    你可能不能很好的理解这个类,而且,php curl 官方主页上都有很多错误的例子,在讲述了其内部机制</P> <P>后,你就能够明白了。</P> <P>    先看代码:</P> <DIV id=codeText class=codeText> <OL style="PADDING-BOTTOM: 5px; MARGIN: 0px 1px 0px 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; PADDING-TOP: 5px" class=dp-css> <LI><SPAN style="COLOR: #000000"><SPAN style="COLOR: #0000cc"><</SPAN><SPAN style="COLOR: #0000cc">?</SPAN><SPAN style="COLOR: #0000ff">php</SPAN><BR></LI> <LI><SPAN style="COLOR: #0000ff">class</SPAN> Http_MultiRequest<BR></LI> <LI><SPAN style="COLOR: #0000cc">{</SPAN><BR></LI> <LI>    <SPAN style="COLOR: #ff9900">//要并行抓取的url 列表<BR></LI> <LI></SPAN><BR></LI> <LI>    <SPAN style="COLOR: #0000ff">private</SPAN> <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">urls</SPAN> <SPAN style="COLOR: #0000cc">=</SPAN> <SPAN style="COLOR: #ff0000">array</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI><BR></LI> <LI>    <SPAN style="COLOR: #ff9900">//curl 的选项<BR></LI> <LI></SPAN><BR></LI> <LI>    <SPAN style="COLOR: #0000ff">private</SPAN> <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">options</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>    <BR></LI> <LI>    <SPAN style="COLOR: #ff9900">//构造函数<BR></LI> <LI></SPAN><BR></LI> <LI>    <SPAN style="COLOR: #0000ff">function</SPAN> __construct<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">options</SPAN> <SPAN style="COLOR: #0000cc">=</SPAN> <SPAN style="COLOR: #ff0000">array</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><BR></LI> <LI>    <SPAN style="COLOR: #0000cc">{</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">this</SPAN><SPAN style="COLOR: #0000cc">-</SPAN><SPAN style="COLOR: #0000cc">></SPAN>setOptions<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">options</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>    <SPAN style="COLOR: #0000cc">}</SPAN><BR></LI> <LI><BR></LI> <LI>    <SPAN style="COLOR: #ff9900">//设置url 列表<BR></LI> <LI></SPAN><BR></LI> <LI>    <SPAN style="COLOR: #0000ff">function</SPAN> setUrls<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">urls</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><BR></LI> <LI>    <SPAN style="COLOR: #0000cc">{</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">this</SPAN><SPAN style="COLOR: #0000cc">-</SPAN><SPAN style="COLOR: #0000cc">></SPAN>urls <SPAN style="COLOR: #0000cc">=</SPAN> <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">urls</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000ff">return</SPAN> <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">this</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>    <SPAN style="COLOR: #0000cc">}</SPAN><BR></LI> <LI><BR></LI> <LI><BR></LI> <LI>    <SPAN style="COLOR: #ff9900">//设置选项<BR></LI> <LI></SPAN><BR></LI> <LI>    <SPAN style="COLOR: #0000ff">function</SPAN> setOptions<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">options</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><BR></LI> <LI>    <SPAN style="COLOR: #0000cc">{</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">options</SPAN><SPAN style="COLOR: #0000cc">[</SPAN>CURLOPT_RETURNTRANSFER<SPAN style="COLOR: #0000cc">]</SPAN> <SPAN style="COLOR: #0000cc">=</SPAN> 1<SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000ff">if</SPAN> <SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #ff0000">isset</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">options</SPAN><SPAN style="COLOR: #0000cc">[</SPAN><SPAN style="COLOR: #ff00ff">'HTTP_POST'</SPAN><SPAN style="COLOR: #0000cc">]</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">)</SPAN> <BR></LI> <LI>        <SPAN style="COLOR: #0000cc">{</SPAN><BR></LI> <LI>            <SPAN style="COLOR: #ff0000">curl_setopt</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">ch</SPAN><SPAN style="COLOR: #0000cc">,</SPAN> CURLOPT_POST<SPAN style="COLOR: #0000cc">,</SPAN> 1<SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>            <SPAN style="COLOR: #ff0000">curl_setopt</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">ch</SPAN><SPAN style="COLOR: #0000cc">,</SPAN> CURLOPT_POSTFIELDS<SPAN style="COLOR: #0000cc">,</SPAN> <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">options</SPAN><SPAN style="COLOR: #0000cc">[</SPAN><SPAN style="COLOR: #ff00ff">'HTTP_POST'</SPAN><SPAN style="COLOR: #0000cc">]</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>            <SPAN style="COLOR: #ff0000">unset</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">options</SPAN><SPAN style="COLOR: #0000cc">[</SPAN><SPAN style="COLOR: #ff00ff">'HTTP_POST'</SPAN><SPAN style="COLOR: #0000cc">]</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000cc">}</SPAN><BR></LI> <LI><BR></LI> <LI>        <SPAN style="COLOR: #0000ff">if</SPAN> <SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000cc">!</SPAN><SPAN style="COLOR: #ff0000">isset</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">options</SPAN><SPAN style="COLOR: #0000cc">[</SPAN>CURLOPT_USERAGENT<SPAN style="COLOR: #0000cc">]</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">)</SPAN> <BR></LI> <LI>        <SPAN style="COLOR: #0000cc">{</SPAN><BR></LI> <LI>            <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">options</SPAN><SPAN style="COLOR: #0000cc">[</SPAN>CURLOPT_USERAGENT<SPAN style="COLOR: #0000cc">]</SPAN> <SPAN style="COLOR: #0000cc">=</SPAN> <SPAN style="COLOR: #ff00ff">'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;)'</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000cc">}</SPAN><BR></LI> <LI><BR></LI> <LI>        <SPAN style="COLOR: #0000ff">if</SPAN> <SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000cc">!</SPAN><SPAN style="COLOR: #ff0000">isset</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">options</SPAN><SPAN style="COLOR: #0000cc">[</SPAN>CURLOPT_FOLLOWLOCATION<SPAN style="COLOR: #0000cc">]</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">)</SPAN> <BR></LI> <LI>        <SPAN style="COLOR: #0000cc">{</SPAN><BR></LI> <LI>            <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">options</SPAN><SPAN style="COLOR: #0000cc">[</SPAN>CURLOPT_FOLLOWLOCATION<SPAN style="COLOR: #0000cc">]</SPAN> <SPAN style="COLOR: #0000cc">=</SPAN> 1<SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000cc">}</SPAN><BR></LI> <LI><BR></LI> <LI>        <SPAN style="COLOR: #0000ff">if</SPAN> <SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000cc">!</SPAN><SPAN style="COLOR: #ff0000">isset</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">options</SPAN><SPAN style="COLOR: #0000cc">[</SPAN>CURLOPT_HEADER<SPAN style="COLOR: #0000cc">]</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000cc">{</SPAN><BR></LI> <LI>            <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">options</SPAN><SPAN style="COLOR: #0000cc">[</SPAN>CURLOPT_HEADER<SPAN style="COLOR: #0000cc">]</SPAN> <SPAN style="COLOR: #0000cc">=</SPAN> 0<SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000cc">}</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">this</SPAN><SPAN style="COLOR: #0000cc">-</SPAN><SPAN style="COLOR: #0000cc">></SPAN>options <SPAN style="COLOR: #0000cc">=</SPAN> <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">options</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>    <SPAN style="COLOR: #0000cc">}</SPAN><BR></LI> <LI><BR></LI> <LI>    <SPAN style="COLOR: #ff9900">//并行抓取所有的内容<BR></LI> <LI></SPAN><BR></LI> <LI>    <SPAN style="COLOR: #0000ff">function</SPAN> <SPAN style="COLOR: #ff0000">exec</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><BR></LI> <LI>    <SPAN style="COLOR: #0000cc">{</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #ff0000">empty</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">this</SPAN><SPAN style="COLOR: #0000cc">-</SPAN><SPAN style="COLOR: #0000cc">></SPAN>urls<SPAN style="COLOR: #0000cc">)</SPAN> <SPAN style="COLOR: #0000cc">|</SPAN><SPAN style="COLOR: #0000cc">|</SPAN> <SPAN style="COLOR: #0000cc">!</SPAN><SPAN style="COLOR: #ff0000">is_array</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">this</SPAN><SPAN style="COLOR: #0000cc">-</SPAN><SPAN style="COLOR: #0000cc">></SPAN>urls<SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000cc">{</SPAN><BR></LI> <LI>            <SPAN style="COLOR: #0000ff">return</SPAN> <SPAN style="COLOR: #0000ff">false</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000cc">}</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">curl</SPAN> <SPAN style="COLOR: #0000cc">=</SPAN> <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">data</SPAN> <SPAN style="COLOR: #0000cc">=</SPAN> <SPAN style="COLOR: #ff0000">array</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">mh</SPAN> <SPAN style="COLOR: #0000cc">=</SPAN> <SPAN style="COLOR: #ff0000">curl_multi_init</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000ff">foreach</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">this</SPAN><SPAN style="COLOR: #0000cc">-</SPAN><SPAN style="COLOR: #0000cc">></SPAN>urls <SPAN style="COLOR: #0000ff">as</SPAN> <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">k</SPAN> <SPAN style="COLOR: #0000cc">=</SPAN><SPAN style="COLOR: #0000cc">></SPAN> <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">v</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000cc">{</SPAN><BR></LI> <LI>            <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">curl</SPAN><SPAN style="COLOR: #0000cc">[</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">k</SPAN><SPAN style="COLOR: #0000cc">]</SPAN> <SPAN style="COLOR: #0000cc">=</SPAN> <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">this</SPAN><SPAN style="COLOR: #0000cc">-</SPAN><SPAN style="COLOR: #0000cc">></SPAN>addHandle<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">mh</SPAN><SPAN style="COLOR: #0000cc">,</SPAN> <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">v</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000cc">}</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">this</SPAN><SPAN style="COLOR: #0000cc">-</SPAN><SPAN style="COLOR: #0000cc">></SPAN>execMulitHandle<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">mh</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000ff">foreach</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">this</SPAN><SPAN style="COLOR: #0000cc">-</SPAN><SPAN style="COLOR: #0000cc">></SPAN>urls <SPAN style="COLOR: #0000ff">as</SPAN> <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">k</SPAN> <SPAN style="COLOR: #0000cc">=</SPAN><SPAN style="COLOR: #0000cc">></SPAN> <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">v</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000cc">{</SPAN><BR></LI> <LI>            <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">data</SPAN><SPAN style="COLOR: #0000cc">[</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">k</SPAN><SPAN style="COLOR: #0000cc">]</SPAN> <SPAN style="COLOR: #0000cc">=</SPAN> <SPAN style="COLOR: #ff0000">curl_multi_getcontent</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">curl</SPAN><SPAN style="COLOR: #0000cc">[</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">k</SPAN><SPAN style="COLOR: #0000cc">]</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>            <SPAN style="COLOR: #ff0000">curl_multi_remove_handle</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">mh</SPAN><SPAN style="COLOR: #0000cc">,</SPAN> <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">curl</SPAN><SPAN style="COLOR: #0000cc">[</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">k</SPAN><SPAN style="COLOR: #0000cc">]</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000cc">}</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #ff0000">curl_multi_close</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">mh</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000ff">return</SPAN> <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">data</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>    <SPAN style="COLOR: #0000cc">}</SPAN><BR></LI> <LI>    <BR></LI> <LI>    <SPAN style="COLOR: #ff9900">//只抓取一个网页的内容。<BR></LI> <LI></SPAN><BR></LI> <LI>    <SPAN style="COLOR: #0000ff">function</SPAN> execOne<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">url</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><BR></LI> <LI>    <SPAN style="COLOR: #0000cc">{</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000ff">if</SPAN> <SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #ff0000">empty</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">url</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">)</SPAN> <SPAN style="COLOR: #0000cc">{</SPAN><BR></LI> <LI>            <SPAN style="COLOR: #0000ff">return</SPAN> <SPAN style="COLOR: #0000ff">false</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000cc">}</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">ch</SPAN> <SPAN style="COLOR: #0000cc">=</SPAN> <SPAN style="COLOR: #ff0000">curl_init</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">url</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">this</SPAN><SPAN style="COLOR: #0000cc">-</SPAN><SPAN style="COLOR: #0000cc">></SPAN>setOneOption<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">ch</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">content</SPAN> <SPAN style="COLOR: #0000cc">=</SPAN> <SPAN style="COLOR: #ff0000">curl_exec</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">ch</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #ff0000">curl_close</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">ch</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000ff">return</SPAN> <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">content</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>    <SPAN style="COLOR: #0000cc">}</SPAN><BR></LI> <LI>    <BR></LI> <LI>    <SPAN style="COLOR: #ff9900">//内部函数,设置某个handle 的选项<BR></LI> <LI></SPAN><BR></LI> <LI>    <SPAN style="COLOR: #0000ff">private</SPAN> <SPAN style="COLOR: #0000ff">function</SPAN> setOneOption<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">ch</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><BR></LI> <LI>    <SPAN style="COLOR: #0000cc">{</SPAN><BR></LI> <LI>        curl_setopt_array<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">ch</SPAN><SPAN style="COLOR: #0000cc">,</SPAN> <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">this</SPAN><SPAN style="COLOR: #0000cc">-</SPAN><SPAN style="COLOR: #0000cc">></SPAN>options<SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>    <SPAN style="COLOR: #0000cc">}</SPAN><BR></LI> <LI><BR></LI> <LI>    <SPAN style="COLOR: #ff9900">//添加一个新的并行抓取 handle<BR></LI> <LI></SPAN><BR></LI> <LI>    <SPAN style="COLOR: #0000ff">private</SPAN> <SPAN style="COLOR: #0000ff">function</SPAN> addHandle<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">mh</SPAN><SPAN style="COLOR: #0000cc">,</SPAN> <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">url</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><BR></LI> <LI>    <SPAN style="COLOR: #0000cc">{</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">ch</SPAN> <SPAN style="COLOR: #0000cc">=</SPAN> <SPAN style="COLOR: #ff0000">curl_init</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">url</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">this</SPAN><SPAN style="COLOR: #0000cc">-</SPAN><SPAN style="COLOR: #0000cc">></SPAN>setOneOption<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">ch</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #ff0000">curl_multi_add_handle</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">mh</SPAN><SPAN style="COLOR: #0000cc">,</SPAN> <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">ch</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000ff">return</SPAN> <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">ch</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>    <SPAN style="COLOR: #0000cc">}</SPAN><BR></LI> <LI><BR></LI> <LI>    <SPAN style="COLOR: #ff9900">//并行执行(这样的写法是一个常见的错误,我这里还是采用这样的写法,这个写法<BR></LI> <LI></SPAN><BR></LI> <LI>    <SPAN style="COLOR: #ff9900">//下载一个小文件都可能导致cup占用100%, 并且,这个循环会运行10万次以上<BR></LI> <LI></SPAN><BR></LI> <LI>    <SPAN style="COLOR: #ff9900">//这是一个典型的不懂原理产生的错误。这个错误在PHP官方的文档上都相当的常见。)<BR></LI> <LI></SPAN><BR></LI> <LI>    <SPAN style="COLOR: #0000ff">private</SPAN> <SPAN style="COLOR: #0000ff">function</SPAN> execMulitHandle<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">mh</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><BR></LI> <LI>    <SPAN style="COLOR: #0000cc">{</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">running</SPAN> <SPAN style="COLOR: #0000cc">=</SPAN> <SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000ff">do</SPAN> <SPAN style="COLOR: #0000cc">{</SPAN><BR></LI> <LI>            <SPAN style="COLOR: #ff0000">curl_multi_exec</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">mh</SPAN><SPAN style="COLOR: #0000cc">,</SPAN> <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">running</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>        <SPAN style="COLOR: #0000cc">}</SPAN> <SPAN style="COLOR: #0000ff">while</SPAN> <SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">running</SPAN> <SPAN style="COLOR: #0000cc">></SPAN> 0<SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>    <SPAN style="COLOR: #0000cc">}</SPAN><BR></LI> <LI><SPAN style="COLOR: #0000cc">}</SPAN></SPAN></LI></OL></DIV> <P>看最后一个注释最多的函数,这个错误在平时调试的时候可能不太容易发现,因为程序完全正常,但是,在生产服务器下,马上会引起崩溃效果。</P> <P>解释为什么不能这样,必须从C 语言内部实现的角度来分析。这个部分将放到下一个教程(<A id=homepage1_HomePageDays_DaysList_DayItem_6_DayList_6_TitleUrl_0 class=postTitle2 href="http://www.cnblogs.com/niniwzw/archive/2010/12/15/1906671.html"><FONT color=#399ab2>PHP高级编程之--单线程实现并行抓取网页</FONT></A> )。不过不是通过C语言来表述原理,而是通过PHP</P> <P>    这个类,实际上也就很简单的实现了前面我们费了4个教程的篇幅,并且是九牛二虎之力才实现的多线程的抓取网页的功能。在纯PHP的实现下,我们只能用一个后台服务的方式来比较好的实现,但是当你使用 操作系统接口语言 C 语言时候,这个实现当然就更加的简单,灵活,高效。</P> <P>    就同时抓取几个网页这样一件简单的事情,实际上在底层涉及到了很多东西,对很多半路出家的PHP程序员,可能不喜欢谈多线程这个东西,深入了就涉及到操作系统,浅点说就是并行运行好几个“程序”。但是,很多时候,多线程必不可少,比如要写个快点的爬虫,往往就会浪费九牛二虎之力。不过,PHP的程序员现在应该感谢CURL 这个扩展,这样,你完全不需要用你不太精通的 python 去写爬虫了,对于一个中型大小的爬虫,有这个内部多线程,就已经足够了。</P> <P> </P> <P>最后是上面的类的一个测试的例子:</P> <DIV id=codeText class=codeText> <OL style="PADDING-BOTTOM: 5px; MARGIN: 0px 1px 0px 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; PADDING-TOP: 5px" class=dp-css> <LI><SPAN style="COLOR: #000000">$urls <SPAN style="COLOR: #0000cc">=</SPAN> array<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #ff00ff">""</SPAN><SPAN style="COLOR: #0000cc">,</SPAN> <SPAN style="COLOR: #ff00ff">""</SPAN><SPAN style="COLOR: #0000cc">,</SPAN> <SPAN style="COLOR: #ff00ff">""</SPAN><SPAN style="COLOR: #0000cc">,</SPAN> <SPAN style="COLOR: #ff00ff">""</SPAN><SPAN style="COLOR: #0000cc">,</SPAN> <SPAN style="COLOR: #ff00ff">""</SPAN><SPAN style="COLOR: #0000cc">,</SPAN> <SPAN style="COLOR: #ff00ff">""</SPAN><SPAN style="COLOR: #0000cc">,</SPAN> <SPAN style="COLOR: #ff00ff">""</SPAN><SPAN style="COLOR: #0000cc">,</SPAN> <SPAN style="COLOR: #ff00ff">""</SPAN><SPAN style="COLOR: #0000cc">,</SPAN> <SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>$m <SPAN style="COLOR: #0000cc">=</SPAN> new Http_MultiRequest<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI><BR></LI> <LI>$t <SPAN style="COLOR: #0000cc">=</SPAN> microtime<SPAN style="COLOR: #0000cc">(</SPAN>true<SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>$m<SPAN style="COLOR: #0000cc">-</SPAN><SPAN style="COLOR: #0000cc">></SPAN>setUrls<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000cc">$</SPAN>urls<SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI><BR></LI> <LI><SPAN style="COLOR: #0000cc">/</SPAN><SPAN style="COLOR: #0000cc">/</SPAN>parallel fetch(并行抓取)<SPAN style="COLOR: #0000cc">:</SPAN><BR></LI> <LI>$<SPAN style="COLOR: #ff0000">data</SPAN> <SPAN style="COLOR: #0000cc">=</SPAN> $m<SPAN style="COLOR: #0000cc">-</SPAN><SPAN style="COLOR: #0000cc">></SPAN>exec<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>$parallel_time <SPAN style="COLOR: #0000cc">=</SPAN> microtime<SPAN style="COLOR: #0000cc">(</SPAN>true<SPAN style="COLOR: #0000cc">)</SPAN> <SPAN style="COLOR: #0000cc">-</SPAN> $t<SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>echo $parallel_time <SPAN style="COLOR: #0000cc">.</SPAN> <SPAN style="COLOR: #ff00ff">"\n"</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI><BR></LI> <LI>$t <SPAN style="COLOR: #0000cc">=</SPAN> microtime<SPAN style="COLOR: #0000cc">(</SPAN>true<SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI><BR></LI> <LI><SPAN style="COLOR: #0000cc">/</SPAN><SPAN style="COLOR: #0000cc">/</SPAN>serial fetch(串行抓取)<SPAN style="COLOR: #0000cc">:</SPAN><BR></LI> <LI>foreach <SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000cc">$</SPAN>urls as $url<SPAN style="COLOR: #0000cc">)</SPAN><BR></LI> <LI><SPAN style="COLOR: #0000cc">{</SPAN><BR></LI> <LI>    $<SPAN style="COLOR: #ff0000">data</SPAN><SPAN style="COLOR: #0000cc">[</SPAN><SPAN style="COLOR: #0000cc">]</SPAN> <SPAN style="COLOR: #0000cc">=</SPAN> $m<SPAN style="COLOR: #0000cc">-</SPAN><SPAN style="COLOR: #0000cc">></SPAN>execOne<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000cc">$</SPAN>url<SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI><SPAN style="COLOR: #0000cc">}</SPAN><BR></LI> <LI>$serial_time <SPAN style="COLOR: #0000cc">=</SPAN> microtime<SPAN style="COLOR: #0000cc">(</SPAN>true<SPAN style="COLOR: #0000cc">)</SPAN> <SPAN style="COLOR: #0000cc">-</SPAN> $t<SPAN style="COLOR: #0000cc">;</SPAN><BR></LI> <LI>echo $serial_time <SPAN style="COLOR: #0000cc">.</SPAN> <SPAN style="COLOR: #ff00ff">"\n"</SPAN><SPAN style="COLOR: #0000cc">;</SPAN></SPAN></LI></OL></DIV> </div> <!-- <div class="Blog_con3_1">管理员在2009年8月13日编辑了该文章文章。</div> --> <div class="Blog_con2_1 Blog_con3_2"> <div> <!--<img src="/image/default/tu_8.png">--> <!-- JiaThis Button BEGIN --> <div class="bdsharebuttonbox"><A class=bds_more href="#" data-cmd="more"></A><A class=bds_qzone title=分享到QQ空间 href="#" data-cmd="qzone"></A><A class=bds_tsina title=分享到新浪微博 href="#" data-cmd="tsina"></A><A class=bds_tqq title=分享到腾讯微博 href="#" data-cmd="tqq"></A><A class=bds_renren title=分享到人人网 href="#" data-cmd="renren"></A><A class=bds_weixin title=分享到微信 href="#" data-cmd="weixin"></A></div> <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script> <!-- JiaThis Button END --> </div> 阅读(1429) | 评论(0) | 转发(0) | <div class="HT_line3"></div> </div> <div class="Blog_con3_3"> <div><span id='digg_num'>0</span><a href="javascript:void(0)" id='digg' bid='256177' url='/blog/digg.html' ></a></div> <p>上一篇:<a href="/uid-25659871-id-256165.html">MYSQL插入数据时忽略重复数据</a></p> <p>下一篇:<a href="/uid-25659871-id-256180.html">腾讯php程序员面试题目</a></p> </div> </div> <!-- <div class="Blog_con3_4 Blog_con3_5"> <div class="Blog_tit2 Blog_tit7">热门推荐</div> <ul> <li><a href="" title="" target='blank' ></a></li> </ul> </div> --> </div> </div> <div class="Blog_right1_7" id='replyList'> <div class="Blog_tit3">给主人留下些什么吧!~~</div> <!--暂无内容--> <!-- 评论分页--> <div class="Blog_right1_6 Blog_right1_12"> </div> <!-- 评论分页--> <div class="Blog_right1_10" style="display:none"> <div class="Blog_tit3">评论热议</div> <!--未登录 --> <div class="Blog_right1_8"> <div class="nologin_con1"> 请登录后评论。 <p><a href="http://account.chinaunix.net/login" onclick="link(this)">登录</a> <a href="http://account.chinaunix.net/register?url=http%3a%2f%2fblog.chinaunix.net">注册</a></p> </div> </div> </div> <div style="text-align:center;margin-top:10px;"> <script type="text/javascript" smua="d=p&s=b&u=u3118759&w=960&h=90" src="//www.nkscdn.com/smu0/o.js"></script> </div> </div> </div> </div> <input type='hidden' id='report_url' value='/blog/ViewReport.html' /> <script type="text/javascript"> //测试字符串的长度 一个汉字算2个字节 function mb_strlen(str) { var len=str.length; var totalCount=0; for(var i=0;i<len;i++) { var c = str.charCodeAt(i); if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) { totalCount++; }else{ totalCount+=2; } } return totalCount; } /* var Util = {}; Util.calWbText = function (text, max){ if(max === undefined) max = 500; var cLen=0; var matcher = text.match(/[^\x00-\xff]/g), wlen = (matcher && matcher.length) || 0; //匹配url链接正则 http://*** var pattern = /http:\/\/([\w-]+\.)+[\w-]+(\/*[\w-\.\/\?%&=][^\s^\u4e00-\u9fa5]*)?/gi; //匹配的数据存入数组 var arrPt = new Array(); var i = 0; while((result = pattern.exec(text)) != null){ arrPt[i] = result[0]; i++; } //替换掉原文本中的链接 for(var j = 0;j<arrPt.length;j++){ text = text.replace(arrPt[j],""); } //减掉链接所占的长度 return Math.floor((max*2 - text.length - wlen)/2 - 12*i); }; */ var allowComment = '0'; //举报弹出层 function showJuBao(url, cid){ $.cover(false); asyncbox.open({ id : 'report_thickbox', url : url, title : '举报违规', width : 378, height : 240, scroll : 'no', data : { 'cid' : cid, 'idtype' : 2 , 'blogurl' : window.location.href }, callback : function(action){ if(action == 'close'){ $.cover(false); } } }); } $(function(){ //创建管理员删除的弹出层 $('#admin_article_del').click(function(){ $.cover(false); asyncbox.open({ id : 'class_thickbox', html : '<div class="HT_layer3_1"><ul><li class="HT_li1">操作原因:<select class="HT_sel7" id="del_type" name="del_type"><option value="广告文章">广告文章</option><option value="违规内容">违规内容</option><option value="标题不明">标题不明</option><option value="文不对题">文不对题</option></select></li><li class="HT_li1" ><input class="HT_btn4" id="admin_delete" type="button" /></li></ul></div>', title : '选择类型', width : 300, height : 150, scroll : 'no', callback : function(action){ if(action == 'close'){ $.cover(false); } } }); }); $('#admin_delete').live('click' , function(){ ///blog/logicdel/id/3480184/url/%252Fblog%252Findex.html.html var type = $('#del_type').val(); var url = '/blog/logicdel/id/256177/url/%252Fuid%252F25659871.html.html'; window.location.href= url + '?type=' + type; }); //顶 js中暂未添加&过滤 $('#digg').live('click' , function(){ if(isOnLine == '' ) { //showErrorMsg('登录之后才能进行此操作' , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); return false; } var bid = $('#digg').attr('bid'); var url = $('#digg').attr('url'); var digg_str = $.cookie('digg_id'); if(digg_str != null) { var arr= new Array(); //定义一数组 arr = digg_str.split(","); //字符分割 for( i=0 ; i < arr.length ; i++ ) { if(bid == arr[i]) { showErrorMsg('已经赞过该文章', '消息提示'); return false; } } } $.ajax({ type:"POST", url:url, data: { 'bid' : bid }, dataType: 'json', success:function(msg){ if(msg.error == 0) { var num = parseInt($('#digg_num').html(),10); num += 1; $('#digg_num').html(num); $('#digg').die(); if(digg_str == null) { $.cookie('digg_id', bid, {expires: 30 , path: '/'}); } else { $.cookie('digg_id', digg_str + ',' + bid, {expires: 30 , path: '/'}); } showSucceedMsg('谢谢' , '消息提示'); } else if(msg.error == 1) { //showErrorMsg(msg.error_content , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); } else if(msg.error == 2) { showErrorMsg(msg.error_content , '消息提示'); } } }); }); //举报弹出层 /*$('.box_report').live('click' , function(){ if(isOnLine == '' ) { //showErrorMsg('登录之后才能进行此操作' , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); return false; } var url = $('#report_url').val(); var cid = $(this).attr('cid'); $.cover(false); asyncbox.open({ id : 'report_thickbox', url : url, title : '举报违规', width : 378, height : 240, scroll : 'no', data : { 'cid' : cid, 'idtype' : 2 }, callback : function(action){ if(action == 'close'){ $.cover(false); } } }); });*/ //评论相关代码 //点击回复显示评论框 $('.Blog_a10').live('click' , function(){ if(isOnLine == '' ) { //showErrorMsg('登录之后才能进行此操作' , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); return false; } if(allowComment == 1) { showErrorMsg('该博文不允许评论' , '消息提示'); return false; } var tid = $(this).attr('toid');//留言作者id var bid = $(this).attr('bid');//blogid var cid = $(this).attr('cid');//留言id var tname = $(this).attr('tname'); var tpl = '<div class="Blog_right1_9">'; tpl += '<div class="div2">'; tpl += '<textarea name="" cols="" rows="" class="Blog_ar1_1" id="rmsg">文明上网,理性发言...</textarea>'; tpl += '</div>'; tpl += '<div class="div3">'; tpl += '<div class="div3_2"><a href="javascript:void(0);" class="Blog_a11" id="quota_sbumit" url="/Comment/PostComment.html" tid="'+tid+'" bid="'+bid+'" cid="'+cid+'" tname="'+tname+'" ></a><a href="javascript:void(0)" id="qx_comment" class="Blog_a12"></a></div>'; tpl += '<div class="div3_1"><a href="javascript:void(0);" id="mface"><span></span>表情</a></div>'; tpl += '<div class="clear"></div>'; tpl += '</div>'; tpl += '</div>'; $('.z_move_comment').html(''); $(this).parents('.Blog_right1_8').find('.z_move_comment').html(tpl).show(); }); //引用的评论提交 $('#quota_sbumit').live('click' , function(){ if(isOnLine == '' ) { //showErrorMsg('登录之后才能进行此操作' , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); return false; } var bid = $(this).attr('bid'); var tid = $(this).attr('tid');//被引用人的id var qid = $(this).attr('cid');//引用的id var url = $(this).attr('url'); var text = $('#rmsg').val(); var tname = $(this).attr('tname'); if(text == '' || text=='文明上网,理性发言...') { showErrorMsg('评论内容不能为空!' , '消息提示'); return false; } else { if(mb_strlen(text) > 1000){ showErrorMsg('评论内容不能超过500个汉字' , '消息提示'); return false; } } $.ajax({ type: "post", url: url , data: {'bid': bid , 'to' : tid , 'qid' : qid , 'text': text , 'tname' : tname }, dataType: 'json', success: function(data){ if(data.code == 1){ var tpl = '<div class="Blog_right1_8">'; tpl+= '<div class="Blog_right_img1"><a href="' +data.info.url+ '" >' + data.info.header + '</a></div>'; tpl+= '<div class="Blog_right_font1">'; tpl+= '<p class="Blog_p5"><span><a href="' +data.info.url+ '" >' + data.info.username + '</a></span>' + data.info.dateline + '</p>'; tpl+= '<p class="Blog_p7"><a href="' + data.info.quota.url + '">' + data.info.quota.username + '</a>:'+ data.info.quota.content + '</p>'; tpl+= '<p class="Blog_p8">' + data.info.content + '</p><span class="span_text1"><a href="javascript:void(0);" class="Blog_a10" toid=' + data.info.fuid + ' bid=' + data.info.bid + ' cid=' + data.info.cid + ' tname = ' + data.info.username + ' >回复</a> |  <a class="comment_del_mark" style="cursor:pointer" url="' + data.info.delurl + '" >删除</a> |  <a href="javascript:showJuBao(\'/blog/ViewReport.html\','+data.info.cid+')" class="box_report" cid="' + data.info.cid + '" >举报</a></span></div>'; tpl+= '<div class="z_move_comment" style="display:none"></div>'; tpl+= '<div class="Blog_line1"></div></div>'; $('#replyList .Blog_right1_8:first').before(tpl); $('.z_move_comment').html('').hide(); } else if(data.code == -1){ //showErrorMsg(data.info , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); } }, error: function(){//请求出错处理 } }); }); //底部发表评论 $('#submitmsg').click(function(){ if(allowComment == 1) { showErrorMsg('该博文不允许评论' , '消息提示'); return false; } var bid = $(this).attr('bid'); var toid = $(this).attr('toid'); var text = $('#reply').val(); var url = $(this).attr('url'); if(text == '' || text=='文明上网,理性发言...') { showErrorMsg('评论内容不能为空!' , '消息提示'); return false; } else { if(mb_strlen(text) > 1000){ showErrorMsg('评论内容不能超过500个汉字' , '消息提示'); return false; } } $.ajax({ type: "post", url: url , data: {'bid': bid , 'to' : toid ,'text': text}, dataType: 'json', success: function(data){ if(data.code == 1) { var tpl = '<div class="Blog_right1_8">'; tpl += '<div class="Blog_right_img1"><a href="' +data.info.url+ '" >' + data.info.header + '</a></div>'; tpl += '<div class="Blog_right_font1">'; tpl += '<p class="Blog_p5"><span><a href="' +data.info.url+ '" >' + data.info.username + '</a></span>' + data.info.dateline + '</p>'; tpl += '<p class="Blog_p6">' + data.info.content + '</p>'; tpl += '<div class="div1"><a href="javascript:void(0);" class="Blog_a10" toid=' + data.info.fuid + ' bid=' + data.info.bid + ' cid=' + data.info.cid + '>回复</a> |  <a class="comment_del_mark" style="cursor:pointer" url="' + data.info.delurl + '">删除</a> |  <a href="javascript:showJuBao(\'/blog/ViewReport.html\','+data.info.cid+')" class="box_report" cid="' + data.info.cid + '">举报</a></div>'; tpl += '<div class="z_move_comment" style="display:none"></div>'; tpl += '</div><div class="Blog_line1"></div></div>'; $('.Blog_tit3:first').after(tpl); $('#reply').val('文明上网,理性发言...'); } else if(data.code == -1) { showErrorMsg(data.info , '消息提示'); } }, error: function(){//请求出错处理 } }); }); //底部评论重置 $('#reset_comment').click(function(){ $('#reply').val('文明上网,理性发言...'); }); //取消回复 $('#qx_comment').live('click' , function(){ $('.z_move_comment').html('').hide(); }); $('#rmsg, #reply').live({ focus:function(){ if($(this).val() == '文明上网,理性发言...'){ $(this).val(''); } }, blur:function(){ if($(this).val() == ''){ $(this).val('文明上网,理性发言...'); } } }); //删除留言确认 $('.comment_del_mark').live('click' , function(){ var url = $(this).attr('url'); asyncbox.confirm('删除留言','确认', function(action){ if(action == 'ok') { location.href = url; } }); }); //删除时间确认 $('.del_article_id').click(function(){ var delurl = $(this).attr('delurl'); asyncbox.confirm('删除文章','确认', function(action){ if(action == 'ok') { location.href = delurl; } }); }); /* //字数限制 $('#rmsg, #reply').live('keyup', function(){ var id = $(this).attr('id'); var left = Util.calWbText($(this).val(), 500); var eid = '#errmsg'; if(id == 'reply') eid = '#rerrmsg'; if (left >= 0) $(eid).html('您还可以输入<span>' + left + '</span>字'); else $(eid).html('<font color="red">您已超出<span>' + Math.abs(left) + '</span>字 </font>'); }); */ //加载表情 $('#face').qqFace({id : 'facebox1', assign : 'reply', path : '/image/qqface/'}); $('#mface').qqFace({id : 'facebox', assign : 'rmsg', path:'/image/qqface/'}); /* $('#class_one_id').change(function(){ alert(123213); var id = parseInt($(this).val() , 10); if(id == 0) return false; $('.hidden_son_class span').each(function( index , dom ){ if( dom.attr('cid') == id ) { } }); }); */ //转载文章 var turn_url = "/blog/viewClassPart.html"; $('#repost_bar').click(function(){ if(isOnLine == '' ) { //showErrorMsg('登录之后才能进行此操作' , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); return false; } var id = $(this).attr('bid'); asyncbox.open({ id : 'turn_class_thickbox', url : turn_url, title : '转载文章', width : 330, height : 131, scroll : 'no', data : { 'id' : id }, callback : function(action){ if(action == 'close'){ $.cover(false); } } }); }); /* //转发文章 $('#repost_bar').live('click' , function(){ if(isOnLine == '' ) { //showErrorMsg('登录之后才能进行此操作' , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); return false; } var bid = $(this).attr('bid'); var url = $(this).attr('url'); asyncbox.confirm('转载文章','确认', function(action){ if(action == 'ok'){ $.ajax({ type:"POST", url:url, data: { 'bid' : bid }, dataType: 'json', success:function(msg){ if(msg.error == 0){ showSucceedMsg('转发成功!', '消息提示'); }else if(msg.error == 1){ //location.href = '/index.php?r=site/login'; showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); }else{ showErrorMsg(msg.error_content, '消息提示'); } } }); } }); }); */ }); </script> <!--该部分应该放在输出代码块的后面才起作用 --> <script type="text/javascript"> SyntaxHighlighter.autoloader( 'applescript /highlight/scripts/shBrushAppleScript.js', 'actionscript3 as3 /highlight/scripts/shBrushAS3.js', 'bash shell /highlight/scripts/shBrushBash.js', 'coldfusion cf /highlight/scripts/shBrushColdFusion.js', 'cpp c /highlight/scripts/shBrushCpp.js', 'c# c-sharp csharp /highlight/scripts/shBrushCSharp.js', 'css /highlight/scripts/shBrushCss.js', 'delphi pascal /highlight/scripts/shBrushDelphi.js', 'diff patch pas /highlight/scripts/shBrushDiff.js', 'erl erlang /highlight/scripts/shBrushErlang.js', 'groovy /highlight/scripts/shBrushGroovy.js', 'java /highlight/scripts/shBrushJava.js', 'jfx javafx /highlight/scripts/shBrushJavaFX.js', 'js jscript javascript /highlight/scripts/shBrushJScript.js', 'perl pl /highlight/scripts/shBrushPerl.js', 'php /highlight/scripts/shBrushPhp.js', 'text plain /highlight/scripts/shBrushPlain.js', 'py python /highlight/scripts/shBrushPython.js', 'ruby rails ror rb /highlight/scripts/shBrushRuby.js', 'scala /highlight/scripts/shBrushScala.js', 'sql /highlight/scripts/shBrushSql.js', 'vb vbnet /highlight/scripts/shBrushVb.js', 'xml xhtml xslt html /highlight/scripts/shBrushXml.js' ); SyntaxHighlighter.all(); function code_hide(id){ var code = document.getElementById(id).style.display; if(code == 'none'){ document.getElementById(id).style.display = 'block'; }else{ document.getElementById(id).style.display = 'none'; } } </script> <!--回顶部js2011.12.30--> <script language="javascript"> lastScrollY=0; function heartBeat(){ var diffY; if (document.documentElement && document.documentElement.scrollTop) diffY = document.documentElement.scrollTop; else if (document.body) diffY = document.body.scrollTop else {/*Netscape stuff*/} percent=.1*(diffY-lastScrollY); if(percent>0)percent=Math.ceil(percent); else percent=Math.floor(percent); document.getElementById("full").style.top=parseInt(document.getElementById("full").style.top)+percent+"px"; lastScrollY=lastScrollY+percent; if(lastScrollY<200) { document.getElementById("full").style.display="none"; } else { document.getElementById("full").style.display="block"; } } var gkuan=document.body.clientWidth; var ks=(gkuan-960)/2-30; suspendcode="<div id=\"full\" style='right:-30px;POSITION:absolute;TOP:500px;z-index:100;width:26px; height:86px;cursor:pointer;'><a href=\"javascript:void(0)\" onclick=\"window.scrollTo(0,0);\"><img src=\"\/image\/top.png\" /></a></div>" document.write(suspendcode); window.setInterval("heartBeat()",1); </script> <!-- footer --> <div class="Blog_footer" style='clear:both'> <div><a href="http://www.chinaunix.net/about/index.shtml" target="_blank" rel="nofollow">关于我们</a> | <a href="http://www.it168.com/bottomfile/it168.shtml" target="_blank" rel="nofollow">关于IT168</a> | <a href="http://www.chinaunix.net/about/connect.html" target="_blank" rel="nofollow">联系方式</a> | <a href="http://www.chinaunix.net/about/service.html" target="_blank" rel="nofollow">广告合作</a> | <a href="http://www.it168.com//bottomfile/flgw/fl.htm" target="_blank" rel="nofollow">法律声明</a> | <a href="http://account.chinaunix.net/register?url=http%3a%2f%2fblog.chinaunix.net" target="_blank" rel="nofollow">免费注册</a> <p>Copyright 2001-2010 ChinaUnix.net All Rights Reserved 北京皓辰网域网络信息技术有限公司. 版权所有 </p> <div>感谢所有关心和支持过ChinaUnix的朋友们 <p><a href="http://beian.miit.gov.cn/">16024965号-6 </a></p> </div> </div> </div> </div> <script language="javascript"> //全局错误提示弹出框 function showErrorMsg(content, title, url){ var url = url || ''; var title = title || '消息提示'; var html = ''; html += '<div class="HT_layer3_1 HT_layer3_2"><ul><li><p><span class="login_span1"></span>' + content + '</p></li>'; if(url == '' || url.length == 0){ html += '<li class="HT_li1"><input type="button" class="HT_btn2" onclick=\'close_windows("error_msg")\'></li>'; } else { html += '<li class="HT_li1"><input type="button" class="login_btn1" onclick="location.href=\'' + url + '\'"></li>'; } html += '</ul></div>'; $.cover(true); asyncbox.open({ id: 'error_msg', title : title, html : html, 'callback' : function(action){ if(action == 'close'){ $.cover(false); } } }); } //全局正确提示 function showSucceedMsg(content, title , url ){ var url = url || ''; var title = title || '消息提示'; var html = ''; html += '<div class="HT_layer3_1 HT_layer3_2"><ul><li><p><span class="login_span2"></span>' + content + '</p></li>'; if(url == '' || url.length == 0){ html += '<li class="HT_li1"><input type="button" class="HT_btn2" onclick=\'close_windows("error_msg")\'></li>'; } else { html += '<li class="HT_li1"><input type="button" class="HT_btn2" onclick="location.href=\'' + url + '\'"></li>'; } html += '</ul></div>'; $.cover(true); asyncbox.open({ id: 'error_msg', title : title, html : html, 'callback' : function(action){ if(action == 'close'){ $.cover(false); } } }); } //关闭指定id的窗口 function close_windows(id){ $.cover(false); $.close(id); } //公告 var tID; var tn; // 高度 var nStopTime = 5000; // 不同行间滚动时间隔的时间,值越小,移动越快 var nSpeed = 50; // 滚动时,向上移动一像素间隔的时间,值越小,移动越快 var isMove = true; var nHeight = 25; var nS = 0; var nNewsCount = 3; /** * n 用于表示是否为第一次运行 **/ function moveT(n) { clearTimeout(tID) var noticev2 = document.getElementById("noticev2") nS = nSpeed; // 只在第一次调用时运行,初始化环境(有没有参数) if (n) { // 设置行高 noticev2.style.lineHeight = nHeight + "px"; // 初始化显示位置 tn = 0; // 刚进入时在第一行停止时间 nS = nStopTime; } // 判断鼠标是否指向层 if (isMove) { // 向上移动一像素 tn--; // 如果移动到最下面一行了,则移到顶行 if (Math.abs(tn) == nNewsCount * nHeight) { tn = 0; } // 设置位置 noticev2.style.marginTop = tn + "px"; // 完整显示一行时,停止一段时间 if (tn % nHeight == 0) { nS = nStopTime; } } tID = setTimeout("moveT()", nS); } moveT(1); // 此处可以传入任何参数 </script> <script type="text/javascript"> // var _gaq = _gaq || []; // _gaq.push(['_setAccount', 'UA-20237423-2']); // _gaq.push(['_setDomainName', '.chinaunix.net']); // _gaq.push(['_trackPageview']); // // (function() { // var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; // ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; // var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); // })(); </script> <script type="text/javascript"> var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://"); document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3F0ee5e8cdc4d43389b3d1bfd76e83216b' type='text/javascript'%3E%3C/script%3E")); function link(t){ var href= $(t).attr('href'); href+="?url="+encodeURIComponent(location.href); $(t).attr('href',href); //setCookie("returnOutUrl", location.href, 60, "/"); } </script> <script type="text/javascript" src="/js/jquery.qqFace.js"></script> </body> </html>