存档

‘PHP’ 分类的存档

补全URL 函数

2010年12月21日 admin 2 条评论
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
   //修正链接补上之前的url
	function correctUrl($base, $url)
	{
		if(strpos($url, "http://") === 0) {
			return $this->trimUrl($url);
		}
		$urlInfo = parse_url($base);
 
		$absUrl = "http://".$urlInfo["host"];
		if($url{0} == "/") {
			$absUrl .= $url;
		} elseif($url{0} == "?") {
			$absUrl .= $urlInfo["path"].$url;
		} else {
			$pos = strrpos($urlInfo["path"], "/");
			if($pos === false) {
				$absUrl .= "/".$url;
			} else {
				$absUrl .= substr($urlInfo["path"], 0, $pos + 1).$url;
			}
		}
		$absUrl= $this->trimUrl($absUrl);
		return $absUrl;
	}
分类: PHP 标签:

file_get_contents获取搜狐博客乱码问题

2010年12月3日 admin 没有评论

file_get_contents — 将整个文件读入一个字符串
说明
string file_get_contents ( string filename [, bool use_include_path [, resource context [, int offset [, int maxlen]]]] )

一般遇到的乱码问题是gbk与utf-8 之间转换的问题,这个用 php 的 mb_convert_encoding 可以解决, 搜狐的抓出来却是乱码。 google了一下, 搜狐页面编码格式的问题。
看搜狐的header
Accept-Encoding: gzip,deflate

用一个函数 解压就可以了。 ^_^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
function gzdecode($data) { 
  $len = strlen($data); 
  if ($len < 18 || strcmp(substr($data,0,2),"\x1f\x8b")) { 
    return null;  // Not GZIP format (See RFC 1952) 
  } 
  $method = ord(substr($data,2,1));  // Compression method 
  $flags  = ord(substr($data,3,1));  // Flags 
  if ($flags & 31 != $flags) { 
    // Reserved bits are set -- NOT ALLOWED by RFC 1952 
    return null; 
  } 
  // NOTE: $mtime may be negative (PHP integer limitations) 
  $mtime = unpack("V", substr($data,4,4)); 
  $mtime = $mtime[1]; 
  $xfl   = substr($data,8,1); 
  $os    = substr($data,8,1); 
  $headerlen = 10; 
  $extralen  = 0; 
  $extra     = ""; 
  if ($flags & 4) { 
    // 2-byte length prefixed EXTRA data in header 
    if ($len - $headerlen - 2 < 8) { 
      return false;    // Invalid format 
    } 
    $extralen = unpack("v",substr($data,8,2)); 
    $extralen = $extralen[1]; 
    if ($len - $headerlen - 2 - $extralen < 8) { 
      return false;    // Invalid format 
    } 
    $extra = substr($data,10,$extralen); 
    $headerlen += 2 + $extralen; 
  } 
 
  $filenamelen = 0; 
  $filename = ""; 
  if ($flags & 8) { 
    // C-style string file NAME data in header 
    if ($len - $headerlen - 1 < 8) { 
      return false;    // Invalid format 
    } 
    $filenamelen = strpos(substr($data,8+$extralen),chr(0)); 
    if ($filenamelen === false || $len - $headerlen - $filenamelen - 1 < 8) { 
      return false;    // Invalid format 
    } 
    $filename = substr($data,$headerlen,$filenamelen); 
    $headerlen += $filenamelen + 1; 
  } 
 
  $commentlen = 0; 
  $comment = ""; 
  if ($flags & 16) { 
    // C-style string COMMENT data in header 
    if ($len - $headerlen - 1 < 8) { 
      return false;    // Invalid format 
    } 
    $commentlen = strpos(substr($data,8+$extralen+$filenamelen),chr(0)); 
    if ($commentlen === false || $len - $headerlen - $commentlen - 1 < 8) { 
      return false;    // Invalid header format 
    } 
    $comment = substr($data,$headerlen,$commentlen); 
    $headerlen += $commentlen + 1; 
  } 
 
  $headercrc = ""; 
  if ($flags & 1) { 
    // 2-bytes (lowest order) of CRC32 on header present 
    if ($len - $headerlen - 2 < 8) { 
      return false;    // Invalid format 
    } 
    $calccrc = crc32(substr($data,0,$headerlen)) & 0xffff; 
    $headercrc = unpack("v", substr($data,$headerlen,2)); 
    $headercrc = $headercrc[1]; 
    if ($headercrc != $calccrc) { 
      return false;    // Bad header CRC 
    } 
    $headerlen += 2; 
  } 
 
  // GZIP FOOTER - These be negative due to PHP's limitations 
  $datacrc = unpack("V",substr($data,-8,4)); 
  $datacrc = $datacrc[1]; 
  $isize = unpack("V",substr($data,-4)); 
  $isize = $isize[1]; 
 
  // Perform the decompression: 
  $bodylen = $len-$headerlen-8; 
  if ($bodylen < 1) { 
    // This should never happen - IMPLEMENTATION BUG! 
    return null; 
  } 
  $body = substr($data,$headerlen,$bodylen); 
  $data = ""; 
  if ($bodylen > 0) { 
    switch ($method) { 
      case 8: 
        // Currently the only supported compression method: 
        $data = gzinflate($body); 
        break; 
      default: 
        // Unknown compression method 
        return false; 
    } 
  } else { 
    // I'm not sure if zero-byte body content is allowed. 
    // Allow it for now...  Do nothing... 
  } 
 
  // Verifiy decompressed size and CRC32: 
  // NOTE: This may fail with large data sizes depending on how 
  //       PHP's integer limitations affect strlen() since $isize 
  //       may be negative for large sizes. 
  if ($isize != strlen($data) || crc32($data) != $datacrc) { 
    // Bad format!  Length or CRC doesn't match! 
    return false; 
  } 
  return $data; 
}

