php与微信上传永久图文

 /*
     * 新增永久素材
     * */
    public function addImg(){
        $filetype=explode('.',$_FILES["uploadfile"]["name"]);
        $file_info=array(
            'filename'=>$_FILES["uploadfile"]["name"],      //图片相对于网站根目录的路径
            'content-type'=>$_FILES["uploadfile"]["type"],  //文件类型
            'filelength'=>$_FILES["uploadfile"]["size"]     //图文大小
        );
        $real_path='C:/Users/Administrator/Desktop/'."{$file_info['filename']}";
        $ImgList= $this->uploadImg($real_path);
        $ImgList=json_decode($ImgList,true);
        if($ImgList['media_id']){
            echo '图片上传成功!<br/>media_id:'.$ImgList['media_id'];
            $this->showImages($ImgList['media_id']);
        }else{
            dump($ImgList);
        }
    }

//图片上传整合需要的东西
    function uploadImg($imgUrl){
        $TOKEN=$this->access_token();
        $URL ='https://api.weixin.qq.com/cgi-bin/material/add_material?access_token='.$TOKEN.'&type=image';//上传临时文件
        $data = array('media'=>"@".$imgUrl);
        $result = $this->http_post($URL,$data);
        $data = @json_decode($result,true);
        return $result;
    }
//显示上传打印出图片
    function showImages($media_id){
        $TOKEN=$this->access_token();
        $url = 'https://api.weixin.qq.com/cgi-bin/material/get_material?access_token='.$TOKEN;//获取临时文件
        $return_content =$this->http_post($url,json_encode(array('media_id'=>$media_id)));
        $filename = date('Ymdhiss').'.jpg';  //设置保存在domain中的文件名
        $fp= @fopen("Public/Uploads/text/".$filename,"a"); //将文件绑定到流 ??
        $fileurl=fwrite($fp,$return_content); //写入文件
        echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
        echo '<br/>图片下载成功!<br/>';
        echo "<img width='300' src='".trim(__ROOT__.'/Public/Uploads/text/'.$filename)."'>";
    }

    /*
     * 获取access_token
     *一个access_token可以使用2分钟,这样可以降低调用access_token的频率
     * */
    public function access_token() {
       $appid='你的appid';
        $appsecret='你的Secret';//现在要获取微信公众号的access_token,需要在公众号里添加IP白名单,否则获取不了 
        $file = file_get_contents("./access_token.json",true);
        $result = json_decode($file,true);
        if (!$result['access_token']||time() > $result['expires']){
            $data = array();
            $data['access_token'] = $this->getNewToken($appid,$appsecret);
            $data['expires']=time()+7000;
            $jsonStr =  json_encode($data);
            $fp = fopen("./access_token.json", "w");
            fwrite($fp, $jsonStr);
            fclose($fp);
            return $data['access_token'];
        }else{
            return $result['access_token'];
        }


    }

    //获取上传的图片
    function http_get($url) {

        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'GET' );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt ( $ch, CURLOPT_URL, $url );
        ob_start ();
        curl_exec ( $ch );
        $return_content = ob_get_contents ();
        ob_end_clean ();

        $return_code = curl_getinfo ( $ch, CURLINFO_HTTP_CODE );
        return $return_content;
    }
    //上传图片用到的curl
    function http_post($url, $data = null)
    {
        //创建一个新cURL资源
        $curl = curl_init();
        //设置URL和相应的选项
        curl_setopt($curl, CURLOPT_URL, $url);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //执行curl,抓取URL并把它传递给浏览器
        $output = curl_exec($curl);
        //关闭cURL资源,并且释放系统资源
        curl_close($curl);
        return $output;
    }

猜你喜欢

转载自blog.csdn.net/lyx_lyq/article/details/78027266
今日推荐