近段时间由于工作需要,需要写个QQ通知的功能,仔细百度了一下,发现了现有的码,现分享大家。特别应该注意的是腾讯公司并未提供过QQ直接通讯的API接口,不过很庆幸的是咋们还有个3g qq可以小小利用下,3Gqq应采用session会话(也许是考虑到部分手机不支持COOKIE或其他功能),总之就是功能限制把。整个QQ会话以唯一会话SID为标识进行通讯,每次登录有效期为30天,说白了也就是大家平常用过的书签功能。不过有时平凡的登录或者太有规律的数据也会被腾讯公司给筛选出来,从而让SID失效,需重新登录,这的确很麻烦,博主也在认真研究...,现奉上整理出来的CODE

 <?php
//打开网址函数(无心问世出品)
function openu($url){
$url = eregi_replace('^http://', '', $url);
$temp = explode('/', $url);
$host = array_shift($temp);
$path = '/'.implode('/', $temp);
$temp = explode(':', $host);
$host = $temp[0];
$port = isset($temp[1]) ? $temp[1] : 80;
$fp = @fsockopen($host, $port, &$errno, &$errstr, 30);
if ($fp){
@fputs($fp, "GET $path HTTP/1.1\r\n");
@fputs($fp, "Host: $host\r\n");
@fputs($fp, "Accept: */*\r\n");
@fputs($fp, "Referer: http://$host/\r\n");
@fputs($fp, "User-Agent: TTMobile/09.03.18/symbianOS9.1 Series60/3.0 Nokia6120cAP3.03\r\n");
@fputs($fp, "Connection: Close\r\n\r\n");
}
$Content = '';
while ($str = @fread($fp, 4096))
$Content .= $str;
@fclose($fp);
return $Content;
}
//QQ登陆函数(需curl的支持)
function qq_login($qqno,$qqpw){
$cookie = dirname(__FILE__).'/cookie.txt';
$post = array(
'login_url' => 'http://pt.3g.qq.com/s?sid=ATAll43N7ZULRQ5V8zdfojol&amp;aid=nLogin',
'q_from' => '',
'loginTitle' => 'login',
'bid' => '0',
'qq' => $qqno,
'pwd' => $qqpw,
'loginType' => '1',
'loginsubmit' => 'login',
);
$curl = curl_init('http://pt.3g.qq.com/handleLogin?aid=nLoginHandle&amp;sid=ATAll43N7ZULRQ5V8zdfojol');
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie); // ?Cookie
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
global $result;
$result = curl_exec($curl);
curl_close($curl);
return $result;
} function get_sid($result){
preg_match_all("/sid=([^&=?]*)/",$result,$regs);
$sid=$regs[1][0];//sid
return $sid;
} //QQ验证码函数
function qq_yzm(){
$cookie = dirname(__FILE__).'/cookie.txt';
$post = array(
'auto'=>0,
'bid'=> 0,
);
$curl = curl_init('http://pt5.3g.qq.com/psw3gqqLogin?sid=AepMDkt5vrXH64LvijQHTfWd');
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie); // ?Cookie
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
global $result;
$result = curl_exec($curl);
curl_close($curl);
return $result;
} //时间函数
function time2string($second){
$day = floor($second/(3600*24));
$second = $second%(3600*24);//除去整天之后剩余的时间
$hour = floor($second/3600);
$second = $second%3600;//除去整小时之后剩余的时间
$minute = floor($second/60);
$second = $second%60;//除去整分钟之后剩余的时间
//返回字符串
return $day.'天'.$hour.'小时'.$minute.'分'.$second.'秒';
} //sid到期时间
function add_onlinetime(){
date_default_timezone_set('PRC');
$tomorrow = mktime(0,0,0,date("m"),date("d")+30,date("Y"));
$time=date("Y-m-d",$tomorrow);
$now=date("Y-m-d");
$deltime=date('G:i:s');
$time5= $time.' '.$deltime;
return $time5;
} function sendmsg($sid,$to_num,$msg){
$params = array();
$params["msg"] = $msg;
$params["u"] = $to_num;
$params["saveURL"] = 0;
$params["do"] = "send";
$params["on"] = 1;
$params["aid"] = "发送";
$url = "http://q16.3g.qq.com/g/s?sid=" . $sid;
$data = http_post($url, $params);
if(preg_match('/消息发送成功/',$data))
echo 'success<br />';
else 'failure';
} function getMsg($qq_num = 0, $sid = 0) {
$url = "http://q16.3g.qq.com/g/s?sid=" . $sid . "&3G_UIN=" . $qq_num ."&saveURL=0&aid=nqqChat";
$data = http_get($url);
preg_match("/name=\"u\" value=\"(\d+)\"/", $data, $matches);
$result["qq"] = $matches[1];
$data = explode("<form", $data);
$data = $data[0];
preg_match_all("/<p>(.+)?<\/p>/", $data, $matches);
unset($matches[1][0]);
$result["content"] = $matches[1];
return $result;
} function http_get($url,$header=0){
$opt = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => $header,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => 60,
CURLOPT_USERAGENT=>'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.29 Safari/525.13'
);
return curl_run($opt);
} function http_post($url,$data,$header=0){
$opt = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => $header,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => 60,
CURLOPT_POSTFIELDS => http_build_query($data),
CURLOPT_USERAGENT=>'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.29 Safari/525.13'
);
return curl_run($opt);
} function curl_run($opt){
$ch=curl_init();
curl_setopt_array($ch,$opt);
$r = curl_exec($ch);
curl_close($ch);
return $r;
}
?>