PHP导出Excel(二) 使用Spreadsheet_Excel_Writer

2010年11月4日 admin 没有评论

使用Pear 的 Spreadsheet_Excel_Writer 来导出Excel 下载地址
简单生成:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
//调用Writer.php
require_once 'Spreadsheet/Excel/Writer.php';
//创建Workbook
$workbook = new Spreadsheet_Excel_Writer();
//定义导出Excel文件名
$workbook->send('wenan8.xls');
//创建Worksheet
$worksheet =& $workbook->addWorksheet('My first worksheet');
//设置字体大小
$format_column = & $workbook->addformat(array('Size'=>9,'Bold'=>1));
//数据写入
//标题行(第一行)
$worksheet->write(0, 0, 'Name', $format_column);
$worksheet->write(0, 1, 'Age', $format_column);
//第一个人(第二行)
$worksheet->write(1, 0, 'John Smith');
$worksheet->write(1, 1, 30);
//第二个人(第三行)
$worksheet->write(2, 0, 'Johann Schmidt');
$worksheet->write(2, 1, 31);
//第三个人(第四行)
$worksheet->write(3, 0, 'Juan Herrera');
$worksheet->write(3, 1, 32);
//关闭Workbook
$workbook->close();
?>

需要导出多个Sheet的Excel ,带数据库的操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
function exportExcel($data,$filePath,$type)
	{
		$this->_data=$data;
		$workbook = new Spreadsheet_Excel_Writer($filePath); // 初始化类		
                $arr=$this->sheetData();
		foreach($arr AS $k=>$v)
		{
			$worksheet =& $workbook->addWorksheet($v['title']); 
			//写入数据字段的标题
			$i=0;
			foreach($columns AS $k2=>$v2)
			{
				$worksheet->writeString(0,$i,$v2);
				$i++;
			}
			$i=1;
			//print_r($this->_data[$v['type']]);
			foreach($this->_data[$v['type']] AS $k2=>$v2){
		    if($k2>=$v['start']-1 && $k2<$v['end']-1)
		    {
				$j=0;
				foreach($columns AS $k3=>$v3)
				{
					if(isNumber($v2[$k3]))  $worksheet->writeNumber($i,$j,$v2[$k3]);
					else $worksheet->writeString($i,$j,$v2[$k3]);
 
					$j++;
				}
				$i++;
		    }
			}
		}
		$workbook->close(); // 完成下载
		return $filePath;
	}
 //将数据按照sheet分类
function sheetData()
	{	$newData=array();
		$sheets=array();
		foreach($this->_data AS $k=>$v)
		{
			if(!array_key_exists($v['dType'],$newData)){
				$newData[$v['dType']]=array();
			}
			$newData[$v['dType']][]=$v;
		}
 
		$types=array_keys($newData);
		$types=implode('\',\'',$types);
		$types='\''.$types.'\'';
 
		$query="SELECT name,value FROM datatype WHERE value IN (".$types.") ORDER BY ordering";
 
		$res=$db->GetAll($query);
 
		foreach($res AS $v){
			$sheets[$v['value']]=$v['name'];
		}
		$this->_data=$newData;
		return $sheets;
	}

