国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php多線程thread開發與應用的例子

2024-05-04 21:47:52
字體:
來源:轉載
供稿:網友

php多線程的使用,首先需要PHP5.3以上版本,并安裝pthreads PHP擴展,可以使PHP真正的支持多線程,擴展如何安裝請自行百度.

PHP擴展下載:https://github.com/krakjoe/pthreads

PHP手冊文檔:http://php.net/manual/zh/book.pthreads.php

在安裝好擴展之后,就可以運用多線程了,下面貼個通過搜索結果抓取百度網盤內容的代碼:

  1. <?php 
  2. include 'include/CurlLoad.class.php'// 引入讀取庫 
  3. /** 
  4.  * 多線程抓取內容 
  5.  * @param array $url 待抓取URL列表集合 
  6.  * @return 成功返回指定內容,失敗返回NULL 
  7.  */ 
  8. function vget($url) { 
  9.  $ret = BaiduSRLinksGet ( $url, 1 ); // 獲取結果列表地址 
  10.  if ($ret != null) { 
  11.   if (array_key_exists ( "links"$ret )) { 
  12.    $infos = array (); 
  13.    $number = count ( $ret ['links'] ); 
  14.    for($i = 0; $i < $number$i ++) {//循環創建線程對象 
  15.     $thread_array [$i] = new baidu_thread_run ( $ret ['links'] [$i] ); 
  16.     $thread_array [$i]->start (); 
  17.    } 
  18.    foreach ( $thread_array as $thread_array_key => $thread_array_value ) {//檢查線程是否執行結束 
  19.     while ( $thread_array [$thread_array_key]->isRunning () ) { 
  20.      usleep ( 10 ); 
  21.     } 
  22.     if ($thread_array [$thread_array_key]->join ()) {//如果執行結束,取出結果 
  23.      $temp = $thread_array [$thread_array_key]->data; 
  24.      if ($temp != null) 
  25.       $infos ['res'] [] = $temp
  26.     } 
  27.    } 
  28.    $infos ['pages'] = $ret ['pages']; 
  29.    $infos ['status'] = "1"
  30.   } else 
  31.   $infos = null; 
  32.  } else 
  33.   $infos = null; 
  34.  return $infos
  35. /** 
  36.  * 獲取百度搜索結果列表URL 
  37.  * 
  38.  * @param string $url 
  39.  *         搜索結果頁URL 
  40.  * @param int $format 
  41.  *         默認$format=0,獲取默認地址;$format=1獲取跳轉后真實地址 
  42.  * @return NULL multitype:array() 
  43.  */ 
  44. function BaiduSRLinksGet($url$format = 0) { 
  45.  $html = CurlLoad::HtmlGet ( $url ); // 獲取頁面 
  46.  if ($html == null) 
  47.   return null; 
  48.  try { 
  49.   preg_match_all ( "/"url":"(?<links>.*)"}/"$html$rets ); // 搜索結果鏈接篩選 
  50.   if (! array_key_exists ( 'links'$rets )) // 如果數組中不包含Links鍵名,表示獲取失敗 
  51.    return null; 
  52.   $ret = array (); 
  53.   if ($format == 1) { 
  54.    $number = count ( $rets ['links'] ); 
  55.    for($i = 0; $i < $number$i ++) { 
  56.     $headr_temp = CurlLoad::Get_Headers ( $rets ['links'] [$i], 1 ); // 通過headr獲取真實地址 
  57.     if (array_key_exists ( "Location"$headr_temp )) 
  58.      $ret ['links'] [$i] = $headr_temp ['Location']; 
  59.     else 
  60.      $ret ['links'] = $rets ['links']; 
  61.    } 
  62.   } else 
  63.    $ret ['links'] = $rets ['links']; 
  64.   preg_match_all ( '/href="?/s?wd=site%3Apan.baidu.com%20(?<url>.+?)&ie=utf-8">/'$html$out ); 
  65.   unset ( $out ['url'] [0] ); 
  66.   $number = count ( $out ['url'] ); 
  67.   for($i = 1; $i < $number$i ++) { 
  68.    preg_match_all ( '/&pn=(.*)/'$out ['url'] [$i], $temp ); 
  69.    $ret ['pages'] [$temp [1] [0] / 10] = base64_encode ( $out ['url'] [$i] ); 
  70.   } 
  71.   return $ret
  72.  } catch ( Exception $e ) { 
  73.   WriteLog ( $e ); 
  74.   return null; 
  75.  } 
  76. /** 
  77.  * 百度網盤資源信息獲取 
  78.  * 
  79.  * @param string $url 
  80.  *         網盤資源頁URL 
  81.  * @return NULL array 
  82.  */ 
  83. function PanInfoGet($url) { 
  84.  $html = CurlLoad::HtmlGet ( $url ); // 獲取頁面 
  85.  if ($html == null) 
  86.   return null; 
  87.  try { 
  88.   if (preg_match_all ( "/文件名:(?<name>.*) 文件大小:(?<size>.*) 分享者:(?<user>.*) 分享時間:(?<date>.*) 下載次數:(?<number>[0-9]+)/"$html$ret ) == 0) 
  89.    return null; 
  90.   $rets ['name'] = $ret ['name'] [0]; 
  91.   $rets ['size'] = $ret ['size'] [0]; 
  92.   $rets ['user'] = $ret ['user'] [0]; 
  93.   $rets ['date'] = $ret ['date'] [0]; 
  94.   $rets ['number'] = $ret ['number'] [0]; 
  95.   $rets ['link'] = $url
  96.   return $rets
  97.  } catch ( Exception $e ) { 
  98.   WriteLog ( $e ); 
  99.   return null; 
  100.  } 
  101. function WriteLog($str) { 
  102.  $file = fopen ( "../error.log""a+" ); 
  103.  fwrite ( $file"Warning:" . date ( "Y/m/d H:i:s" ) . ":" . $str . "rn" ); 
  104.  fclose ( $file ); 
  105. /** 
  106.  * 多線程抓取對象 
  107.  * @author MuXi 
  108.  * 
  109.  */ 
  110. class baidu_thread_run extends Thread { 
  111.  public $url
  112.  public $data
  113.  public function __construct($url) { 
  114.   $this->url = $url
  115.  } 
  116.  public function run() { 
  117.   if (($url = $this->url)) { 
  118.    $this->data = PanInfoGet ( $url );//線程執行方法 
  119.   } 
  120.  } 
  121. ?> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 枣强县| 保德县| 白银市| 逊克县| 广饶县| 富裕县| 枣庄市| 长垣县| 临夏市| 珲春市| 荣成市| 乐至县| 宾川县| 广德县| 蚌埠市| 山丹县| 会昌县| 温州市| 泗阳县| 仙居县| 广水市| 观塘区| 虹口区| 伊川县| 陇川县| 三穗县| 桓仁| 拜泉县| 定南县| 崇信县| 成都市| 城口县| 电白县| 新蔡县| 富裕县| 奉化市| 禹州市| 浦北县| 河东区| 女性| 叶城县|