微信素材管理之获取永久素材和删除永久素材

注意:获取永久素材/删除永久素材/新增永久素材 需要认证号,且接口限制为10

/**
     * 获取素材列表[永久]
     * $material_type [类型] image/video/voice/news 图片 视频 语音 图文
     * QVqFyY4hY4_xpPf_lO2OG1fIslTDY4wzkgg72IOeoDo
     * QVqFyY4hY4_xpPf_lO2OG80_sJiK4v2BGGq1-cwQSLU
     */
    function getMaterial($material_type="image",$offset=0,$count=20){
        $access_token=$this->getAccessToken();  
        $type = $material_type;
        $url = 'https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token='.$access_token;
        $filedata = array(
            'type' => $type,
            'offset' => $offset,
            'count' => $count
        );
        $filedata = json_encode($filedata,true);
        $result = $this->https_request($url, $filedata);
        $result = json_decode($result, true);
        dump($result);
    }

    /**
     * 删除永久素材
     * 
     */
    function delMaterial($media_id){
        $access_token = $this -> getAccessToken();
        $url="https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=".$access_token;
        if(empty($media_id)){
            $this-> error('素材ID不能为空');
        }
        $filedata = array( 'media_id'=> $media_id);
        $filedata = json_encode($filedata,true);
        $result = $this->https_request($url, $filedata);
        $result = json_decode($result, true);
        if($result['errcode']==0){
            echo "素材删除成功";
        }elseif ($result['errcode']==40007) {
            echo "素材ID不正确";
        }
        dump($result);

    }

    //https请求(支持GET和POST)
    protected function https_request($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

你可能感兴趣的:(微信开发,ThinkPHP)