You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
git-test/core/responseinitiative.lib.php

204 lines
7.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace LaneWeChat\Core;
class ResponseInitiative {
// 发送消息的URL基础部分
protected static $queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=';
// HTTP请求方法主动发送消息使用POST方法
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();
// 拼接完整的请求URL
self::$queryUrl .= $accessToken;
// 构造文本消息模板
$template = array(
'touser' => $tousername, // 接收者的OpenID
'msgtype' => 'text', // 消息类型为文本
'text' => array(
'content' => $content, // 文本消息内容
),
);
// 将模板数组转换为JSON格式
$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();
// 拼接完整的请求URL
self::$queryUrl .= $accessToken;
// 构造图片消息模板
$template = array(
'touser' => $tousername, // 接收者的OpenID
'msgtype' => 'image', // 消息类型为图片
'image' => array(
'media_id' => $mediaId, // 图片的媒体ID
),
);
// 将模板数组转换为JSON格式
$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();
// 拼接完整的请求URL
self::$queryUrl .= $accessToken;
// 构造语音消息模板
$template = array(
'touser' => $tousername, // 接收者的OpenID
'msgtype' => 'voice', // 消息类型为语音
'voice' => array(
'media_id' => $mediaId, // 语音的媒体ID
),
);
// 将模板数组转换为JSON格式
$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();
// 拼接完整的请求URL
self::$queryUrl .= $accessToken;
// 构造视频消息模板
$template = array(
'touser' => $tousername, // 接收者的OpenID
'msgtype' => 'video', // 消息类型为视频
'video' => array(
'media_id' => $mediaId, // 视频的媒体ID
'title' => $title, // 视频标题
'description' => $description, // 视频描述
),
);
// 将模板数组转换为JSON格式
$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();
// 拼接完整的请求URL
self::$queryUrl .= $accessToken;
// 构造音乐消息模板
$template = array(
'touser' => $tousername, // 接收者的OpenID
'msgtype' => 'music', // 消息类型为音乐
'music' => array(
'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);
}
/**
* 准备图文消息的单个项目
* @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();
// 拼接完整的请求URL
self::$queryUrl .= $accessToken;
// 构造图文消息模板
$template = array(
'touser' => $tousername, // 接收者的OpenID
'msgtype' => 'news', // 消息类型为图文
'news' => array(
'articles' => $item // 图文消息项目数组
),
);
// 将模板数组转换为JSON格式
$template = json_encode($template);
// 发送请求并返回结果
return Curl::callWebServer(self::$queryUrl, $template, self::$action);
}
}