另外还有一个版本也顺便奉献给大家,原理大同小异

<?php
/*
*该类用于登录3gqq,可以作为3gqq网游戏辅助登录的基类,继承该类,可以添加具体游戏的处理方法,类还有待完善
*By 草木の灰(CrazyK)
*QQ:497920802
*email:netchen@vip.qq.com
*2013.5.4
*/
class qq{
/**
*为了继承时候可访问属性,故使用保护属性
*/
protected $m_sid;
protected $m_qq;
protected $m_cookie;
protected $m_error;
protected $m_cookie_file;
protected $m_yzm_post_data;
protected $m_yzm_post_url;
protected $m_yzm_url;
protected $m_http_header;
protected $m_login_status;//0没有登录3g,1登录3g
protected $m_chat_status;//0离线,1上线,2隐身,3离开
/**
* 构造函数
*
*/
function __construct($sid=null,$cookie_file=null){
$this->m_http_header=array(
'Cache-Control:max-age=0',
'User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.79 Safari/535.11 QIHU THEWORLD',
'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language:zh-CN,zh;q=0.8',
'Accept-Charset:GBK,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding:gzip,deflate,sdch'
);
if($sid==null||$cookie_file==null){
$this->m_cookie_file=tempnam('./cookietmp/','tmp');
$this->m_login_status=0;//没有登录3gqq
$this->m_chat_status=0;//没有进行3g聊天
}else{
$this->m_sid=$sid;
$this->m_cookie_file=$cookie_file;
$this->m_login_status=1;//没有登录3gqq
$this->m_chat_status=0;//没有进行3g聊天
}
}
/**
* 析构函数
* 用于测试时候删除临时文件
*/
/*
function __destruct()
{
if(file_exists($this->m_cookie_file))
{
unlink($this->m_cookie_file);
}
}
*/
/**
* 利用qq账号和密码登录
* mode为登录方式,3为不登陆qq聊天,2为隐身登录,1为在线登录
* 成功返回0,失败返回其他值
*/
function login($qq,$pwd,$mode='3'){
if($this->m_login_status===0){
$http_header=$this->m_http_header;
$this->m_qq=$qq;
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,"http://pt.3g.qq.com/");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HTTPHEADER,$http_header);
curl_setopt($ch,CURLOPT_ENCODING,'gzip');
$content=curl_exec($ch);
curl_close($ch);
$post_url=$this->substring($content,'action="','"');
$http_header[]='Origin:http://pt.3g.qq.com';
$http_header[]='Referer:http://pt.3g.qq.com/g/s?aid=nLogin&go_url=http://Fhouse60.3g.qq.com/g/my_home.jsp';
//array_push($http_header,'Content-Type: application/x-www-form-urlencoded');
$post_data='login_url=http%3A%2F%2Fpt.3g.qq.com%2Fs%3Faid%3DnLogin%26go_url%3Dhttp%3A%2F%2FFhouse60.3g.qq.com%2Fg%2Fmy_home.jsp&sidtype=1&nopre=0&q_from=&loginTitle=%E6%89%8B%E6%9C%BA%E8%85%BE%E8%AE%AF%E7%BD%91&bid=0&go_url=http%3A%2F%2FFhouse60.3g.qq.com%2Fg%2Fmy_home.jsp&qq='.$qq.'&pwd='.urlencode($pwd).'&loginType='.$mode.'&loginsubmit=%E7%99%BB%E5%BD%95';
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$post_url);
curl_setopt($ch,CURLOPT_COOKIEJAR,$this->m_cookie_file);
//curl_setopt($ch,CURLOPT_COOKIEFILE,)
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HTTPHEADER,$http_header);
curl_setopt($ch,CURLOPT_POST,1);
//curl_setopt($ch,CURLOPT_HEADER,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);
curl_setopt($ch,CURLOPT_ENCODING,'gzip');
$content=curl_exec($ch);
//echo $content;
curl_close($ch);
return $this->check_login($content);
}
$this->m_error='已登录';
return 3;
}
function substring($text,$strstart,$strend,$mode=''){
//preg_match('/action="(?P<post_url>.*?sid=(?P<sid>.{24}).*?)"/',$text,$m);
preg_match('/'.$strstart.'(.*?)'.$strend.'/'.$mode,$text,$m);
return $m[1];
}
/**
* 核对验证码
* $yzm验证码
* 成功返回0,失败返回其他值
*/
function check_yzm($yzm){
$this->m_yzm_post_data['verify']=$yzm;
$http_header=$this->m_http_header;
$ch=curl_init('http://pt.3g.qq.com'.$this->m_yzm_post_url);
curl_setopt_array($ch,array(CURLOPT_POST=>1,CURLOPT_FOLLOWLOCATION=>1,CURLOPT_HEADER=>1,CURLOPT_HTTPHEADER=>$http_header,CURLOPT_RETURNTRANSFER=>1,CURLOPT_COOKIEJAR=>$this->m_cookie_file,CURLOPT_COOKIEFILE=>$this->m_cookie_file,CURLOPT_POSTFIELDS=>$this->m_yzm_post_data,CURLOPT_ENCODING=>'gzip'));
$content=curl_exec($ch);
curl_close($ch);
return $this->check_login($content);
} function check_login($content)
{
if(strpos($content,"不正确"))
{
$this->m_error="用户名或密码不正确";
return 1;
}
else if(strpos($content,"验证码"))
{
$this->m_error="需要输入或验证码不正确";
preg_match('/<img src="(?P<yzm_url>.*?)" alt="验证码"\s?\/?>/si',$content,$m);
$this->m_yzm_url=$m['yzm_url'];//得到验证码地址
//echo $this->m_yzm_url;
$reg='/<form action="(?P<posturl>.*?)"/si';
preg_match_all($reg,$content,$m);
$this->m_yzm_post_url=$m['posturl'][1];//得到验证码表单发送地址
//var_dump($m);
//echo $this->m_yzm_post_url;
$this->m_yzm_post_data=$this->get_input_value($content,1);//得到验证码表单各个域的name=>value数组
return 2;
}
else
{
preg_match('/sid=(?P<sid>.{24})/',$content,$m);
$this->m_sid=$m['sid'];
$this->m_login_status=1;
return 0;
}
}
//获取表单中hidden表域的name和value值,成功返回以表单中name为key,value为值的数组,失败返回空数组
//text为需要查找的表单文本,如果有多个表单将使用index来提取对应表单
function get_input_value($text,$index=null)
{
if($index!=null)
{
$reg='/<form.*?\/form>/si';
preg_match_all($reg,$text,$m);
//var_dump($m);
$text=$m[0][$index];
//echo $text;
}
$reg='/<input type="hidden" name="(?P<name>.*?)" value="(?P<value>.*?)"\s?\/?>/si';
preg_match_all($reg,$text,$m);
//var_dump($m);
$ret=array();
if($m)
{
for($i=0;$i<count($m['name']);$i++)
{
$ret[$m['name'][$i]]=$m['value'][$i];
}
}
//var_dump($ret);
return $ret;
}
/*
* 退出登录
* 成功返回0,失败返回1
*/
function logout()
{
if($this->m_login_status===1)
{
$http_header=$this->m_http_header;
$ch=curl_init('http://pt.3g.qq.com/s?sid='.$this->m_sid.'&aid=nLogout');
curl_setopt_array($ch,array(CURLOPT_FOLLOWLOCATION=>1,CURLOPT_HTTPHEADER=>$http_header,CURLOPT_RETURNTRANSFER=>1,CURLOPT_COOKIEJAR=>$this->m_cookie_file,CURLOPT_COOKIEFILE=>$this->m_cookie_file,CURLOPT_ENCODING=>'gzip'));
$content=curl_exec($ch);
curl_close($ch);
unlink($this->m_cookie_file);
$this->m_login_status=0;
return 0;
}
else
{
$this->m_error="未登录";
return 1;
}
} /**
* 改变qq聊天状态,10上线,20离线,30离开,40隐身,默认为隐身
* 成功返回0,失败返回其他值,此函数暂时不可用
*/
function change_status($status='40')
{
if($this->m_login_status===1&&$this->m_chat_status!==0)
{
$post_url='http://q16.3g.qq.com/g/s?sid='.$this->m_sid;
$post_data=array('s'=>$status,'aid'=>'chgStatus');
$http_header=$this->m_http_header;
$http_header[]='Origin:http://q32.3g.qq.com';
$http_header[]='Referer:http://q32.3g.qq.com/g/s?sid='.$this->m_sid.'&aid=nqqStatus';
$ch=curl_init($post_url);
curl_setopt_array($ch,array(CURLOPT_POST=>1,CURLOPT_FOLLOWLOCATION=>1,CURLOPT_HTTPHEADER=>$http_header,CURLOPT_RETURNTRANSFER=>1,CURLOPT_COOKIEJAR=>$this->m_cookie_file,CURLOPT_COOKIEFILE=>$this->m_cookie_file,CURLOPT_ENCODING=>'gzip',CURLOPT_POSTFIELDS=>$post_data));
$content=curl_exec($ch);
curl_close($ch);
echo $content;
if(strpos($content,"成功"))
{
return 0;
}
}
return 1;
}
/**
* 登录3gqq,用于保持3gqq在线状态
* 成功返回0,失败返回其他值
*/
function chat_login()
{
if($this->m_login_status===1&&$this->m_chat_status>0)
{
$this->m_error="已登录聊天";
return 2;
}
if($this->m_login_status===0)
{
$this->m_error="还未登录3g网";
return 1;
}
if($this->m_login_status===1&&$this->m_chat_status===0)
{
$chat_url='http://pt.3g.qq.com/s?aid=nLogin3gqqbysid&3gqqsid='.$this->m_sid;
//$chat_url='http://q32.3g.qq.com/g/s?aid=nqqchatMain&sid='.$this->m_sid.'&myqq='.$this->m_qq;
$http_header=$this->m_http_header;
$ch=curl_init($chat_url);
curl_setopt_array($ch,array(CURLOPT_FOLLOWLOCATION=>1,CURLOPT_HTTPHEADER=>$http_header,CURLOPT_RETURNTRANSFER=>1,CURLOPT_COOKIEJAR=>$this->m_cookie_file,CURLOPT_COOKIEFILE=>$this->m_cookie_file,CURLOPT_ENCODING=>'gzip'));
$content=curl_exec($ch);
curl_close($ch);
$this->m_chat_status=2;//设置为已经登录状态
//echo $content;
if(strpos($content,"sid已经过期"))
{
$this->m_error="sid已经过期,请重新登录";
$this->m_login_status=0;
unlink($this->m_cookie_file);
return 3;
}
}
return 0;
}
}
//以下是测试
header("Content-Type","application/xhtml+xml; charset=UTF-8");
$a=new qq();
echo $a->login('280759843','****','1');
echo $a->chat_login();
$a->logout();
?>

一定得注意SID的有效期,祝大家好运。

如何用PHP开发机器人。的更多相关文章

  1. php怎么做网站?如何用PHP开发一个完整的网站?

    1.PHPer应具备的知识 (1)PHP知识: 熟练掌握基础函数,PHP语句(条件.循环),数组(排序.读取),函数(内部 构造),运算(数学 逻辑),面向对象(继承 接口 封装 多态静态属性)等. ...

  2. SNF软件开发机器人平台2018-发展升级履历-零编程时代

    一.SNF软件开发机器人产品白皮书 二.SNF开发机器人教程:链接:https://pan.baidu.com/s/1Qpomg11c_1b1NKY5P7e4Bw 密码:jwc3 三.SNF软件开发机 ...

  3. SNF快速开发平台成长史V4.5-Spring.Net.Framework-SNF软件开发机器人

    SNF快速开发平台成长史 SNF框架CS\BS 视频教程 https://pan.baidu.com/s/1dFegFKX SNF开发机器人教程:链接:https://pan.baidu.com/s/ ...

  4. SNF软件开发机器人2018最新更新内容

    SNF软件开发机器人从10月份到现在的更新升级情况如下: 1 表单 表单控件占多列时,宽度默认0,自适应宽度2 excel导出 部分excel导出方法移动到框架中,可通用获取3 生成代码 生成的代码, ...

  5. SNF软件开发机器人教程更新

    SNF开发机器人教程:链接:https://pan.baidu.com/s/1Qpomg11c_1b1NKY5P7e4Bw 密码:jwc3

  6. SNF软件开发机器人产品白皮书

    软件开发机器人 产品白皮书 使用说明书 模块名称:软件开发机器人 模块编号:12 项目负责人:王金斗 所属部门:技术中心 文档编制: 编制日期:2018-02-02 文档审核:王金斗 审核日期: 文档 ...

  7. SNF-软件开发机器人-免费-火爆登场-程序下载及实战配套教程免费发放

    软件开发机器人不辱使命的完成了在软件开发方面的方式方法,颠覆了传统开发,可零编程开发软件,也可二开更强大功能. 为了更好的了解和理解软件开发机器人我们以模拟用友u8系统部分供应链程序为例进行模拟. 联 ...

  8. 如何用node开发自己的cli工具

    如何用node开发自己的cli工具 灵感 写这个工具的灵感以及场景源于youtube的一次闲聊 github 地址 blog首发 使用场景 原本我们写博客展示shell,例如:安装运转docker,一 ...

  9. 如何用TypeScript开发微信小程序

    微信小程序来了!这个号称干掉传统app的玩意儿虽然目前处于内测阶段,不过目前在应用号的官方文档里已经放出了没有内测号也能使用的模拟器了. 工具和文档可以参考官方文档:https://mp.weixin ...