欢迎交流: QQ:276200108

PHP导出Excel(一)

2010年11月4日 admin 没有评论

PHP导出Excel方法很多,网上也有很多成熟的类,现在用最简单的方法导出一个Excel, 输出一个Table,然后用header 类型改为Excel就可以.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
includer_once("config.php");
$sql="select * from user";
$result=mysql_query($sql);
$data_array=mysql_fetch_array($result);
$keynames=array("orderTrade"=>"订单号","orderRecName"=>"收货人姓名","orderCount"=>"数量","orderPhone"=>"电话","orderRecMail"=>"Email","orderRecAddress"=>"地址","orderRecZip"=>"邮编","receiving"=>"收货时间");
function down_xls($data_array,$keynames,$filename) {
   //组合表头行,以制表符\t分隔,并转码为gb18030
   $title = implode("\t", array_values($keynames));
   $title = iconv("UTF-8","GB18030", $title);
 
   $result[] = $title;
  //组合表体行
   foreach ($data_array as $data){
    //按行组合数据体
      $a1 = array();
      foreach ($keynames as $k => $v){
         if ($k == "endtime"){
            $data[$k] = date("Y-m-d H:i", $data[$k]);
         }
         //utf8转码到gb18030,为了windows正常显示
         $a1[] = iconv("UTF-8","GB18030", preg_replace("/[\t]+/"," ",$data[$k]));
      }
      //以制表符分隔每列
      $result[] = implode("\t", $a1);
   }
 
  //为了windows下正常显示,以\r\n为换行符,分隔数据字符串
   $result = implode("\r\n", $result);
   header("Content-type: text/html; charset=GB18030");
   header("Content-Disposition: attachment; filename=\"".$filename.".xls\"");
   exit($result);
} 
down_xls($data_array,$keynames,"dataxls"); 
?>
分类: PHP 标签: , ,

将文字表述的时间 转化为相应的时间

