博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
素材上传
阅读量:4616 次
发布时间:2019-06-09

本文共 3141 字,大约阅读时间需要 10 分钟。

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; }}

转载于:https://www.cnblogs.com/chentailin/p/9083315.html

你可能感兴趣的文章
2015.09.13 网易游戏在线笔试(运营开发工程师)
查看>>
Atcoder D - Widespread (二分)
查看>>
centos下安装postgresql
查看>>
未来SEO的发展方向和趋势猜测与分析
查看>>
Python面向对象——多态
查看>>
键盘事件的运用
查看>>
e b
查看>>
CSS居中
查看>>
搜索引擎点击隐藏文字
查看>>
关于YII2如何修改默认控制器的问题
查看>>
QML和JS引擎的关系以及调用c++函数的原理
查看>>
35.在PCB中删除元件
查看>>
数据库MySQL-----介绍,安装配置
查看>>
Friends (ZOJ - 3710)
查看>>
解决:Ubuntu无法进入图形化界面(报错/dev/sda2:clean)
查看>>
div(固定宽度和不固定宽度)居中显示的方法总结
查看>>
还原MySql数据库失败:max_allowed_packet 设置过小导致记录写入失败
查看>>
PhpStorm 9.03 集成 开源中国(oschina.net)的Git项目,提交SVN时注意事项
查看>>
css选择器的优先级
查看>>
学习笔记3
查看>>