|
|
|
@ -1,21 +1,13 @@
|
|
|
|
|
<?php
|
|
|
|
|
namespace LaneWeChat\Core;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送主动响应类
|
|
|
|
|
* Created by Lane.
|
|
|
|
|
* User: lane
|
|
|
|
|
* Date: 2013-12-29
|
|
|
|
|
* Time: 下午5:54
|
|
|
|
|
* Mail: lixuan868686@163.com
|
|
|
|
|
* Website: http://www.lanecn.com
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
class ResponseInitiative {
|
|
|
|
|
|
|
|
|
|
// 发送消息的URL
|
|
|
|
|
// 发送消息的URL基础部分
|
|
|
|
|
protected static $queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=';
|
|
|
|
|
|
|
|
|
|
// HTTP请求方法
|
|
|
|
|
// HTTP请求方法,主动发送消息使用POST方法
|
|
|
|
|
protected static $action = 'POST';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -27,19 +19,21 @@ class ResponseInitiative {
|
|
|
|
|
public static function text($tousername, $content) {
|
|
|
|
|
// 获取ACCESS_TOKEN
|
|
|
|
|
$accessToken = AccessToken::getAccessToken();
|
|
|
|
|
// 拼接完整的请求URL
|
|
|
|
|
self::$queryUrl .= $accessToken;
|
|
|
|
|
|
|
|
|
|
// 构造消息模板
|
|
|
|
|
// 构造文本消息模板
|
|
|
|
|
$template = array(
|
|
|
|
|
'touser' => $tousername,
|
|
|
|
|
'msgtype' => 'text',
|
|
|
|
|
'touser' => $tousername, // 接收者的OpenID
|
|
|
|
|
'msgtype' => 'text', // 消息类型为文本
|
|
|
|
|
'text' => array(
|
|
|
|
|
'content' => $content,
|
|
|
|
|
'content' => $content, // 文本消息内容
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
// 将模板数组转换为JSON格式
|
|
|
|
|
$template = json_encode($template);
|
|
|
|
|
|
|
|
|
|
// 发送请求
|
|
|
|
|
// 发送请求并返回结果
|
|
|
|
|
return Curl::callWebServer(self::$queryUrl, $template, self::$action);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -52,19 +46,21 @@ class ResponseInitiative {
|
|
|
|
|
public static function image($tousername, $mediaId) {
|
|
|
|
|
// 获取ACCESS_TOKEN
|
|
|
|
|
$accessToken = AccessToken::getAccessToken();
|
|
|
|
|
// 拼接完整的请求URL
|
|
|
|
|
self::$queryUrl .= $accessToken;
|
|
|
|
|
|
|
|
|
|
// 构造消息模板
|
|
|
|
|
// 构造图片消息模板
|
|
|
|
|
$template = array(
|
|
|
|
|
'touser' => $tousername,
|
|
|
|
|
'msgtype' => 'image',
|
|
|
|
|
'touser' => $tousername, // 接收者的OpenID
|
|
|
|
|
'msgtype' => 'image', // 消息类型为图片
|
|
|
|
|
'image' => array(
|
|
|
|
|
'media_id' => $mediaId,
|
|
|
|
|
'media_id' => $mediaId, // 图片的媒体ID
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
// 将模板数组转换为JSON格式
|
|
|
|
|
$template = json_encode($template);
|
|
|
|
|
|
|
|
|
|
// 发送请求
|
|
|
|
|
// 发送请求并返回结果
|
|
|
|
|
return Curl::callWebServer(self::$queryUrl, $template, self::$action);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -77,19 +73,21 @@ class ResponseInitiative {
|
|
|
|
|
public static function voice($tousername, $mediaId) {
|
|
|
|
|
// 获取ACCESS_TOKEN
|
|
|
|
|
$accessToken = AccessToken::getAccessToken();
|
|
|
|
|
// 拼接完整的请求URL
|
|
|
|
|
self::$queryUrl .= $accessToken;
|
|
|
|
|
|
|
|
|
|
// 构造消息模板
|
|
|
|
|
// 构造语音消息模板
|
|
|
|
|
$template = array(
|
|
|
|
|
'touser' => $tousername,
|
|
|
|
|
'msgtype' => 'voice',
|
|
|
|
|
'touser' => $tousername, // 接收者的OpenID
|
|
|
|
|
'msgtype' => 'voice', // 消息类型为语音
|
|
|
|
|
'voice' => array(
|
|
|
|
|
'media_id' => $mediaId,
|
|
|
|
|
'media_id' => $mediaId, // 语音的媒体ID
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
// 将模板数组转换为JSON格式
|
|
|
|
|
$template = json_encode($template);
|
|
|
|
|
|
|
|
|
|
// 发送请求
|
|
|
|
|
// 发送请求并返回结果
|
|
|
|
|
return Curl::callWebServer(self::$queryUrl, $template, self::$action);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -104,21 +102,23 @@ class ResponseInitiative {
|
|
|
|
|
public static function video($tousername, $mediaId, $title, $description) {
|
|
|
|
|
// 获取ACCESS_TOKEN
|
|
|
|
|
$accessToken = AccessToken::getAccessToken();
|
|
|
|
|
// 拼接完整的请求URL
|
|
|
|
|
self::$queryUrl .= $accessToken;
|
|
|
|
|
|
|
|
|
|
// 构造消息模板
|
|
|
|
|
// 构造视频消息模板
|
|
|
|
|
$template = array(
|
|
|
|
|
'touser' => $tousername,
|
|
|
|
|
'msgtype' => 'video',
|
|
|
|
|
'touser' => $tousername, // 接收者的OpenID
|
|
|
|
|
'msgtype' => 'video', // 消息类型为视频
|
|
|
|
|
'video' => array(
|
|
|
|
|
'media_id' => $mediaId,
|
|
|
|
|
'title' => $title,
|
|
|
|
|
'description' => $description,
|
|
|
|
|
'media_id' => $mediaId, // 视频的媒体ID
|
|
|
|
|
'title' => $title, // 视频标题
|
|
|
|
|
'description' => $description, // 视频描述
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
// 将模板数组转换为JSON格式
|
|
|
|
|
$template = json_encode($template);
|
|
|
|
|
|
|
|
|
|
// 发送请求
|
|
|
|
|
// 发送请求并返回结果
|
|
|
|
|
return Curl::callWebServer(self::$queryUrl, $template, self::$action);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -135,23 +135,25 @@ class ResponseInitiative {
|
|
|
|
|
public static function music($tousername, $title, $description, $musicUrl, $hqMusicUrl, $thumbMediaId) {
|
|
|
|
|
// 获取ACCESS_TOKEN
|
|
|
|
|
$accessToken = AccessToken::getAccessToken();
|
|
|
|
|
// 拼接完整的请求URL
|
|
|
|
|
self::$queryUrl .= $accessToken;
|
|
|
|
|
|
|
|
|
|
// 构造消息模板
|
|
|
|
|
// 构造音乐消息模板
|
|
|
|
|
$template = array(
|
|
|
|
|
'touser' => $tousername,
|
|
|
|
|
'msgtype' => 'music',
|
|
|
|
|
'touser' => $tousername, // 接收者的OpenID
|
|
|
|
|
'msgtype' => 'music', // 消息类型为音乐
|
|
|
|
|
'music' => array(
|
|
|
|
|
'title' => $title,
|
|
|
|
|
'description' => $description,
|
|
|
|
|
'musicurl' => $musicUrl,
|
|
|
|
|
'hqmusicurl' => $hqMusicUrl,
|
|
|
|
|
'thumb_media_id' => $thumbMediaId,
|
|
|
|
|
'title' => $title, // 音乐标题
|
|
|
|
|
'description' => $description, // 音乐描述
|
|
|
|
|
'musicurl' => $musicUrl, // 音乐链接
|
|
|
|
|
'hqmusicurl' => $hqMusicUrl, // 高质量音乐链接
|
|
|
|
|
'thumb_media_id' => $thumbMediaId, // 缩略图的媒体ID
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
// 将模板数组转换为JSON格式
|
|
|
|
|
$template = json_encode($template);
|
|
|
|
|
|
|
|
|
|
// 发送请求
|
|
|
|
|
// 发送请求并返回结果
|
|
|
|
|
return Curl::callWebServer(self::$queryUrl, $template, self::$action);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -164,11 +166,12 @@ class ResponseInitiative {
|
|
|
|
|
* @return array 图文消息项目
|
|
|
|
|
*/
|
|
|
|
|
public static function newsItem($title, $description, $picUrl, $url) {
|
|
|
|
|
// 返回图文消息的单个项目数组
|
|
|
|
|
return array(
|
|
|
|
|
'title' => $title,
|
|
|
|
|
'description' => $description,
|
|
|
|
|
'url' => $url,
|
|
|
|
|
'picurl' => $picUrl,
|
|
|
|
|
'title' => $title, // 标题
|
|
|
|
|
'description' => $description, // 描述
|
|
|
|
|
'url' => $url, // 点击图文消息跳转链接
|
|
|
|
|
'picurl' => $picUrl, // 图片链接
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -181,19 +184,21 @@ class ResponseInitiative {
|
|
|
|
|
public static function news($tousername, $item) {
|
|
|
|
|
// 获取ACCESS_TOKEN
|
|
|
|
|
$accessToken = AccessToken::getAccessToken();
|
|
|
|
|
// 拼接完整的请求URL
|
|
|
|
|
self::$queryUrl .= $accessToken;
|
|
|
|
|
|
|
|
|
|
// 构造消息模板
|
|
|
|
|
// 构造图文消息模板
|
|
|
|
|
$template = array(
|
|
|
|
|
'touser' => $tousername,
|
|
|
|
|
'msgtype' => 'news',
|
|
|
|
|
'touser' => $tousername, // 接收者的OpenID
|
|
|
|
|
'msgtype' => 'news', // 消息类型为图文
|
|
|
|
|
'news' => array(
|
|
|
|
|
'articles' => $item
|
|
|
|
|
'articles' => $item // 图文消息项目数组
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
// 将模板数组转换为JSON格式
|
|
|
|
|
$template = json_encode($template);
|
|
|
|
|
|
|
|
|
|
// 发送请求
|
|
|
|
|
// 发送请求并返回结果
|
|
|
|
|
return Curl::callWebServer(self::$queryUrl, $template, self::$action);
|
|
|
|
|
}
|
|
|
|
|
}
|