slince/smartqq

The wrapper for smartqq api using php

2.2.0 2018-09-27 07:08 UTC

This package is auto-updated.

Last update: 2024-12-17 18:11:40 UTC


README

Build Status Coverage Status Latest Stable Version Scrutinizer

SmartQQ(WebQQ) API的PHP实现,通过对原生web api的请求以及返回值的分析,重新进行了整理; 解决了原生接口杂乱的请求规则与混乱的数据返回;使得开发者可以更多关注自己的业务。

灵感来自于Java SmartQQ,感谢原作者对SmartQQ的详尽解释。

安装

composer require slince/smartqq

使用

登录

登录是获取授权的必备步骤,由于SmartQQ抛弃了用户名密码的登录方式,所以只能采用二维码登录

$smartQQ = new Slince\SmartQQ\Client();

$smartQQ->login(function($qrcode){ //$qrcode 是二维码字符串
    // 自定义逻辑
    @file_put_contents('/path/to/qrcode.png', $qrcode);
});

如果成功的话你会在/path/to/qrcode.png下发现二维码,使用手机扫描即可登录; 注意:程序会阻塞直到确认成功;成功之后你可以通过下面方式持久化登录凭证,用于下次查询。

$credential = $smartQQ->getCredential();

//将凭证对象转换为标量方便存储,写入数据库或者缓存系统
$credentialParameters = $credential->toArray();

通过下面方式还原一个凭证对象;需要注意的是此次凭证并不会长久有效, 如果该凭证长时间没有被用来发起查询,则很可能会失效。

//还原凭证对象
$credential = Credential::fromArray($credentialParameters);
$smartQQ = new Client($credential);

查询好友、群以及讨论组

好友相关

  • 查询所有好友
$friends = $smartQQ->getFriends();

//找出昵称为张三的好友
$zhangSan = $friends->firstByAttribute('nick', '张三');

//自定义筛选,如找出所有女性并且是vip会员的好友
$girls = $friends->filter(function(Friend $friend){
     return $friend->getGender() == 'female' && $friend->isVip();
})->toArray();
  • 查询好友详细资料
//上例,找出张三的资料
$profile = $smartQQ->getFriendDetail($zhangSan);

群相关

  • 查询所有群
$groups = $smartQQ->getGroups();

//找出名称为“少年”的群
$shaoNianGroup = $groups->firstByAttribute('name', '少年');

同样支持自定义筛选

讨论组相关

  • 查询所有讨论组
$discusses = $smartQQ->getDiscusses();

//找出名称为“少年”的讨论组
$shaoNianDiscuss = $discusses->firstByAttribute('name', '少年');

一样,也支持自定义筛选

  • 查询讨论组的详细资料,比如群成员信息等

接上例,查询讨论组“少年”的详细资料

$shaoNianDetail = $smartQQ->getDiscussDetail($shaoNianDiscuss);

//所有群成员,支持自定义筛选
$members = $shaoNianDetail->getMembers();

发送消息

支持表情,表情短语

[
"微笑" ,"撇嘴" ,"" ,"发呆" ,"得意" ,"流泪" ,"害羞" ,"闭嘴" ,"" ,"大哭" ,"尴尬" ,"发怒" ,"调皮" ,"呲牙" ,"惊讶" ,"难过" ,"" ,"冷汗" ,"抓狂" ,"",
"偷笑" ,"可爱" ,"白眼" ,"傲慢" ,"饥饿" ,"" ,"惊恐" ,"流汗" ,"憨笑" ,"大兵" ,"奋斗" ,"咒骂" ,"疑问" ,"" ,"" ,"折磨" ,"" ,"骷髅" ,"敲打" ,"再见",
"擦汗" ,"抠鼻" ,"鼓掌" ,"糗大了" ,"坏笑" ,"左哼哼" ,"右哼哼" ,"哈欠" ,"鄙视" ,"委屈" ,"快哭了" ,"阴险" ,"亲亲" ,"" ,"可怜" ,"菜刀" ,"西瓜" ,"啤酒" ,"篮球" ,"乒乓",
"咖啡" ,"" ,"猪头" ,"玫瑰" ,"凋谢" ,"示爱" ,"爱心" ,"心碎" ,"蛋糕" ,"闪电" ,"炸弹" ,"" ,"足球" ,"瓢虫" ,"便便" ,"月亮" ,"太阳" ,"礼物" ,"拥抱" ,"",
"" ,"握手" ,"胜利" ,"抱拳" ,"勾引" ,"拳头" ,"差劲" ,"爱你" ,"NO" ,"OK" ,"爱情" ,"飞吻" ,"跳跳" ,"发抖" ,"怄火" ,"转圈" ,"磕头" ,"回头" ,"跳绳" ,"挥手",
"激动", "街舞", "献吻", "左太极", "右太极", "双喜", "鞭炮", "灯笼", "发财", "K歌", "购物", "邮件", "", "喝彩","祈祷","爆筋","棒棒糖","喝奶","下面","香蕉",
"飞机","开车","左车头","车厢","右车头","多云","下雨","钞票","熊猫","灯泡","风车","闹钟","打伞","彩球","钻戒","沙发","纸巾","","手枪","青蛙"
]

给好友发送消息

//1、找到好友
$friend = $friends->firstByAttribute('nick', '秋易');

//2、生成消息
$message = new FriendMessage($friend, '[微笑] 你好');
$result = $smartQQ->sendMessage($message);
var_dump($result);

给群发送消息

//1、找到群
$group = $groups->firstByAttribute('name', 'msu');
//2、生成消息
$message = new GroupMessage($group, new Content('[微笑] 哈喽'));
$result = $smartQQ->sendMessage($message);
var_dump($result);

发送讨论组消息

//1、找到讨论组
$discuss = $discusses->firstByAttribute('name', '他是个少年');
//2、生成消息
$message = new DiscussMessage($discuss, '[微笑] 讨论组消息');
$result = $smartQQ->sendMessage($message);
var_dump($result);

给讨论组成员发消息

$discussMember = $smartQQ->getDiscussDetail($discuss)
    ->getMembers()
    ->firstByAttribute('nick', '张三');
$message = new FriendMessage($discussMember,  '[微笑] 你好');
$result = $smartQQ->sendMessage($message);
var_dump($result);

接收消息

$messages = $smartQQ->pollMessages();

关于消息的处理请参照 examples.

当然你也可以使用 MessageHandler 来帮忙维护客户端消息。

$handler = $smartQQ->getMessageHandler();

$handler->onMessage(function(Slince\SmartQQ\Message\Response\Message $message) use ($hander){
    var_dump($message);
    $handler->stop(); //停止消息轮询
});

详细使用案例以及更多其它案例请参考examples

其它

  • 关于103错误,多是由于webqq多点登录引起的,如果遇到错误,先到http://w.qq.com/确认能够收发消息,然后退出登录。

  • 关于登录凭证的时效性,smartqq是基于web接口的,对cookie有要求,登录成功之后如果长时间没有操作,cookie将会失效;此时需要重新登录。

  • 本组件只是对原生的请求与数据进行了整理并进行了合理的抽象,并没有过多的进行业务层级的封装。

  • SmartQQ接口一直在调整,如果api无法使用请直接提issue,或者给我发邮件;如果你找到了原因并且有了解决方案欢迎 PR。