2010年11月3日 admin 没有评论
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
     /*
	 *将文字表述的时间 转化为相应的时间
	*/
	function changToDateTime($str)
	{
		$str=trim($str);
		$res=array();
		//如果本身是时间格式
		if(preg_match("/(\d{4}-\d{1,2}-\d{1,2})\s(\d{1,2}:\d{1,2}:\d{1,2})/",$str)){
			$res=explode(" ",$str);
			$res[2]=$str;
		}elseif(preg_match('/(\d{4})年(\d{1,2})月(\d{1,2})日\s(\d{1,2}:\d{1,2})/',$str,$match)){
			$res[0]=$match[1].'-'.$match[2].'-'.$match[3];
			$res[1]=$match[4].':00';
			$res[2]=$res[0].' '.$res[1];			
		}elseif(preg_match('/(\d{1,2})月(\d{1,2})日\s(\d{1,2}:\d{1,2})/',$str,$match)){
			$res[0]=date("Y").'-'.$match[1].'-'.$match[2];
			$res[1]=$match[3].':00';
			$res[2]=$res[0].' '.$res[1];			
		}elseif(preg_match('/(\d{4})\s*年(\d{1,2})\s*月(\d{1,2})\s*日/',$str,$match)){
			//只有日期数据的格式
			$res[0]=$match[1].'-'.$match[2].'-'.$match[3];
			$res[1]='00:00:00';
			$res[2]=$res[0].' '.$res[1];	
		}elseif(preg_match("/(\d+)\s*秒前/",$str,$match)){
			$dateTime=time()-$match[1];
			$dateTime=date("Y-m-d H:i:s",$dateTime);
			$res=explode(" ",$dateTime);
			$res[2]=$dateTime;			
		}elseif(preg_match("/(\d+)\s*分钟前/",$str,$match)){//如果是分钟格式 如:3分钟前
			$dateTime=time()-$match[1]*60;
			$dateTime=date("Y-m-d H:i:s",$dateTime);
			$res=explode(" ",$dateTime);
			$res[2]=$dateTime;
		}elseif(preg_match("/(\d+)\s*小时前/",$str,$match)){//如果是小时格式
			$dateTime=time()-$match[1]*3600;
			$dateTime=date("Y-m-d H:i:s",$dateTime);
			$res=explode(" ",$dateTime);
			$res[2]=$dateTime;
		}elseif(preg_match("/(\d+)\s*天前/",$str,$match)){
			$dateTime=time()-$match[1]*3600*24;
			$dateTime=date("Y-m-d H:i:s",$dateTime);
			$res=explode(" ",$dateTime);
			$res[2]=$dateTime;
		}elseif(preg_match("/(\d+)\s*[h|H]ours/",$str,$match)){
			//英文版本的多少个小时以前
			$dateTime=time()-$match[1]*60;
			$dateTime=date("Y-m-d H:i:s",$dateTime);
			$res=explode(" ",$dateTime);
			$res[2]=$dateTime;
		}elseif(preg_match("/([a-zA-Z])+\s+(\d{1,2}):(\d{1,2}:\d{1,2})\s+[A|P]M/",$str,$match)){
			//英文版本带昨天 之类 的说明 
			if($match[1]=='Yesterday'){
				$res[0]=date("Y-m-d",time()-86400);
			}else{
				$res[0]=date("Y-m-d"); //默认设置为今天 会出错!!根据 实际 情况 完善
			}
			//如果 是下等,时间要加12个小时
			if($match[4]=='PM'){
				$match[2]=intval($match[2]); //应该是去年前面 的0! 待改
				$match[2]+=12;
			}
			$res[1]=$match[2].":".$match[3];			
			$res[2]=$res[0]." ".$res[1];
		}elseif(preg_match("/\d{1,2}\/\d{1,2}\/10/",$str,$match)){
			$res[0]="2010-".$match[1]."-".$match[2];
			$res[1]="00:00:00"; 
			$res[2]=$res[0]." ".$res[1];
		}elseif(preg_match('/(\d+)\s*[M|m]inutes/',$str,$match)){
			$dateTime=time()-$match[1]*60;
			$dateTime=date("Y-m-d H:i:s",$dateTime);
			$res=explode(" ",$dateTime);
			$res[2]=$dateTime;
		}elseif(preg_match('/前天 (\d{1,2}:\d{1,2})/',$str,$match)){
			$dateTime=time()-24*60*60*2;
			$dateTime=date("Y-m-d",$dateTime).' '.$match[1].':00';
			$res=explode(" ",$dateTime);
			$res[2]=$dateTime;
		}elseif(preg_match('/昨天 (\d{1,2}:\d{1,2})/',$str,$match)){
			$dateTime=time()-24*60*60;
			$dateTime=date("Y-m-d",$dateTime).' '.$match[1].':00';
			$res=explode(" ",$dateTime);
			$res[2]=$dateTime;
		}elseif(preg_match('/今天 (\d{1,2}:\d{1,2})/',$str,$match)){
			$dateTime=date("Y-m-d").' '.$match[1].':00';
			$res=explode(" ",$dateTime);
			$res[2]=$dateTime;
		}elseif(preg_match('/(\d{1,2}-\d{1,2}) (\d{1,2}:\d{1,2})/',$str,$match)){
			$dateTime=date("Y").'-'.$match[1].' '.$match[2].':00';
			$res=explode(" ",$dateTime);
			$res[2]=$dateTime;
		}elseif(preg_match('/(\d{2,4}-\d{1,2}-\d{1,2}) (\d{1,2}:\d{1,2})/',$str,$match)){
			$dateTime=$match[1].' '.$match[2].':00';
			$res=explode(" ",$dateTime);
			$res[2]=$dateTime;
		}elseif(preg_match('/(\d+)\s*Seconds/',$str,$match)){
			$dateTime=time()-$match[1];
			$dateTime=date("Y-m-d H:i:s",$dateTime);
			$res=explode(" ",$dateTime);
			$res[2]=$dateTime;
		}elseif(preg_match('/([a-zA-Z]+)\s+(\d+).*?(\d{4})/',$str,$match)){
			//格式:Mar 1,2010
			$m=$this->monthSimple($match[1]);
			$res[0]=$match[3].'-'.$m.'-'.$match[2];
			$res[1]="00:00:00";
			$res[2]=$res[0]." ".$res[1];
		}elseif($str=='今天'){
			$res[0]=date("Y-m-d");
			$res[1]="00:00:00";
			$res[2]=$res[0]." ".$res[1];
		}elseif($str=='昨天') {
			$dateTime=time()-86400;
			$res[0]=date("Y-m-d",$dateTime);
			$res[1]="00:00:00";
			$res[2]=$res[0]." ".$res[1];
		}elseif($str=='前天'){
			$res[0]=date("Y-m-d",(time()-86400*2));
			$res[1]="00:00:00";
			$res[2]=$res[0]." ".$res[1];
		}elseif($str=='刚刚'){
			$res[0]=date("Y-m-d");
			$res[1]=date("H:i:s");
			$res[2]=$res[0]." ".$res[1];
		}else{ 
			$res[0]="0000-00-00";
			$res[1]="00:00:00";
			$res[2]=$res[0]." ".$res[1];
		}
		return $res;
	}
分类: PHP 标签: ,

Windows下SVN的配置详解

2010年10月29日 admin 没有评论

Subversion,简称SVN,是一个开放源代码的版本控制系统。 额. 灰常好用。

准备工作 :
下载:
服务器端程序下载: http://subversion.tigris.org/getting.html#binary-packages

Windows常用客户端TortoiseSVN:http://tortoisesvn.net/downloads

好了。win下 傻瓜式 下一步下一步安装完毕(装完客户端需要重启)。 现在做一下配置一个版本库。

1:创建一个版本库 ,可以在任意一个盘里 右键–选择创建

阅读全文…

分类: PHP 标签: , , ,

PHP fsockopen 函数

2010年10月28日 admin 没有评论

Description
resource fsockopen ( string target [, int port [, int &errno [, string &errstr [, float timeout]]]] )

Initiates a socket connection to the resource specified by target. PHP supports targets in the Internet and Unix domains as described in 附录 N. A list of supported transports can also be retrieved using stream_get_transports().

注: If you need to set a timeout for reading/writing data over the socket, use stream_set_timeout(), as the timeout parameter to fsockopen() only applies while connecting the socket.

As of PHP 4.3.0, if you have compiled in OpenSSL support, you may prefix the hostname with either ‘ssl://’ or ‘tls://’ to use an SSL or TLS client connection over TCP/IP to connect to the remote host.

fsockopen() returns a file pointer which may be used together with the other file functions (such as fgets(), fgetss(), fwrite(), fclose(), and feof()).

If the call fails, it will return FALSE and if the optional errno and errstr arguments are present they will be set to indicate the actual system level error that occurred in the system-level connect() call. If the value returned in errno is 0 and the function returned FALSE, it is an indication that the error occurred before the connect() call. This is most likely due to a problem initializing the socket. Note that the errno and errstr arguments will always be passed by reference.

Depending on the environment, the Unix domain or the optional connect timeout may not be available.

The socket will by default be opened in blocking mode. You can switch it to non-blocking mode by using stream_set_blocking().

打开网络的 Socket 链接。
语法: int fsockopen(string hostname, int port, int [errno], string [errstr], int [timeout]);
返回值: 整数
函数种类: 网络系统

内容说明目前这个函数提供二个 Socket 资料流界面,分别为 Internet 用的 AF_INET 及 Unix 用的 AF_UNIX。当在 Internet 情形下使用时,参数 hostname 及 port 分别代表网址及埠号。在 UNIX 情形可做 IPC,hostname 参数表示到 socket 的路径,port 配置为 0。可省略的 timeout 选项表示多久没有连上就中断。在使用本函数之后会返回文件指针,供文件函数使用,包括 fgets()、fgetss()、fputs()、fclose() 与 feof()。参数 errno 及 errstr 也是可省略的,主要当做错误处理使用。使用本函数,会使用搁置模式 (blocking mode) 处理,可用 set_socket_blocking() 转换成无搁置模式。

使用范例
本例用来模拟成HTTP连接。

<?php
$fp = fsockopen("www.wenan8.com", 80, &$errno, &$errstr, 10);
if(!$fp) {
        echo "$errstr ($errno)<br>\n";
} else {
        fputs($fp,"GET / HTTP/1.0\nHost: www.wenan8.com\n\n");
        while(!feof($fp)) {
                echo fgets($fp,128);
        }
        fclose($fp);
}
?>
分类: PHP 标签: ,

定时运行PHP页面

2010年10月28日 admin 没有评论

某个PHP页面需要在每天的某个时间定时运行。
其实就是应用windows的定时任务。
控制面板中任务计划–》添加任务计划–》 依次下一步找到php的安装目录php.exe 然后 高级设置里 -q 命令 运行某个页面。(如图)

分类: PHP 标签: , ,

PHP模拟浏览器重启路由器

2010年10月28日 admin 没有评论

