公众号上传永久图文素材php

如题,今天用到了 就分享一波给大家咯 

token可以自行获取缓存里的,下面我只用一次 就直接获取token了

  //新增永久图文素材
    public function upload_article()
    {
        $res = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appid&secret=secret');
        $res = json_decode($res, true);
        $token = $res['access_token'];
        $url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=$token";
        $data =  '{
            "articles": [{
            "title": "这是个标题",
            "thumb_media_id":"上传获取的图片素材ID",
            "show_cover_pic":"1",
            "content": "内容",
            "content_source_url": "链接",
}]
       }';
        $result = $this->https_post($url, $data);
        dump($result);
        //$result = http_url($url, $data);
        $res = json_decode($result, true);
        //unlink($path);
    }
 function https_post($url, $post_data = null)
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            // post数据
            curl_setopt($ch, CURLOPT_POST, 1);
            // post的变量
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
            $output = curl_exec($ch);
            curl_close($ch);
            return $output;
        }

 

上一篇:Python第三方模块PYCURL使用


下一篇:PHP curl_init函数