1. 程式人生 > >php與微信上傳永久圖文

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;
    }