进一步详细注释 responseinitiative.lib.php

src
mlg6veufz 2 months ago
parent 6e32234adf
commit 3dc34cea48

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