Home > Backend Development > PHP Tutorial > Detailed introduction of new permanent materials in php WeChat (basic process)

Detailed introduction of new permanent materials in php WeChat (basic process)

不言
Release: 2023-04-03 20:18:02
Original
3363 people have browsed it

This article brings you a detailed introduction (basic process) about the new permanent materials of PHP WeChat. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

WeChat’s new permanent materials, the most important ones are graphic materials, the basic process is as follows:

  1. Get access_token;

  2. Upload the cover material and get the media_id of the permanent material, which can be images or thumb type

  3. Upload the picture in the text and get the URL of the Tencent domain name of the picture

  4. Upload permanent graphics and text materials

I won’t go into the first step, curl can solve it.

The second step to obtain the permanent material is similar to the previous article to obtain the temporary ID. I will post the code later

The third step is to obtain the image URL in the image and text, which is mainly curl, and The process of obtaining materials is almost the same

The fourth step is the most important, but it is easy with the artifact

The code is basically the following encapsulated methods, the picture address can refer to the address in the previous article , the rest are very simple

    /*
    * 新增永久图文
     */
    public function addEver_media($json){
        $url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=".$this->get_access_token();
        $res = $this->post($url, $json, 'json');
        // 异常处理: 获取时网络错误
        // 判断是否调用成功        
        return $res;
    }
  /*
    * 新增永久素材
     */
    public function addSucai($path,$type,$title="",$dis=""){
 
       $url = 
"https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=".$this->get_access_token()."&type=".$type;
        if($type=="video"){
            $b = json_encode(array(
                    'title'=>$title,
                    'introduction'=>$dis
                ));
            $res = array(
                'media' => '@'.$path,
                'description'=>$b
                
            );
            $res = $this->upload($url, $res);
        }else{
            $res = $this->upload($url, array('media' => '@'.$path));
        }
        // 判断是否调用成功
        return $res;
    } 
    /*
    * 获取永久素材url
     */
    public function getSucaiURL($media){
        $url = "https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=" .$this->get_access_token();
        $arr = json_encode(array('media_id' => $media));
        $res = $this->post($url, $arr, 'json');   
        // 判断是否调用成功
        return $res;
    }
    /*
     * 上传图片获取url
     */
    public function getPicURL($path){
        $url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=" .$this->get_access_token() ;
        $res = $this->upload($url, array('media' => '@'.$path));    
        // 判断是否调用成功
        return $res;
    }
  /*
    * 上传图片。图文专用
     */
    public static function upload($url, $filedata) {  
        $curl = curl_init ();  
        if (class_exists ( '/CURLFile' )) {//php5.5跟php5.6中的CURLOPT_SAFE_UPLOAD的默认值不同  
            curl_setopt ( $curl, CURLOPT_SAFE_UPLOAD, true );  
        } else {  
            if (defined ( 'CURLOPT_SAFE_UPLOAD' )) {  
                curl_setopt ( $curl, CURLOPT_SAFE_UPLOAD, false );  
            }  
        }  
        curl_setopt ( $curl, CURLOPT_URL, $url );  
        curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE );  
        curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, FALSE );  
        if (! empty ( $filedata )) {  
            curl_setopt ( $curl, CURLOPT_POST, 1 );  
            curl_setopt ( $curl, CURLOPT_POSTFIELDS, $filedata );  
        }  
        curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );  
        $output = curl_exec ( $curl );  
        curl_close ( $curl );  
        return $output;  
          
    }
    public function post($url, $fields, $data_type='text')
    {
        $cl = curl_init();
        if(stripos($url, 'https://') !== FALSE) {
            curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($cl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($cl, CURLOPT_SSLVERSION, 1);
        }
        curl_setopt($cl, CURLOPT_URL, $url);
        curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($cl, CURLOPT_POST, true);        
        curl_setopt($cl, CURLOPT_POSTFIELDS, $fields);
        $content = curl_exec($cl);
        $status = curl_getinfo($cl);
        curl_close($cl);
        if (isset($status['http_code']) && $status['http_code'] == 200) {
            if ($data_type == 'json') {
                $content = json_decode($content);
            }
            return $content;
        } else {
            return FALSE;
        }
    }
Copy after login

Related recommendations:

Related recommendations for permanent materials

Detailed explanation of using php Call the WeChat interface to upload permanent materials

The above is the detailed content of Detailed introduction of new permanent materials in php WeChat (basic process). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template