php 微信公众号上传永久素材
<?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;
} }
php 微信公众号上传永久素材的更多相关文章
- Java 微信公众号上传永久素材的方法
Java 微信公众号上传永久素材的方法 学习了:http://blog.csdn.net/u013791374/article/details/53258275 膜拜一下,源码如下: @Request ...
- 怎么在微信公众号上传PPT?
我们都知道创建一个微信公众号,在公众号中发布一些文章是非常简单的,但公众号添加附件下载的功能却被限制,如今可以使用小程序“微附件”进行在公众号中添加附件. 以下是公众号添加附件使用“微附件”小程序的教 ...
- 基于 muse-ui 封装一个微信公众号上传插件 实现多图上传
Vue.component('my-wx-upload', { template: ` <mu-grid-list :cols="3" :cellHeight="9 ...
- 怎么将PPT文件上传到微信公众号上?
我们都知道创建一个微信公众号,在公众号中发布一些文章是非常简单的,但公众号添加附件下载的功能却被限制,如今可以使用小程序“微附件”进行在公众号中添加附件. 以下是公众号添加附件使用“微附件”小程序的教 ...
- php 微信公众平台上传多媒体接口 41005错误
文链接: http://www.maoyupeng.com/wechart-upload-image-errorcode-41005.html PHP的cURL支持通过给CURL_POSTFIELDS ...
- Azure 项目构建 - 用 Azure 认知服务在微信公众号上搭建智能会务系统
通过完整流程详细介绍了如何在Azure平台上快速搭建基于微信公众号的智慧云会务管理系统. 此系列的全部课程 https://school.azure.cn/curriculums/11 立即访问htt ...
- 个人微信公众号搭建Python实现 -个人公众号搭建-永久素材管理(14.3.5)
@ 目录 1.说明 2.上传素材 3.获取素材列表 关于作者 1.说明 个人微信公众号开发的功能有限,因为很多权限没有,但支持上传永久素材,具体查看微信公众号文档 这里的请求都要将本地IP地址放到微信 ...
- 微信上传文章素材—ASP.NET MVC从View层传数据到Controller层
View层: $('#btnNews').click(function() { if (!confirm('确定要提交吗?')) { return; } var frontViewData = []; ...
- 在安卓上,微信公众号无法分享到QQ的解决办法之一
今天做一个微信公众号分享功能,参考微信sdk,代码几乎没有任何问题,但就是分享到QQ失败,以下是我QQ分享部分的代码: wx.onMenuShareQQ({ title: '快来和我一起玩转大脑', ...
随机推荐
- jstl 使用
1.需要导入: 1)jstl.jar 2)standard.jar 引入:jsp相应的核心库:<%@taglib prefix="c" uri="http:// ...
- Scrapy 框架(二)数据的持久化
scrapy数据的持久化(将数据保存到数据库) 一.建立项目 1.scrapy startproject dushu 2.进入项目 cd dushu 执行:scrapy genspider -t cr ...
- (二)基于阿里云的MQTT远程控制(购买阿里云,在云端安装MQTT,测试MQTT远程通信)
QQ名称为Friday~的网友把他自己买MQTT的过程截图发给了我,今天就说一下如何购买阿里云,安装MQTT可以参考 http://www.cnblogs.com/yangfengwu/p/77646 ...
- abp 如何设置和访问嵌入资源(视图、css、js等)
1.设置文件为嵌入资源 2.在含有嵌入资源的程序集下的模版类下,配置暴露嵌入的资源. Configuration.EmbeddedResources.Sources.Add( new Embedded ...
- linux_vim
今天稍微学习以下vim 学习的一些课件: 1. 2. 3. 4. 5.
- 20155333 《网络对抗》 Exp9 Web安全基础
20155333 <网络对抗> Exp9 Web安全基础 基础问题回答 1.SQL注入攻击原理,如何防御? 原理: 通过在用户名.密码登输入框中输入一些',--,#等特殊字符,实现引号闭合 ...
- Hibernate一对多关联关系保存时的探究
在以前使用hibernate时,经常对保存存在关联关系的对象时,不确定是否能保存成功. 因此,特意对一对多关系的2个对象进行实践. 一.pojo类和配置文件的准备 这里有一点提前 ...
- vue-cli,build 后,报错的解决办法
报错如下图: 或: 解决办法:config / index.js 中,找到 build { assetsPublicPath: '/' },将其设置为:assetsPublicPath: './ ' ...
- C语言与数据库操作入门
https://blog.csdn.net/flyingqd/article/details/78763652 C语言与数据库操作入门(Win版) 2017年12月10日 17:30:17 阅读数:1 ...
- 12、利用docker快速搭建Wordpress网站
一.准备工作 结构图: 用户访问页面,Nginx将请求进行转发,如果请求的是php页面,则通过FastCGI转发给后端php进行处理:如果非php页面,则直接返回静态页面. 关键点: mysql.ph ...