


Implementation method of saving user avatar to server after WeChat authorization
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":[]}';
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);
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']));
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']); } //快来写你的注册或者修改代码吧! }
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; }
Create the file at the specified location
function create($dir){ if(is_dir($dir)){ $temp = explode('/',$dir); $cur_dir = ''; for($i=0; $i<count><p>Hope it is useful to everyone. </p> <!--<ul class="list-unstyled text-muted mt30"><li> Previous article: Android Basics 2: Service</li><li class="mt5">Next article: Spark User-guide Summary - Basic Programming</li></ul>--><h2 class="h4 post-comment-title"></h2> <p class="comments--news comments--default comments--article ">Related recommendations: </p> <p class="comments--news comments--default comments--article "><a href="http://www.php.cn/weixin-kaifa-365131.html" target="_self">How to use WeChat authorized login? Summarize the usage of WeChat authorized login examples</a>、</p> <p class="comments--news comments--default comments--article "><a href="http://www.php.cn/weixin-kaifa-360264.html" target="_self">Detailed explanation of WeChat authorization for WeChat development</a></p> <p class="comments--news comments--default comments--article "><a href="http://www.php.cn/weixin-kaifa-359005.html" target="_self">IOS implementation of WeChat authorized login function example code</a></p> <p class="comments-box-content"><br></p></count>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor
