现在大部分获取昵称的接口都是用的QQ空间的,至于邮箱,直接在QQ号后面加上@qq.com就行了,今天分享一个WordPress网站评论时自动填写QQ邮箱与昵称教程。

先把下面的接口保存为PHP文件,放在你的网站中,确保能被访问,之所以写成接口是为了避免跨域请求。

<?php

header(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”content-Type:application/json; charset=utf-8\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”);

$type = @$_GET[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’type\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’] ? $_GET[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’type\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’] : \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’;

if(empty($type)){

exit;

}

if($type == \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”getqqnickname\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”){

$qq = isset($_GET[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’qq\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’]) ? addslashes(trim($_GET[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’qq\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’])) : \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’;

if(!empty($qq) && is_numeric($qq) && strlen($qq) > 4 && strlen($qq) < 13){

$qqnickname = file_get_contents(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’http://users.qzone.qq.com/fcg-bin/cgi_get_portrait.fcg?uins=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’.$qq);

if($qqnickname){

$qqnickname = mb_convert_encoding($qqnickname, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”UTF-8\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”GBK\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”);

$str=explode(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”,\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”,$qqnickname);

$s=str_replace(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’,\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’, $str[6]);

print_r(json_encode(array(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”nicek\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”=>$s)));

}

}

}

if($type == \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”getqqavatar\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”){

$qq = isset($_GET[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’qq\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’]) ? addslashes(trim($_GET[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’qq\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’])) : \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’;

if(!empty($qq) && is_numeric($qq) && strlen($qq) > 4 && strlen($qq) < 13){

$qqavatar = file_get_contents(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’http://ptlogin2.qq.com/getface?appid=1006102&imgtype=3&uin=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’.$qq);

if($qqavatar){

$str= str_replace(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”pt.setHeader\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”,\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”qqavatarCallBack\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”,$qqavatar);

echo substr($str,17,strlen($str)-18);

}

}

}

上面的接口中我只取了昵称这个数据,如果你需要头像链接也可以取出来,至于怎么取数据我就不多说了,方法多种多样,看自己喜好。需要注意的是头像也是QQ空间的头像。当然你也可以通过后面的接口取头像,这个就是QQ的头像。

然后在你的主题全局加载的js中写上下面的js代码。注意jquery选择器选择的HTML标签,根据你自己的改一下。

$(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”#comt-qq\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”).blur(function() {

var curValue = $(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”#comt-qq\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”).val();

if ($.trim(curValue) == \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”) {

//空值

} else {

$(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”#email\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”).val(curValue + \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”@qq.com\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”);

getQQnic(curValue);

}

});

function getQQnic(num) {

$.get(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”你的接口文件地址(就是上面的代码保存的文件).php?type=getqqnickname&qq=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\” + num, function(data) {

$(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”#author\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”).val(data.nicek);

});

}

只要没有语法问题,应该就可以看到效果了,填完QQ号之后,点击其它区域,取消掉输入QQ号的编辑框的焦点就会自动查询并填写相关资料。有的人需要把QQ号也存到评论信息里,不知道想干嘛,完全没有意义。当然要实现也很简单,使用wordpress的函数给评论增加一个字段,然后在评论提交处将评论数据中传来的QQ号一起提交就好了。

相关新闻

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

联系我们

联系我们

151-1101-0018

在线咨询:
邮件:30115776@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
关注微信
分享本页
返回顶部