天天看點

公衆号上傳永久圖文素材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;
        }
           

繼續閱讀