send() public method

发送客服消息(文本、图片、语音、视频、音乐、图文)
public send ( string $openid, array $msg ) : array(err,
$openid string
$msg array
return array(err,
// 用户需要自己定义获取和保存access_token的方法
$m = new Memcached();
$m->addServer('localhost', 11211);
// wechat模块 - 处理用户发送的消息和回复消息
$wechat = new Wechat(array('appId' => $appId, 'token' => $token, 'encodingAESKey' => $encodingAESKey));
// api模块 - 包含各种系统主动发起的功能
$api = new Api(array('appId' => $appId, 'appSecret' => $appSecret, 'get_access_token' => function () use($m) {
    // 用户需要自己实现access_token的返回
    return $m->get('access_token');
}, 'save_access_token' => function ($token) use($m) {
    // 用户需要自己实现access_token的保存
    $m->set('access_token', $token, 0);
}));
// 获取微信消息
$msg = $wechat->serve();
// 回复用户消息
$wechat->reply('hehhe!');
// 主动发送文本消息 - 简洁模式
$api->send($msg->FromUserName, 'heheh');
// 主动发送文本消息
$api->send($msg->FromUserName, array('type' => 'text', 'content' => 'hello world!', 'kf_account' => 'test1@kftest'));
// 主动发送图片消息
$api->send($msg->FromUserName, array('type' => 'image', 'media_id' => 'Uq7OczuEGEyUu--dYjg7seTm-EJTa0Zj7UDP9zUGNkVpjcEHhl7tU2Mv8mFRiLKC', 'kf_account' => 'test1@kftest'));
// 主动发送语音消息
$api->send($msg->FromUserName, array('type' => 'voice', 'media_id' => 'rVT43tfDwjh4p1BV2gJ5D7Zl2BswChO5L_llmlphLaTPytcGcguBAEJ1qK4cg4r_', 'kf_account' => 'test1@kftest'));
// 主动发送视频消息
$api->send($msg->FromUserName, array('type' => 'video', 'media_id' => 'yV0l71NL0wtpRA8OMX0-dBRQsMVyt3fspPUzurIS3psi6eWOrb_WlEeO39jasoZ8', 'thumb_media_id' => '7ct_DvuwZXIO9e9qbIf2ThkonUX_FzLAoqBrK-jzUboTYJX0ngOhbz6loS-wDvyZ', 'title' => '视频消息的标题', 'description' => '视频消息的描述', 'kf_account' => 'test1@kftest'));
// 主动发送音乐消息
$api->send($msg->FromUserName, array('type' => 'music', 'title' => '音乐标题', 'description' => '音乐描述', 'music_url' => 'http://me.diary8.com/data/music/2.mp3', 'hqmusic_url' => 'http://me.diary8.com/data/music/2.mp3', 'thumb_media_id' => 'O39wW0ZsXCb5VhFoCgibQs5PupFb6VZ2jH5A8gHUJCJz2Qmkrb7objoTue7bGTGQ', 'kf_account' => 'test1@kftest'));
// 主动发送图文消息
$api->send($msg->FromUserName, array('type' => 'news', 'articles' => array(array('title' => '图文消息标题1', 'description' => '图文消息描述1', 'picurl' => 'http://me.diary8.com/data/img/demo1.jpg', 'url' => 'http://www.example.com/'), array('title' => '图文消息标题2', 'description' => '图文消息描述2', 'picurl' => 'http://me.diary8.com/data/img/demo2.jpg', 'url' => 'http://www.example.com/'), array('title' => '图文消息标题3', 'description' => '图文消息描述3', 'picurl' => 'http://me.diary8.com/data/img/demo3.jpg', 'url' => 'http://www.example.com/')), 'kf_account' => 'test1@kftest'));
Beispiel #2
0
    exit;
}
// 用户回复4 - 回复视频消息
if ($msg->MsgType == 'text' && $msg->Content == '4') {
    $wechat->reply(array('type' => 'video', 'media_id' => 'yV0l71NL0wtpRA8OMX0-dBRQsMVyt3fspPUzurIS3psi6eWOrb_WlEeO39jasoZ8', 'title' => '视频消息的标题', 'description' => '视频消息的描述'));
    exit;
}
// 用户回复5 - 回复音乐消息
if ($msg->MsgType == 'text' && $msg->Content == '5') {
    $wechat->reply(array('type' => 'music', 'title' => '音乐标题', 'description' => '音乐描述', 'music_url' => 'http://me.diary8.com/data/music/2.mp3', 'hqmusic_url' => 'http://me.diary8.com/data/music/2.mp3', 'thumb_media_id' => 'O39wW0ZsXCb5VhFoCgibQs5PupFb6VZ2jH5A8gHUJCJz2Qmkrb7objoTue7bGTGQ'));
    exit;
}
// 用户回复6 - 回复图文消息
if ($msg->MsgType == 'text' && $msg->Content == '6') {
    $wechat->reply(array('type' => 'news', 'articles' => array(array('title' => '图文消息标题1', 'description' => '图文消息描述1', 'picurl' => 'http://me.diary8.com/data/img/demo1.jpg', 'url' => 'http://www.example.com/'), array('title' => '图文消息标题2', 'description' => '图文消息描述2', 'picurl' => 'http://me.diary8.com/data/img/demo2.jpg', 'url' => 'http://www.example.com/'), array('title' => '图文消息标题3', 'description' => '图文消息描述3', 'picurl' => 'http://me.diary8.com/data/img/demo3.jpg', 'url' => 'http://www.example.com/'))));
    exit;
}
// 用户回复7 - 发送主动消息
if ($msg->MsgType == 'text' && $msg->Content == '7') {
    // 被动回复
    $wechat->reply("这是被动回复的一条消息");
    // 主动发送
    $api->send($msg->FromUserName, '这是主动发送的一条消息');
    // 主动发送
    $api->send($msg->FromUserName, array('type' => 'text', 'content' => '这是一个客服主动发送的一条消息!', 'kf_account' => 'test1@kftest'));
    // 主动发送
    $api->send($msg->FromUserName, '您的openid是: ' . $msg->FromUserName);
    exit;
}
// 默认回复默认信息
$wechat->reply($default_msg);