thinkphp 把小程序码二进制流存储到本地

时间:2022-06-08 07:01:57
 1 public function getxcxm(){
 2         $id = input('id');
 3         $astk = json_decode($this->getasstk())->access_token;
 4         $url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=".$astk;
 5         $data = array(
 6             'path' => "pages/app/querytest?id=".$id,
 7             'width' => 430,
 8             'is_hyaline' => true
 9         );
10         $xcxm = $this->http_request($url,json_encode($data));
11         $path = ROOT_PATH . 'public/upload/'.date("Ymd/");
12         if(!is_dir($path)){
13             mkdir($path,0755,true);
14         }
15         $filepath = $path.time().".png";
16         $file = fopen($filepath,"w");
17         fwrite($file,$xcxm);
18         fclose($file);
19         return $filepath;    
20     }

实际开发中 我这样写的:

 1 //获取小程序码
 2     public function getxcxm(){
 3         $id = input('id');
 4         //先从本地读取,本地没有再向微信提取
 5         $ckname = ROOT_PATH . 'public/upload/recode/'.$id.".png";
 6         if(file_exists($ckname)){
 7             return str_replace("/www/wwwroot/","https://",$ckname);
 8         }else{
 9             $astk = json_decode($this->getasstk())->access_token;
10             $url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=".$astk;
11             $data = array(
12                 'path' => "pages/app/querytest?id=".$id,
13                 'width' => 430,
14                 'is_hyaline' => true
15             );
16             $xcxm = $this->http_request($url,json_encode($data));
17             $path = ROOT_PATH . 'public/upload/recode/';
18             if(!is_dir($path)){
19                 mkdir($path,0755,true);
20             }
21             $filepath = $path.$id.".png";
22             $file = fopen($filepath,"w");
23             fwrite($file,$xcxm);
24             fclose($file);
25             return str_replace("/www/wwwroot/","https://",$filepath);
26         }
27     }