首页 > 编程技术 > php

php获取QQ头像并显示的例子

发布时间:2016-11-25 17:23

最近看到博客留言的头像有点别扭,因为游客的头像都是同一个头像,看着不是很舒服。虽然现在绝大多数的主题集成了Gavatar头像功能,先不说gavatar被墙的问题,我自己现在都没弄个gavatar头像。

因为我登陆了几次,连接速度巨慢,所以我就放弃了。当然留言插件也不胜枚举,比如现在比较火的多说,但对于没有注册多说的朋友,头像仍是个问题。对于多说的社交账号绑定,我测试多次,QQ,人人这些主流平台的绑定经常出错,而且有的朋友(像我)讨厌繁琐的授权。

鉴于此,我在想一个大众化的,比较简单的方法。我想到的是对于没有头像的朋友调用其QQ头像,因为QQ现在至少是人手一个,所以只需要留言时填写QQ号,然后调用其头像。这样一来就方便多了。

首先是获取QQ的头像了,这也是这个想法的第一步,即今天的主题:

第一种方法:

 代码如下 复制代码

<?php
$qq = 552452006;
echo '<img" width=100% src="'.'http://q1.qlogo.cn/g?b=qq&nk='.$qq.'&s=100&t='. time() .'">';
?>

第二种方法:

 代码如下 复制代码

<?php
$qq = 552452006;
 
$src = 'http://q1.qlogo.cn/g?b=qq&nk=' . $qq . '&s=100&t=' . time();
 
header('Content-type: image/png');
 
$res = imagecreatefromstring(file_get_contents($src));
 
imagepng($res);
 
imagedestroy($res);
?>

这两种方法的区别:

第一种方法的优点是可以输出头像的原图,如果你的头像是动态的gif,那么输出的也是动态图。缺点是速度比较慢,不适合作为调用头像的方法。

第二种方法的优点是相比第一种速度比较快,但只抓取头像的静态图,不会显示动态头像,比较适合作为调用头像的方法。

临时性需求,研究了一下天翼开发平台的东西,用来发送验证码还是不错的,但是每日限额不多,所以很鸡肋,但是保证100%到达 买的话还是蛮贵的,代码没有做任何优化处理,只是测试是否可以实现接口,用的同学记得完善代码,刚写完老大又说是鸡肋的东西,不用了,代码放在博客记录下
 代码如下 复制代码

<?php
//
date_default_timezone_set('PRC');
//获取access_token
$data = "app_id=x&app_secret=x&grant_type=client_credentials";
$ch = curl_init("https://oauth.api.189.cn/emp/oauth2/v2/access_token");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,1);//使用post提交数据
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);//设置 post提交的数据
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// 从证书中检查SSL加密算法是否存在

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$access_token = curl_exec($ch);
curl_close($ch);
$access_token = json_decode($access_token,true);
//获取短信信任码 www.111cn.net
$timestamp = date('Y-m-d H:i:s');

$param['app_id'] = "app_id=x";
$param['access_token'] = "access_token=".$access_token['access_token'];
$param['timestamp'] = "timestamp=".$timestamp;
ksort($param);
$plaintext = implode("&",$param);
$sign = rawurlencode(base64_encode(hash_hmac('sha1',$plaintext,app_secret,true)));
//echo $sign;exit;
$code = file_get_contents("http://api.189.cn/v2/dm/randcode/token?app_id=x&access_token={$access_token['access_token']}&timestamp=".$timestamp."&sign=".$sign);
$code = json_decode($code,true);
$code = $code['token'];
echo $code;
//下发验证码
unset($param,$plaintext,$sign);
$param['app_id'] = "app_id=x";
$param['access_token'] = "access_token=".$access_token['access_token'];
$param['token'] = "token=".$code;
$param['phone'] = "phone=15091421612";
$param['url'] = "url=http://wx.podapi.com/test.php";
$param['exp_time'] = "exp_time=2";
$param['timestamp'] = "timestamp=".$timestamp;
ksort($param);
$plaintext = implode("&",$param);
$sign = rawurlencode(base64_encode(hash_hmac('sha1',$plaintext,'xx',true)));

