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.

83 lines
4.7 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 AutoReply{
/**
* 获取自动回复规则
* 这个函数用于从微信公众号的API获取当前的自动回复规则。
*
* @param int $industryId1 主行业ID
* @param int $industryId2 副行业ID
* @return String 返回结果与字段说明请查看微信公众平台文档
* http://mp.weixin.qq.com/wiki/7/7b5789bb1262fb866d01b4b40b0efecb.html
*/
public static function getRole($industryId1, $industryId2){
// 构建请求URL这里需要替换'access_token'为实际的access_token值
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/get_current_autoreply_info?access_token=' . AccessToken::getAccessToken();
// 定义请求方法为POST
$queryAction = 'POST';
// 初始化模板数组
$template = array();
// 设置主行业ID
$template['industry_id1'] = "$industryId1";
// 设置副行业ID
$template['industry_id2'] = "$industryId2";
// 将模板数组转换为JSON格式
$template = json_encode($template);
// 调用Curl类的方法发送请求并获取结果
return Curl::callWebServer($queryUrl, $template, $queryAction);
}
}
类声明class AutoReply 定义了一个名为 AutoReply 的类,用于处理微信公众号的自动回复功能。
方法声明public static function getRole($industryId1, $industryId2) 定义了一个静态方法 getRole用于获取自动回复规则。该方法接受两个参数主行业ID ($industryId1) 和副行业ID ($industryId2)
构建请求URL$queryUrl = 'https://api.weixin.qq.com/cgi-bin/get_current_autoreply_info?access_token=' . AccessToken::getAccessToken(); 构建请求微信API的URL使用 AccessToken::getAccessToken() 方法获取 access_token 并拼接到URL中。
定义请求方法:$queryAction = 'POST'; 设置请求方法为POST。
初始化模板数组:$template = array(); 创建一个空数组用于存储请求数据。
设置主行业ID$template['industry_id1'] = "$industryId1"; 将主行业ID添加到模板数组中。
设置副行业ID$template['industry_id2'] = "$industryId2"; 将副行业ID添加到模板数组中。
转换为JSON格式$template = json_encode($template); 将模板数组转换为JSON字符串以便发送给微信API。
发送请求并获取结果return Curl::callWebServer($queryUrl, $template, $queryAction); 使用 Curl::callWebServer 方法发送POST请求到微信API并返回结果。
添加与补充总结
以下是 AutoReply 类的详细总结:
类声明
类名AutoReply
命名空间LaneWeChat\Core
功能:该类用于处理微信公众号的自动回复功能,提供方法来获取和设置自动回复规则。
方法声明
方法名getRole
访问修饰符public static
功能用于从微信公众号的API获取当前的自动回复规则。
参数:
$industryId1主行业ID用于指定公众号的主要行业分类。
$industryId2副行业ID用于指定公众号的次要行业分类。
返回值:返回一个字符串,包含获取自动回复规则的结果与字段说明。具体返回结果的格式和字段请参考微信公众平台文档。
方法实现
构建请求URL
使用 'https://api.weixin.qq.com/cgi-bin/get_current_autoreply_info?access_token=' 作为基础URL。
通过调用 AccessToken::getAccessToken() 方法获取当前有效的 access_token并将其拼接到URL中形成完整的请求URL。
定义请求方法:
将请求方法设置为 'POST'表示使用POST请求方式向微信API发送数据。
初始化模板数组:
创建一个空数组 $template用于存储请求数据。
设置主行业ID和副行业ID
将传入的主行业ID $industryId1 和副行业ID $industryId2 分别添加到 $template 数组中,作为请求数据的一部分。
转换为JSON格式
使用 json_encode() 函数将 $template 数组转换为JSON字符串因为微信API通常要求以JSON格式接收请求数据。
发送请求并获取结果:
调用 Curl::callWebServer() 方法传入构建好的请求URL、JSON格式的请求数据以及请求方法向微信API发送POST请求。
返回微信API的响应结果该结果包含了当前的自动回复规则信息。
总结
AutoReply 类通过其静态方法 getRole 提供了便捷的方式来获取微信公众号的自动回复规则。
该方法通过构建请求URL、设置请求方法、准备请求数据并发送请求实现了与微信API的交互。
获取到的自动回复规则信息可以用于进一步的处理或展示,帮助开发者了解和管理公众号的自动回复设置。