Implementation method of saving user avatar to server after WeChat authorization

小云云
Release: 2018-02-02 17:30:43
Original
3894 people have browsed it

This article mainly shares with you how to save the user's avatar to the server after WeChat authorization. I recently worked on a project about WeChat authorized login. I won't say much about authorization. I obtained the user's basic information in the following format:

$user_info = '{"openid":"xx","nickname":"nice花椒.","sex":2,"language":"zh_CN","city":"杭州","province":"浙江","country":"中国","headimgurl":"http:\/\/wx.qlogo.cn\/mmopen\/vi_32\/Q0j4TwGTfTJPpziaqOoIpGia9ZVj9fheAwLmTiavV4rxR40NQ1zPQPyiaMY58FEISDIfpn0q5VeeXliaKyiaiabgK2ZHg\/0","privilege":[]}';
Copy after login

Save headimgurl directly into the database, a bug is coming.

Without further ado, let’s get to the point:

WeChat returns json format

$user_arr = json_decode($user_info, true);
Copy after login

Get the user’s openid and compare it with the database to determine whether it is the first authorization

$mem_info= $member->getuserinfo(array('openid'=>$user_arr['openid']));
Copy after login

First authorization or the avatar link address is different from the last time

if(!$mem_info || $mem_info['wximage']!=$user_info['headimg']){ $headimgurl = saveheadimgurl($user_arr['headimgurl']); $headimg ? 0 : $headimg='lib/avata.jpg' ; if($mem_info['wximage']!=$user_info['headimg']){ unlink($mem_info['headimg']); } //快来写你的注册或者修改代码吧! }
Copy after login

Save the picture

function saveheadimgurl($headimg){ $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', ); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $headimg); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curl, CURLOPT_HTTPHEADER,$header); $dataimg = curl_exec($curl); $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); if ($code == 200) { $imgBase64Code = "data:image/jpeg;base64," . base64_encode($dataimg); } $img_content=$imgBase64Code; if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $img_content, $result)){ $type = $result[2]; create(DOFILESPATH."head"); $new_file = DOFILESPATH."head/".md5(uniqid(rand())).".{$type}"; if (file_put_contents($new_file, base64_decode(str_replace($result[1],'', $img_content)))) { return $new_file; } } return false; }
Copy after login

Create the file at the specified location

function create($dir){ if(is_dir($dir)){ $temp = explode('/',$dir); $cur_dir = ''; for($i=0; $i
        
Copy after login

Hope it is useful to everyone.

Related recommendations:

How to use WeChat authorized login? Summarize the usage of WeChat authorized login examples

Detailed explanation of WeChat authorization for WeChat development

IOS implementation of WeChat authorized login function example code


The above is the detailed content of Implementation method of saving user avatar to server after WeChat authorization. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!