Home > Backend Development > PHP Tutorial > php WeChat public account development, obtain user avatar and download

php WeChat public account development, obtain user avatar and download

不言
Release: 2023-03-24 12:06:01
Original
5499 people have browsed it


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);
	}
Copy after login

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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template