123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php
- /**
- * MediaApi.php UTF-8
- * 素材接口
- *
- * @date : 2018/9/25 18:27
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @version : HuoMp 1.0
- */
- namespace huoMpMsg\controller;
- use huo\controller\common\Base;
- use huoMpMsg\status\OfficialAccountStatus;
- class MediaApi extends Base {
- const API_URL_PREFIX = 'https://api.weixin.qq.com/cgi-bin';
- const MEDIA_FOREVER_UPLOAD_URL = '/material/add_material?';
- const MEDIA_FOREVER_NEWS_UPLOAD_URL = '/material/add_news?';
- const MEDIA_FOREVER_NEWS_UPDATE_URL = '/material/update_news?';
- const MEDIA_FOREVER_GET_URL = '/material/get_material?';
- const MEDIA_FOREVER_DEL_URL = '/material/del_material?';
- const MEDIA_FOREVER_COUNT_URL = '/material/get_materialcount?';
- const MEDIA_FOREVER_BATCHGET_URL = '/material/batchget_material?';
- /**
- * 获取永久素材列表
- *
- * @param string $wx_app_id 第三方用户唯一凭证
- * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
- * @param string $type 类型有image,vedio和audio
- * @param int $offset 起始位置,0表示从第一个
- * @param int $count 个数,区间为0~20
- *
- * @return array
- */
- public function getForeverList($wx_app_id, $wx_app_secret, $type, $offset, $count) {
- $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
- $_url = self::API_URL_PREFIX.self::MEDIA_FOREVER_BATCHGET_URL.'access_token='.$_access_token;
- $_param = array(
- 'type' => $type,
- 'offset' => $offset,
- 'count' => $count,
- );
- $_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);
- }
- /**
- * 上传永久图文素材(认证后的订阅号可用)
- *
- * @param string $wx_app_id 第三方用户唯一凭证
- * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
- * @param string $data a 消息结构{"articles":[{...}]}
- *
- * @return array
- */
- public function uploadForeverArticles($wx_app_id, $wx_app_secret, $data) {
- $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
- $_url = self::API_URL_PREFIX.self::MEDIA_FOREVER_NEWS_UPLOAD_URL.'access_token='.$_access_token;
- $_param = $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) {
- $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
- $_url = self::API_URL_PREFIX.self::MEDIA_FOREVER_UPLOAD_URL;
- $_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);
- }
- }
|