php 微信公众号上传永久素材
2016-02-22 15:11
579 查看
<?php namespace Mob\Controller; use Think\Controller; class WxmaterialController extends Controller { private $appId; private $appSecret; protected function _initialize() { /* 读取站点配置 */ $config = api('Config/lists'); C($config); $this -> appId = C('WX_APPID'); $this -> appSecret = C('WX_SECRET'); } function add_material() { $file_info = array('filename' => '/Public/1111.jpg', //国片相对于网站根目录的路径 'content-type' => 'image/jpg', //文件类型 'filelength' => '71' //图文大小 ); dump($file_info); $access_token = $this -> get_access_token(); $url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$access_token}&type=image"; $ch1 = curl_init(); $timeout = 5; $real_path = "{$_SERVER['DOCUMENT_ROOT']}{$file_info['filename']}"; //$real_path=str_replace("/", "//", $real_path); $data = array("media" => "@{$real_path}", 'form-data' => $file_info); curl_setopt($ch1, CURLOPT_URL, $url); curl_setopt($ch1, CURLOPT_POST, 1); curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch1, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch1, CURLOPT_POSTFIELDS, $data); $result = curl_exec($ch1); curl_close($ch1); if (curl_errno() == 0) { $result = json_decode($result, true); var_dump($result); return $result['media_id']; } else { return false; } } // ===================================== // = 获取微信公众号的 access_token= // ===================================== private function get_access_token() { $m_appact = M('Appact', 'ot_', DB_GY); $data = $m_appact -> where(array('appid' => $this -> appId)) -> field('access_token,expire_time') -> find(); if ($data['expire_time'] < time()) { $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret"; $res = json_decode($this -> httpGet($url)); $access_token = $res -> access_token; if ($access_token) { $data['expire_time'] = time() + 7000; $data['access_token'] = $access_token; $r = $m_appact -> where(array('appid' => $this -> appId)) -> find(); $save_arr = array('access_token' => $data['access_token'], 'expire_time' => $data['expire_time']); $add_arr = array('appid' => $this -> appId, 'access_token' => $data['access_token'], 'expire_time' => $data['expire_time']); $r ? $m_appact -> where(array('appid' => $this -> appId)) -> save($save_arr) : $m_appact -> add($add_arr); } } else { $access_token = $data['access_token']; } return $access_token; } private function httpGet($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 500); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_URL, $url); $res = curl_exec($curl); curl_close($curl); return $res; } }
相关文章推荐
- iOS开发 ios9.0后友盟第三方分享, qq微信找不到设备
- 微信企业号开发1--连接
- H5大行其道,微信应用号的个人看法
- Java仿实现微信红包分配算法
- android应用集成微信支付
- 微信分享不显示缩略图,但显示标题和摘要
- 微信第三方登录接口
- 微信接口php
- 采用java单例模式实现微信AccessToken和Jsapi_ticket缓存支持
- 微信研究 如何把微信中的文章转发到微信企业号应用中?(图文)
- 微信研究 如何把微信中的文章转发到企业号应用中?(图文)
- 微信转账选错人,好友收钱后手机关机
- 剖析微信等即时通讯常用组件(一)
- 关于微信公众平台接口的开发
- JAVA实现仿微信红包分配规则
- 欢迎关注个人微信公众号:icsoc
- 微信开发基础教程
- H5小游戏定制开发,微信游戏定制开发公司 -找向阳花科技!
- gameboy网页闯关游戏(riddle webgame)--仿微信聊天的前端页面设计和难点
- asp.net mvc 5 微信接入VB版 - 获取AccessToken