项目中需要用到经常采集搜索引擎的数据,而百度,google等对短时间内大流量搜索做了限制,会限制IP(百度) 等,google的更是做了识别本机生成cookie(猜测) 的限制,模拟cookie都不行。

大多数采集器都做了自动重启IP的功能, 当采集页面匹配到做了限制的 正则时,执行重启IP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//$this->noData 为匹配限制的页面正则, $this->page 为页面内容,可用file_get_content()获取
 
if(preg_match($this->noData,$this->page)){
$this->reStartRouter();
 
}
 
//操作路由
 
function router($target=''")
 
{
 
$server  = '192.168.0.1';            // IP address
 
$host    = '192.168.0.1';            // Domain name
 
$port    = 80;
 
$referer = 'http://'.$host.$target;    // Referer
 
$username = "admin";#路由的用户名
 
$password = "admin";#路由的管理密码
 
$authorization = base64_encode($username.":".$password);
 
$File = fsockopen($server, $port, $errno, $errstr, 50);
 
if ($File)
 
{
 
$out = "GET $target HTTP/1.1\r\n";
 
$out .= "Host: $host\r\n";
 
$out .= "Referer: $referer\r\n";
 
$out .= "Authorization: Basic $authorization\r\n";
 
$out .= "Connection: Close\r\n\r\n";
 
fputs($File, $out);
 
$makeFile = $buffer = "";
 
while ($buffer = fread($File,4096))
 
{
 
$makeFile = $makeFile.$buffer;
 
}
 
fclose($File);
 
return $makeFile;
 
}
 
}
 
//断开
 
function disconnectr() {
 
$url = '/if.cgi?redirect=sys_status.htm&amp;failure=fail.htm&amp;type=wan_state_reset&amp;ifname=ppp0&amp;ifstatus=Up&amp;ifcmd=DISCONNECT';
 
router($url);
 
}
 
//连接
 
function connectr() {
 
$url = '/if.cgi?redirect=sys_status.htm&amp;failure=fail.htm&amp;type=wan_state_reset&amp;ifname=ppp0&amp;ifstatus=Down&amp;ifcmd=CONNECT';
 
router($url);
 
}
 
function reStartRouter()
{
$r=new router();
$r->disconnectr();//断开
$r->connectr();//连接
sleep(3);//静止3秒,一般要过一会才可以 连上
return true;
}
分类: PHP 标签: ,

Twitter新搜索架构将采用开源Lucene

2010年10月11日 admin 没有评论

除了重新设计了UI,在后端,Twitter也带来了非常巨大的变化。今天,Michael Busch 更新了Twitter Engineering Blog, 给我们带来了一些搜索方面改进的细节。

刚开始Twitter的实时搜索引擎是基于Summize技术,Summize是Twitter 于 2008 年收购的一家公司。但是从那以后,Twitter开始显著成长:每秒 1000 条推和 12000 个查询,一天下来要 10 亿查询,从那里起,Twitter 的工程师团队就开始寻找一个替代品了,因为旧式的基于MySQL的系统已经面临了巨大的挑战。

所以Twitter开始转向一个新的搜索架构,选择了开源的Lucene。

先不说Lucene的优势,首先它在实时搜索方面就有一些缺点,所以 Twitter 重写了它的部分架构,但是仍然支持 Lucene 的API。重写的部分包括:

* 改进无用数据收集的性能

* 无锁数据结构和算法

* 逆序遍历 posting 列表

* 有效的早期查询终止

Twitter的目标是,信息发布10秒内即可被搜索服务索引。Twitter称,该公司目前只利用了新搜索服务后台约5%的资源,每秒能够索引的Twitter消息数量可以达到当前的50倍。使工程师们”至少支持更大负载“的使命向前迈进了一步。

值得注意的是,就在一个月前,Google曾发布了Instant即时搜索功能,并向美国用户正式推出,当用户在谷歌搜索框中输入“W”时,谷歌便可以为其提供当地的天气预报。如果用户最终并未使用“weather”(天气),而是选择了其他关键词,搜索结果页面也会即时刷新,并进行相应的改变。

在Google的Instant即时搜索引擎中,部署了新的缓存系统,用以处理高速度的查询要求,同时连续性地抓取网络内容,并为这些内容重新建立索引。

对于即时搜索技术的未来,我们很难判定谁能代表未来,但不可否认的是,在面向社会化网络时,以Twitter的Lucene架构和Google的Instant技术将是代表未来搜索引擎的重要技术方向。

分类: PHP 标签: