天空小小岛技术论坛

 找回密码
 注册
搜索
查看: 11531|回复: 1
打印 上一主题 下一主题

[分享] Discuz! dfopen 优化 timeout 版

[复制链接]
跳转到指定楼层
1#
一粒米 发表于 2010-10-17 21:56:53 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
Discuz! 里的函数 dfopen 确实很强大, 不过会遇到一点点小麻烦, 就是并不能完全按照 timeout 设置的时间中断连接.
原函数中只有一次判断, 也就是说当连接成功后, 数据未发送完毕之前连接中断了, 并不能中断该连接, 将造成程序锁死.
所以对原函数做了一些改动, 以避免该情况的发生.


  1. /*
  2. 改进后的 dfopen
  3. */

  4. function dfopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE) {
  5.         $return = '';
  6.         $matches = parse_url($url);
  7.         $host = $matches['host'];
  8.         $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';
  9.         $port = !empty($matches['port']) ? $matches['port'] : 80;

  10.         if($post) {
  11.                 $out = "POST $path HTTP/1.0\r\n";
  12.                 $out .= "Accept: */*\r\n";
  13.                 //$out .= "Referer: $boardurl\r\n";
  14.                 $out .= "Accept-Language: zh-cn\r\n";
  15.                 $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
  16.                 $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  17.                 $out .= "Host: $host\r\n";
  18.                 $out .= 'Content-Length: '.strlen($post)."\r\n";
  19.                 $out .= "Connection: Close\r\n";
  20.                 $out .= "Cache-Control: no-cache\r\n";
  21.                 $out .= "Cookie: $cookie\r\n\r\n";
  22.                 $out .= $post;
  23.         } else {
  24.                 $out = "GET $path HTTP/1.0\r\n";
  25.                 $out .= "Accept: */*\r\n";
  26.                 //$out .= "Referer: $boardurl\r\n";
  27.                 $out .= "Accept-Language: zh-cn\r\n";
  28.                 $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  29.                 $out .= "Host: $host\r\n";
  30.                 $out .= "Connection: Close\r\n";
  31.                 $out .= "Cookie: $cookie\r\n\r\n";
  32.         }
  33.         $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
  34.         if(!$fp) {
  35.                 return '';//note $errstr : $errno \r\n
  36.         } else {
  37.                 stream_set_blocking($fp, $block);
  38.                 stream_set_timeout($fp, $timeout);
  39.                 @fwrite($fp, $out);
  40.                 while (!feof($fp)) {
  41.                         $status = stream_get_meta_data($fp);
  42.                         if(!empty($status['timed_out'])) {
  43.                                 return '';
  44.                         }
  45.                         if(($header = @fgets($fp)) && ($header == "\r\n" ||  $header == "\n")) {
  46.                                 break;
  47.                         }
  48.                 }
  49.                 $stop = false;
  50.                 while(!feof($fp) && !$stop) {
  51.                         $status = stream_get_meta_data($fp);
  52.                         if(!empty($status['timed_out'])) {
  53.                                 return '';
  54.                         }
  55.                         $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
  56.                         $return .= $data;
  57.                         if($limit) {
  58.                                 $limit -= strlen($data);
  59.                                 $stop = $limit <= 0;
  60.                         }
  61.                 }
  62.                 @fclose($fp);
  63.                 return $return;
  64.         }
  65. }



复制代码
原函数

  1. /*
  2. Discuz! 原 dfopen 函数
  3. */
  4. function dfopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE) {
  5.         $return = '';
  6.         $matches = parse_url($url);
  7.         $host = $matches['host'];
  8.         $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';
  9.         $port = !empty($matches['port']) ? $matches['port'] : 80;

  10.         if($post) {
  11.                 $out = "POST $path HTTP/1.0\r\n";
  12.                 $out .= "Accept: */*\r\n";
  13.                 //$out .= "Referer: $boardurl\r\n";
  14.                 $out .= "Accept-Language: zh-cn\r\n";
  15.                 $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
  16.                 $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  17.                 $out .= "Host: $host\r\n";
  18.                 $out .= 'Content-Length: '.strlen($post)."\r\n";
  19.                 $out .= "Connection: Close\r\n";
  20.                 $out .= "Cache-Control: no-cache\r\n";
  21.                 $out .= "Cookie: $cookie\r\n\r\n";
  22.                 $out .= $post;
  23.         } else {
  24.                 $out = "GET $path HTTP/1.0\r\n";
  25.                 $out .= "Accept: */*\r\n";
  26.                 //$out .= "Referer: $boardurl\r\n";
  27.                 $out .= "Accept-Language: zh-cn\r\n";
  28.                 $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  29.                 $out .= "Host: $host\r\n";
  30.                 $out .= "Connection: Close\r\n";
  31.                 $out .= "Cookie: $cookie\r\n\r\n";
  32.         }
  33.         $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
  34.         if(!$fp) {
  35.                 return '';
  36.         } else {
  37.                 stream_set_blocking($fp, $block);
  38.                 stream_set_timeout($fp, $timeout);
  39.                 @fwrite($fp, $out);
  40.                 $status = stream_get_meta_data($fp);
  41.                 if(!$status['timed_out']) {
  42.                         while (!feof($fp)) {
  43.                                 if(($header = @fgets($fp)) && ($header == "\r\n" ||  $header == "\n")) {
  44.                                         break;
  45.                                 }
  46.                         }

  47.                         $stop = false;
  48.                         while(!feof($fp) && !$stop) {
  49.                                 $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
  50.                                 $return .= $data;
  51.                                 if($limit) {
  52.                                         $limit -= strlen($data);
  53.                                         $stop = $limit <= 0;
  54.                                 }
  55.                         }
  56.                 }
  57.                 @fclose($fp);
  58.                 return $return;
  59.         }
  60. }
复制代码
2#
superlk1 发表于 2012-10-19 16:57:39 | 只看该作者
顶楼主 楼主观点不错  

www.ZTSUN.COM  充气娃娃多少钱
www.szyuantaidz.com  脚气
www.yushenjt.com  阴茎短小
www.btdyj.com  脚臭
www.1tidc.com  蹭网器
www.cn-xyz.com  卡皇
www.hfloge.com  白发治疗
www.hnshiqi.com  英国卫裤
www.wzeas.com  治疗白发
www.qdzjzc.com  伟哥官网
www.szjczq.com  日本充气娃娃
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|天空小小岛  |京ICP备2025130156号|

GMT+8, 2025-6-21 19:11 , Processed in 0.095298 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表