随机推荐

  1. linux下mysql的简单使用

    写这篇的主要目的是记录一点mysql的基本使用方法,当然sql查询语句本来就有不少东西,这里就不一一介绍,这个网址有详细的教程(http://www.sdau.edu.cn/support/mysq_ ...

  2. linux使用读写锁pthread_rwlock_t

    转自:http://blog.csdn.net/onlyou930/article/details/6755593 使用读写锁 配置读写锁的属性之后,即可初始化读写锁.以下函数用于初始化或销毁读写锁. ...

  3. BuildFilePath 及打开文件对话框

    也许以后就主要在这里发SOUI的介绍了. 贴一段文件相关的helper, 测试一下贴代码是不是方便. /** * Copyright (C) 2014-2050 * All rights reserv ...

  4. 为什么调用 FragmentPagerAdapter.notifyDataSetChanged() 并不能更新其 Fragment?

    在一个 Android 应用中,我使用 FragmentPagerAdapter 来 处理多 Fragment 页面的横向滑动.不过我碰到了一个问题,即当 Fragment 对应的数据集发生改变时,我 ...

  5. Liferay 6.2 改造系列之十:修改系统登录相关配置

    1.关闭自动登录功能: 在/portal-master/portal-impl/src/portal.properties文件中,有如下配置: # # Set this to true to allo ...

  6. cf429B dp递推

    Description Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to loo ...

  7. C++11 std::chrono库详解

    所谓的详解只不过是参考www.cplusplus.com的说明整理了一下,因为没发现别人有详细讲解. chrono是一个time library, 源于boost,现在已经是C++标准.话说今年似乎又 ...

  8. json解析不推荐使用eval

    推荐使用JSON.parse() 低版本浏览器(IE6/7)不支持此方法,可以通过网上下载json2.js,引入到文件中,此文件通过判断浏览器是否支持JSON.parse()方法,如果不支持,文件中编 ...

  9. 转:delphi 删除指定文件夹下所有文件

    function TFtpContentThd.DeleteDirectory(NowPath: string): Boolean; var search: TSearchRec; ret: inte ...

  10. 卸载Photoshop

    安装Photoshop后出现卸载不干净的问题,从而导致安装另一个photoshop版本时出现错误,一般情况下我们都是在控制面板的卸载程序里进行卸载的,但往往这样的卸载并不能清除注册表残留以及系统盘里的 ...