* @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); } }