$data = "app_id=x&access_token={$access_token['access_token']}&token={$code}&phone=15091421612&url=http://wx.podapi.com/test.php&exp_time=2&timestamp={$timestamp}&sign=".$sign;
$ch = curl_init("http://api.189.cn/v2/dm/randcode/send");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,1);//使用post提交数据
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);//设置 post提交的数据
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// 从证书中检查SSL www.111cn.net 加密算法是否存在
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$state = curl_exec($ch);
//var_dump(curl_getinfo($ch));
curl_close($ch);
//echo $state;

在 php 获取图片尺寸的方法我们可以使用 getimagesize 获取图片尺寸的效率是很低的,首先需要获取整个的图片信息,然后再进行操作,下面的例子更科学算法更好,我们一起来看看吧。

下方法可以用于快速获取图片尺寸信息

1.获取JPEG格式图片的尺寸信息

 代码如下 复制代码

<?php
/*

* 获取JPEG格式图片的尺寸信息,并且不需要下载/读取整个图片。

* 经测试这个函数不是对所有JPEG格式的图片都有效。

* http://www.111cn.net

*/

// Retrieve JPEG width and height without downloading/reading entire image.

function getjpegsize($img_loc) {
    $handle = fopen($img_loc, "rb") or die("Invalid file stream.");
    $new_block = NULL;
    if(!feof($handle)) {
        $new_block = fread($handle, 32);
        $i = 0;
        if($new_block[$i]=="xFF" && $new_block[$i+1]=="xD8" && $new_block[$i+2]=="xFF" && $new_block[$i+3]=="xE0") {
            $i += 4;
            if($new_block[$i+2]=="x4A" && $new_block[$i+3]=="x46" && $new_block[$i+4]=="x49" && $new_block[$i+5]=="x46" && $new_block[$i+6]=="x00") {
                
// Read block size and skip ahead to begin cycling through blocks in search of SOF marker

                $block_size = unpack("H*", $new_block[$i] . $new_block[$i+1]);
                $block_size = hexdec($block_size[1]);
                while(!feof($handle)) {
                    $i += $block_size;
                    $new_block .= fread($handle, $block_size);
                    if($new_block[$i]=="xFF") {
                        
// New block detected, check for SOF marker

                        $sof_marker = array("xC0", "xC1", "xC2", "xC3", "xC5", "xC6", "xC7", "xC8", "xC9", "xCA", "xCB", "xCD", "xCE", "xCF");
                        if(in_array($new_block[$i+1], $sof_marker)) {
                            
// SOF marker detected. Width and height information is contained in bytes 4-7 after this byte.

                            $size_data = $new_block[$i+2] . $new_block[$i+3] . $new_block[$i+4] . $new_block[$i+5] . $new_block[$i+6] . $new_block[$i+7] . $new_block[$i+8];
                            $unpacked = unpack("H*", $size_data);
                            $unpacked = $unpacked[1];
                            $height = hexdec($unpacked[6] . $unpacked[7] . $unpacked[8] . $unpacked[9]);
                            $width = hexdec($unpacked[10] . $unpacked[11] . $unpacked[12] . $unpacked[13]);
                            return array($width, $height);
                        } else {
                            
// Skip block marker and read block size

                            $i += 2;
                            $block_size = unpack("H*", $new_block[$i] . $new_block[$i+1]);
                            $block_size = hexdec($block_size[1]);
                        }
                    } else {
                        return FALSE;
                    }
                }
            }
        }
    }
    return FALSE;
}
?>

2.

 代码如下 复制代码

$url='http://www.111cn.net /images/201203/08/1331189004_28093400.jpg';
$image_content = file_get_contents($url);
$image = imagecreatefromstring($image_content);
$width = imagesx($image);
$height = imagesy($image);
echo $width.'*'.$height."nr";   

在php中要实现用户登录我们一般都会到curl模拟登陆功能,下面我就基于php的curl来实现登录人人网哦,完整的例子希望对各位有帮助。

 代码如下 复制代码

<?php
$cookie_file = dirname(__FILE__)."/renren.cookie";

$login_url = 'http://passport.renren.com/PLogin.do';

$post_fields['email'] = '';//人人的帐号
$post_fields['password'] = '';//人人密码
$post_fields['origURL'] = 'http%3A%2F%2Fhome.renren.com%2FHome.do';
$post_fields['domain'] = 'renren.com';


$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
$content = curl_exec($ch);
curl_close($ch);
//匹配用户的ID
$send_url='http://www.renren.com/home';
$ch = curl_init($send_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
//获取用户id
$tmp = explode('/',$info['redirect_url']);
$uid = array_pop($tmp);
unset($tmp);

//$uid = "305115027";
//获取token和rtk

$send_url='http://www.renren.com/'.$uid;
$ch = curl_init($send_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
$tmp = curl_exec($ch);
curl_close($ch);
preg_match_all("/get_check:'(.*?)',get_check_x:'(.*?)',/is",$tmp,$arr);
$token = $arr[1][0];//1121558104
$rtk = $arr[2][0];//e9a9cb2

//发布信息
$poststr['content'] = "这就是一个测试而已!!!";
$poststr['withInfo'] = '{"wpath":[]}';
$poststr['hostid:'] = $uid;
$poststr['privacyParams'] = '{"sourceControl": 99}';
$poststr['requestToken'] = $token;
$poststr['_rtk'] = $rtk;
$poststr['channel'] = "renren";

$head = array(
   'Referer:http://shell.renren.com/ajaxproxy.htm',
   'X-Requested-With:XMLHttpRequest',
  );
$ch = curl_init("http://shell.renren.com/{$uid}/status");
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5');
curl_setopt($ch,CURLOPT_HTTPHEADER,$head);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $poststr);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
$content = curl_exec($ch);
curl_close($ch);
$data = json_decode($content,true);
if($data["code"] == "0"){
 echo "发布成功!";
}else{
 echo "shit !!!";
}

原文来自:http://www.mapenggang.com/

重复提交数据我们在应用中经常会碰到了,今天我给各位介绍利用session来防止用户不小心重复提交数据的一个例子


原理非常的简单:就是用session在表单页面记录下,然后提交页面判断,如果相等则视为成功,并清空session

例子

 代码如下 复制代码

<?php
//开启session
session_start();

//如果有提交标识
if(isset($_GET['action']) && $_GET['action'] === 'save'){

 //如果有session且跟传过来的值一样 www.111cn.net 才算提交
 if(isset($_SESSION['__open_auth']) && isset($_POST['auth']) && $_SESSION['__open_auth'] == $_POST['auth']){
  print_r($_POST);
  $_SESSION['__open_auth'] = null;//清空
 } else {

  //走起
  header("location: post.php");
 }
 exit();
}

//授权
$auth = $_SESSION['__open_auth'] = time();

?>
<!doctype html>
<html>
<head>
 <meta charset="UTF-8">
 <title>post</title>
</head>
<body>
 <form action="post.php?action=save" method="post">
  <ul>
   <li>
    <input type="hidden" name="auth" value="1395454119">
    <input type="text" name="userName">
   </li>
   <li>
    <input type="password" name="userpass">
   </li>
   <li>
    <input type="submit" value="走起">
   </li>
   <li>
    1395454119   </li>
  </ul>
 </form>
</body>
</html>

当然还有更多更好的办法在这就不介绍了,文章最下面你感兴趣的文章中有很多相关文章。

标签:[!--infotagslink--]

您可能感兴趣的文章: