|
|
<?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
|
|
|
protected static $queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=';
|
|
|
|
|
|
// HTTP请求方法
|
|
|
protected static $action = 'POST';
|
|
|
|
|
|
/**
|
|
|
* 发送文本消息
|
|
|
* @param string $tousername 接收者的OpenID
|
|
|
* @param string $content 回复的消息内容(换行:在content中能够换行,微信客户端就支持换行显示)
|
|
|
* @return string 发送结果
|
|
|
*/
|
|
|
public static function text($tousername, $content) {
|
|
|
// 获取ACCESS_TOKEN
|
|
|
$accessToken = AccessToken::getAccessToken();
|
|
|
self::$queryUrl .= $accessToken;
|
|
|
|
|
|
// 构造消息模板
|
|
|
$template = array(
|
|
|
'touser' => $tousername,
|
|
|
'msgtype' => 'text',
|
|
|
'text' => array(
|
|
|
'content' => $content,
|
|
|
),
|
|
|
);
|
|
|
$template = json_encode($template);
|
|
|
|
|
|
// 发送请求
|
|
|
return Curl::callWebServer(self::$queryUrl, $template, self::$action);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送图片消息
|
|
|
* @param string $tousername 接收者的OpenID
|
|
|
* @param string $mediaId 通过上传多媒体文件,得到的id
|
|
|
* @return string 发送结果
|
|
|
*/
|
|
|
public static function image($tousername, $mediaId) {
|
|
|
// 获取ACCESS_TOKEN
|
|
|
$accessToken = AccessToken::getAccessToken();
|
|
|
self::$queryUrl .= $accessToken;
|
|
|
|
|
|
// 构造消息模板
|
|
|
$template = array(
|
|
|
'touser' => $tousername,
|
|
|
'msgtype' => 'image',
|
|
|
'image' => array(
|
|
|
'media_id' => $mediaId,
|
|
|
),
|
|
|
);
|
|
|
$template = json_encode($template);
|
|
|
|
|
|
// 发送请求
|
|
|
return Curl::callWebServer(self::$queryUrl, $template, self::$action);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送语音消息
|
|
|
* @param string $tousername 接收者的OpenID
|
|
|
* @param string $mediaId 通过上传多媒体文件,得到的id
|
|
|
* @return string 发送结果
|
|
|
*/
|
|
|
public static function voice($tousername, $mediaId) {
|
|
|
// 获取ACCESS_TOKEN
|
|
|
$accessToken = AccessToken::getAccessToken();
|
|
|
self::$queryUrl .= $accessToken;
|
|
|
|
|
|
// 构造消息模板
|
|
|
$template = array(
|
|
|
'touser' => $tousername,
|
|
|
'msgtype' => 'voice',
|
|
|
'voice' => array(
|
|
|
'media_id' => $mediaId,
|
|
|
),
|
|
|
);
|
|
|
$template = json_encode($template);
|
|
|
|
|
|
// 发送请求
|
|
|
return Curl::callWebServer(self::$queryUrl, $template, self::$action);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送视频消息
|
|
|
* @param string $tousername 接收者的OpenID
|
|
|
* @param string $mediaId 通过上传多媒体文件,得到的id
|
|
|
* @param string $title 标题
|
|
|
* @param string $description 描述
|
|
|
* @return string 发送结果
|
|
|
*/
|
|
|
public static function video($tousername, $mediaId, $title, $description) {
|
|
|
// 获取ACCESS_TOKEN
|
|
|
$accessToken = AccessToken::getAccessToken();
|
|
|
self::$queryUrl .= $accessToken;
|
|
|
|
|
|
// 构造消息模板
|
|
|
$template = array(
|
|
|
'touser' => $tousername,
|
|
|
'msgtype' => 'video',
|
|
|
'video' => array(
|
|
|
'media_id' => $mediaId,
|
|
|
'title' => $title,
|
|
|
'description' => $description,
|
|
|
),
|
|
|
);
|
|
|
$template = json_encode($template);
|
|
|
|
|
|
// 发送请求
|
|
|
return Curl::callWebServer(self::$queryUrl, $template, self::$action);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送音乐消息
|
|
|
* @param string $tousername 接收者的OpenID
|
|
|
* @param string $title 标题
|
|
|
* @param string $description 描述
|
|
|
* @param string $musicUrl 音乐链接
|
|
|
* @param string $hqMusicUrl 高质量音乐链接,WIFI环境优先使用该链接播放音乐
|
|
|
* @param string $thumbMediaId 缩略图的媒体id,通过上传多媒体文件,得到的id
|
|
|
* @return string 发送结果
|
|
|
*/
|
|
|
public static function music($tousername, $title, $description, $musicUrl, $hqMusicUrl, $thumbMediaId) {
|
|
|
// 获取ACCESS_TOKEN
|
|
|
$accessToken = AccessToken::getAccessToken();
|
|
|
self::$queryUrl .= $accessToken;
|
|
|
|
|
|
// 构造消息模板
|
|
|
$template = array(
|
|
|
'touser' => $tousername,
|
|
|
'msgtype' => 'music',
|
|
|
'music' => array(
|
|
|
'title' => $title,
|
|
|
'description' => $description,
|
|
|
'musicurl' => $musicUrl,
|
|
|
'hqmusicurl' => $hqMusicUrl,
|
|
|
'thumb_media_id' => $thumbMediaId,
|
|
|
),
|
|
|
);
|
|
|
$template = json_encode($template);
|
|
|
|
|
|
// 发送请求
|
|
|
return Curl::callWebServer(self::$queryUrl, $template, self::$action);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 准备图文消息的单个项目
|
|
|
* @param string $title 标题
|
|
|
* @param string $description 描述
|
|
|
* @param string $picUrl 图片链接,支持JPG、PNG格式,较好的效果为大图360*200,小图200*200
|
|
|
* @param string $url 点击图文消息跳转链接
|
|
|
* @return array 图文消息项目
|
|
|
*/
|
|
|
public static function newsItem($title, $description, $picUrl, $url) {
|
|
|
return array(
|
|
|
'title' => $title,
|
|
|
'description' => $description,
|
|
|
'url' => $url,
|
|
|
'picurl' => $picUrl,
|
|
|
);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送图文消息
|
|
|
* @param string $tousername 接收者的OpenID
|
|
|
* @param array $item 图文消息项目数组,每个项由self::newsItem()返回
|
|
|
* @return string 发送结果
|
|
|
*/
|
|
|
public static function news($tousername, $item) {
|
|
|
// 获取ACCESS_TOKEN
|
|
|
$accessToken = AccessToken::getAccessToken();
|
|
|
self::$queryUrl .= $accessToken;
|
|
|
|
|
|
// 构造消息模板
|
|
|
$template = array(
|
|
|
'touser' => $tousername,
|
|
|
'msgtype' => 'news',
|
|
|
'news' => array(
|
|
|
'articles' => $item
|
|
|
),
|
|
|
);
|
|
|
$template = json_encode($template);
|
|
|
|
|
|
// 发送请求
|
|
|
return Curl::callWebServer(self::$queryUrl, $template, self::$action);
|
|
|
}
|
|
|
} |