1. 论坛系统升级为Xenforo,欢迎大家测试!
    排除公告

本人学php的第一个作品 -alexa查询缓存版

本帖由 小叶2006-04-16 发布。版面名称:后端开发

  1. 小叶

    小叶 New Member

    注册:
    2005-09-04
    帖子:
    17,941
    赞:
    33
  2. Picky

    Picky New Member

    注册:
    2005-09-04
    帖子:
    538
    赞:
    2
    代码:
    <?php
    /*
    作者:blue2004
    Date:2006-04-14
    演示:http://weblog.chinahtml.com/163/?url=pearphp.com
    QQ :80392625
    首发chinahtml,转贴和使用请保留这段信息
    */
    $hostname=$_GET['url'];
    if (!$hostname){
    	$hostname ="www.google.com";
    }
    $url= "http://www.alexa.com/data/details/?url=$hostname";
    
    $file =cached_fopen_url($url, "r", $timeout_seconds = 100, $cache_path = "./temp", $fsocket_timeout = 100);
    $files =explode("<span class=\"descBold\">",$file);
    $files1 =explode("</td><td class=\"paddingRight\" valign=\"top\">",$files[1]);
    $alexa =preg_replace ("'<[\/\!]*?[^<>]*?>'si", "", $files1[0]);
    
    echo $hostname.":".$alexa;
    
    function cached_fopen_url($url, $file_mode, $timeout_seconds = 90, $cache_path = "./temp", $fsocket_timeout = 100){ 
    $debug = false; 
    clearstatcache(); 
    $cache_filename=$cache_path . "/" . urlencode($url) .".cached"; 
    if ($debug) { 
    	print "local_cache creation_time =" . 
    	@filemtime($cache_filename) . 
    	" actual time = " . time() . 
    	" timeout = " . 
    	timeout_seconds ."<p>"; 
    } 
    if ( ( @file_exists($cache_filename ) and ( ( @filemtime($cache_filename) + $timeout_seconds) > ( time() ) ) ) ) { 
    
    if ($debug) { print "using cached file ($cache_filename) <p>";} 
    } else { 
    	if ($debug) { print "cacheing file ($url) to local ($cache_filename)<p>";} 
    
    	$urlParts = parse_url($url); 
    	$host = $urlParts['host']; 
    	$port = (isset($urlParts['port'])) ? $urlParts['port'] : 80; 
    
    if( !$fp = @fsockopen( $host, $port, $errno, $errstr, $fsocket_timeout )) { 
    } else { 
    
    if( !fputs( $fp, "GET $url HTTP/1.0\r\nHost:$host\r\n\r\n" )) { 
    	die( "unable to send get request" ); 
    } 
    $data = null; 
    stream_set_timeout($fp, $fsocket_timeout); 
    $status = socket_get_status($fp); 
    while( !feof($fp) && !$status['timed_out']) 
    { 
    	$data .= fgets ($fp,8192); 
    	$status = socket_get_status($fp); 
    } 
    fclose ($fp); 
    
    $sData = split("\r\n\r\n", $data, 2); 
    $data = $sData[1]; 
    
    $f2 = fopen($cache_filename,"w+"); 
    fwrite($f2,$data); 
    fclose($f2); 
    } 
    } 
    
    if ( @file_exists($cache_filename )) { 
    	$handle = fopen($cache_filename, $file_mode); 
    	while (!feof($handle))
    	{
    		$files .=fread($handle,8192);
    	}
    } 
    
    return $files;
    } 
    ?> 
    
     
  3. 相当

    相当 New Member

    注册:
    2006-03-28
    帖子:
    9,431
    赞:
    42
    嗯嗯不错 我XX你
     
  4. Theabc

    Theabc Member

    注册:
    2006-03-16
    帖子:
    673
    赞:
    1
    不错...真的很不错....确实真的很不错
     
  5. 小叶

    小叶 New Member

    注册:
    2005-09-04
    帖子:
    17,941
    赞:
    33
    如果直接按照普通的思路,直接file,fopen,都获取不到那页面的内容。。
     
  6. sluke

    sluke New Member

    注册:
    2005-09-04
    帖子:
    4,550
    赞:
    13
    夸奖一下
     
  7. swenge

    swenge New Member

    注册:
    2005-11-28
    帖子:
    386
    赞:
    1
    为何呢
     
  8. 蚊香

    蚊香 New Member

    注册:
    2005-11-08
    帖子:
    38
    赞:
    2
    支持小叶
     
  9. 那一年

    那一年 New Member

    注册:
    2005-09-05
    帖子:
    107
    赞:
    0
    相当,你是相当能灌~