The previous article talked about obtaining the user’s personal information. Let’s talk about obtaining the user’s WeChat avatar. Many developers need to obtain the user’s avatar when displaying user information. Usually They are all url connections. We need to download and save the image to facilitate our call;
In the previous article, we said that we used access_token to obtain the user's personal information. We obtained the headimgurl key It is the avatar connection. We need to intercept the content of the connection and then save the image to our server for our convenience. Let’s add the code below:
public function userIconSave($url,$openid){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); $file = curl_exec($ch); curl_close($ch); $resource = fopen($_SERVER['DOCUMENT_ROOT']."/usericon/" . $openid.".jpg" ,'a'); fwrite($resource, $file); fclose($resource); }
The idea is the same as obtaining user information, intercepting the image information through the curl function. Then just put the picture into the file. The fopen() and fwrite() functions are mainly used to operate. fopen can create the file, and then fwrite writes the content to the file. The file path can be saved in the database. When the time comes, It can be called at any time. If the user updates the file, as long as it is not transferred out and compared, the user can save it and the same avatar will no longer be updated;
The above is the detailed content of php WeChat public account development, obtain user avatar and download. For more information, please follow other related articles on the PHP Chinese website!