10个超级有用值得收藏的PHP代码片段(2)

function page_rank($page, $type = 'alexa'){
    switch($type){
        case 'alexa':
            $url = 'http://alexa.com/siteinfo/';
            $handle = fopen($url.$page, 'r');
        break;
        case 'google':
            $url = 'http://google.com/search?client=navclient-auto&ch=6-1484155081&features=Rank&q=info:';
            $handle = fopen($url.'http://'.$page, 'r');
        break;
    }
    $content = stream_get_contents($handle);
    fclose($handle);
    $content = preg_replace("~(n|t|ss+)~",'', $content);
    switch($type){
        case 'alexa':
            if(preg_match('~<div><img.+?>(.+?) </div>~im',$content,$matches)){
                return $matches[2];
            }else{
                return FALSE;
            }
        break;
        case 'google':
            $rank = explode(':',$content);
            if($rank[2] != '')
                return $rank[2];
            else
                return FALSE;
        break;
        default:
            return FALSE;
        break;
    }
}
// Alexa Page Rank:
echo 'Alexa Rank: '.page_rank('techug.com');
echo '
';
// Google Page Rank
echo 'Google Rank: '.page_rank('techug.com', 'google');

五、强制下载文件

复制代码 代码如下:


$filename = $_GET['file']; //Get the fileid from the URL
// Query the file ID
$query = sprintf("SELECT * FROM tableName WHERE id = '%s'",mysql_real_escape_string($filename));
$sql = mysql_query($query);
if(mysql_num_rows($sql) > 0){
    $row = mysql_fetch_array($sql);
    // Set some headers
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header("Content-Disposition: attachment; filename=".basename($row['FileName']).";");
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize($row['FileName']));

@readfile($row['FileName']);
    exit(0);
}else{
    header("Location: /");
    exit;
}


六、通过Email显示用户的Gravatar头像

复制代码 代码如下:


 $gravatar_link = 'http://www.gravatar.com/avatar/' . md5($comment_author_email) . '?s=32';
  echo '<img src="' . $gravatar_link . '" />';


七、通过cURL获取RSS订阅数

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/afa5f4398a69bc12f2ac2c5cbd81ce8f.html