123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- <?php
- /**
- * OfficialAccountApi.php UTF-8
- * 公众号消息API
- *
- * @date : 2018/9/13 18:03
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @version : HuoMp 1.0
- */
- namespace huoMpMsg\controller;
- use huo\controller\common\Base;
- use huoMpMsg\constant\OfficialAccountConst;
- use huoMpMsg\status\OfficialAccountStatus;
- class OfficialAccountApi extends Base {
- /**
- * 获取微信服务器IP地址
- * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140187
- *
- * @param string $wx_app_id 第三方用户唯一凭证
- * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
- *
- * @return array
- */
- public function getCallbackIp($wx_app_id, $wx_app_secret) {
- $_url = 'https://api.weixin.qq.com/cgi-bin/getcallbackip';
- $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
- $_url = $_url.'?access_token='.$_access_token;
- $_param = '';
- $_ret = Common::curl($_url, $_param);
- $_rdata = [];
- if (Common::isJson($_ret)) {
- $_rdata = json_decode($_ret, true);
- }
- if (isset($_rdata['errcode'])) {
- $_code = $_rdata['errcode'];
- return $this->huoError($_code, $_rdata['errmsg']);
- }
- return $_rdata['ip_list'];
- }
- /**
- * 添加客服帐号
- * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140547
- *
- * @param string $wx_app_id 第三方用户唯一凭证
- * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
- *
- * @return array
- */
- public function addFfAccount($wx_app_id, $wx_app_secret, $kf_account, $nickname, $password) {
- $_url = 'https://api.weixin.qq.com/customservice/kfaccount/add';
- $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
- $_url = $_url.'?access_token='.$_access_token;
- $_param['kf_account'] = $kf_account;
- $_param['nickname'] = $nickname;
- $_param['password'] = $password;
- $_ret = Common::curl($_url, http_build_query($_param), 'POST');
- $_rdata = [];
- if (Common::isJson($_ret)) {
- $_rdata = json_decode($_ret, true);
- }
- if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
- $_code = $_rdata['errcode'];
- return $this->huoError($_code, $_rdata['errmsg']);
- }
- $_code = OfficialAccountStatus::NO_ERROR;
- return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code));
- }
- /**
- * 修改客服帐号
- * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140547
- *
- * @param string $wx_app_id 第三方用户唯一凭证
- * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
- *
- * @param string $kf_account 客服邮箱
- * @param string $nickname 客服昵称
- * @param string $password 客服密码
- *
- * @return array
- */
- public function updateKfAccount($wx_app_id, $wx_app_secret, $kf_account, $nickname, $password) {
- $_url = 'https://api.weixin.qq.com/customservice/kfaccount/update';
- $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
- $_url = $_url.'?access_token='.$_access_token;
- $_param['kf_account'] = $kf_account;
- $_param['nickname'] = $nickname;
- $_param['password'] = $password;
- $_ret = Common::curl($_url, http_build_query($_param), 'POST');
- $_rdata = [];
- if (Common::isJson($_ret)) {
- $_rdata = json_decode($_ret, true);
- }
- if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
- $_code = $_rdata['errcode'];
- return $this->huoError($_code, $_rdata['errmsg']);
- }
- $_code = OfficialAccountStatus::NO_ERROR;
- return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code));
- }
- /**
- * 删除客服帐号
- * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140547
- *
- * @param string $wx_app_id 第三方用户唯一凭证
- * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
- *
- * @param string $kf_account 客服邮箱
- * @param string $nickname 客服昵称
- * @param string $password 客服密码
- *
- * @return array
- */
- public function delKfAccount($wx_app_id, $wx_app_secret, $kf_account, $nickname, $password) {
- $_url = 'https://api.weixin.qq.com/customservice/kfaccount/del';
- $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
- $_url = $_url.'?access_token='.$_access_token;
- $_param['kf_account'] = $kf_account;
- $_param['nickname'] = $nickname;
- $_param['password'] = $password;
- $_ret = Common::curl($_url, http_build_query($_param), 'POST');
- $_rdata = [];
- if (Common::isJson($_ret)) {
- $_rdata = json_decode($_ret, true);
- }
- if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
- $_code = $_rdata['errcode'];
- return $this->huoError($_code, $_rdata['errmsg']);
- }
- $_code = OfficialAccountStatus::NO_ERROR;
- return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code));
- }
- /**
- * 设置客服帐号的头像
- * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140547
- *
- * @param string $wx_app_id 第三方用户唯一凭证
- * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
- *
- * @param string $kf_account 客服邮箱
- *
- * @return array
- */
- public function uploadKfHeadImg($wx_app_id, $wx_app_secret, $kf_account) {
- $_url = 'http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg';
- $_code = OfficialAccountStatus::NO_ERROR;
- return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code));
- }
- /**
- * 获取所有客服账号
- * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140547
- *
- * @param string $wx_app_id 第三方用户唯一凭证
- * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
- *
- * @return array
- */
- public function getKfList($wx_app_id, $wx_app_secret) {
- $_url = 'https://api.weixin.qq.com/cgi-bin/customservice/getkflist';
- $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
- $_url = $_url.'?access_token='.$_access_token;
- $_ret = Common::curl($_url, '');
- $_rdata = [];
- if (Common::isJson($_ret)) {
- $_rdata = json_decode($_ret, true);
- }
- if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
- $_code = $_rdata['errcode'];
- return $this->huoError($_code, $_rdata['errmsg']);
- }
- $_code = OfficialAccountStatus::NO_ERROR;
- return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code), $_rdata);
- }
- /**
- * 获取所有客服账号
- * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140547
- *
- * @param string $wx_app_id 第三方用户唯一凭证
- * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
- *
- * @param string $open_id
- * @param string $msgtype 消息类型
- * @param mixd $data
- *
- * @return array
- */
- public function sendMsg($wx_app_id, $wx_app_secret, $open_id, $msgtype, $data = null) {
- $_url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send';
- $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
- $_url = $_url.'?access_token='.$_access_token;
- $_param['touser'] = $open_id;
- $_param['msgtype'] = $msgtype;
- switch ($msgtype) {
- case OfficialAccountConst::MSG_TYPE_TEXT:
- $_param[$msgtype] = $data;
- break;
- case OfficialAccountConst::MSG_TYPE_LINK:
- case OfficialAccountConst::MSG_TYPE_IMAGE:
- case OfficialAccountConst::MSG_TYPE_VOICE:
- $_param[$msgtype] = $data;
- break;
- case OfficialAccountConst::MSG_TYPE_VIDEO:
- break;
- case OfficialAccountConst::MSG_TYPE_MUSIC:
- break;
- case OfficialAccountConst::MSG_TYPE_NEWS:
- break;
- case OfficialAccountConst::MSG_TYPE_MPNEWS:
- break;
- case OfficialAccountConst::MSG_TYPE_WXCARD:
- break;
- case OfficialAccountConst::MSG_TYPE_MINIPROGRAMPAGE:
- break;
- default:
- break;
- }
- $_param_json = json_encode($_param, JSON_UNESCAPED_UNICODE);
- $_header = ['Content-Type: application/octet-stream; charset=utf-8'];
- $_ret = Common::curl($_url, $_param_json, 'POST', $_header);
- $_rdata = [];
- if (Common::isJson($_ret)) {
- $_rdata = json_decode($_ret, true);
- }
- if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
- $_code = $_rdata['errcode'];
- return $this->huoError($_code, $_rdata['errmsg']);
- }
- $_code = OfficialAccountStatus::NO_ERROR;
- return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code));
- }
- /**
- * 发送模板消息
- * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277
- *
- * @param string $wx_app_id 第三方用户唯一凭证
- * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
- *
- * @param string $open_id
- * @param string $template_id 模板ID
- * @param string $url 模板跳转链接
- * @param array $miniprogram 跳小程序所需数据,不需跳小程序可不用传该数据
- * @param mixed $data 模板数据
- *
- * @return array
- */
- public function sendTemplateMsg(
- $wx_app_id, $wx_app_secret, $open_id, $template_id, $url = '', $miniprogram = [], $data
- ) {
- $_url = 'https://api.weixin.qq.com/cgi-bin/message/template/send';
- $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
- $_url = $_url.'?access_token='.$_access_token;
- $_param['touser'] = $open_id;
- $_param['template_id'] = $template_id;
- $_param['url'] = $url;
- $_param['miniprogram'] = $miniprogram;
- $_param['data'] = $data;
- $_param_json = json_encode($_param);
- $_header = ['Content-Type: application/octet-stream; charset=utf-8'];
- $_ret = Common::curl($_url, $_param_json, 'POST', $_header);
- $_rdata = [];
- if (Common::isJson($_ret)) {
- $_rdata = json_decode($_ret, true);
- }
- if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
- $_code = $_rdata['errcode'];
- return $this->huoError($_code, $_rdata['errmsg']);
- }
- $_code = OfficialAccountStatus::NO_ERROR;
- return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code), $_rdata);
- }
- /**
- * 发送模板消息
- * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277
- *
- * @param string $wx_app_id 第三方用户唯一凭证
- * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
- * @param string $file_path 图片绝对路径
- *
- * @return array
- */
- public function imageUpload($wx_app_id, $wx_app_secret, $file_path) {
- $_url = 'https://api.weixin.qq.com/cgi-bin/media/upload';
- $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
- $_type = "image";
- $_url = $_url.'?access_token='.$_access_token.'&type='.$_type;
- //这里声明文件的路径,使用绝对路径
- $_file_data = array("media" => new \CURLFile($file_path));
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $_url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $_file_data);
- $_ret = curl_exec($ch);//发送请求获取结果
- curl_close($ch);//关闭会话
- $_rdata = [];
- if (Common::isJson($_ret)) {
- $_rdata = json_decode($_ret, true);
- }
- if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
- $_code = $_rdata['errcode'];
- return $this->huoError($_code, $_rdata['errmsg']);
- }
- $_code = OfficialAccountStatus::NO_ERROR;
- return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code), $_rdata);
- }
- }
|