• PHP下载微信头像


    public function downloadPic($openid='',$headimgurl='')
        {
           $header = array(   
           'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0',    
           'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',    
           'Accept-Encoding: gzip, deflate',);
           $url=$headimgurl;
           $curl = curl_init();
     
           curl_setopt($curl, CURLOPT_URL, $url);
           curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
           curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
           curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
           curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
           $data = curl_exec($curl);
           $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
     
           curl_close($curl);
           if ($code == 200) {//把URL格式的图片转成base64_encode格式的!    
              $imgBase64Code = "data:image/jpeg;base64," . base64_encode($data);
           }
           $img_content=$imgBase64Code;//图片内容
           // echo $img_content;exit;
           if (preg_match('/^(data:s*image/(w+);base64,)/', $img_content, $result))
           { 
               $type = $result[2];//得到图片类型png?jpg?gif?  
               // dump($result);exit;
               
               $new_file = ROOT_PATH . 'public' . DS . 'uploads/'.$openid.'.'.$type;//存储目录
               if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $img_content))))
               { 
                    $pic = $openid.'.'.$type;
                    return $pic;
               }
           }
    
        }
    

      

  • 相关阅读:
    非冒泡事件的冒泡支持
    js--题目二
    js-- 一些题目
    jQuery 请指出'$'和'$.fn'的区别?或者说出'$.fn'的用途。
    jQuery 请指出'.bind()','.live()'和'.delegate()'的区别
    什么时候不能完全按照设计稿来
    edm注意细节
    响应式设计
    css -- 题目汇总
    什么是Handler(四)
  • 原文地址:https://www.cnblogs.com/huanghanyi/p/9890285.html
Copyright © 2020-2023  润新知