PHP code example of slince / smartqq

1. Go to this page and download the library: Download slince/smartqq library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

slince / smartqq example snippets


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

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

$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();

$handler = $smartQQ->getMessageHandler();

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