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 微信公众号上传永久素材

上一篇:C#微信公众号开发 -- (四)获取API调用所需的全局唯一票据access_token


下一篇:【Leetcode】Trapping Rain Water