Save the visitor's WeChat avatar to the server

Source: Internet
Author: User
Save the visitor's profile picture to the server. note: Currently, the address of the visitor's profile picture is obtained through authorization and saved in the database. it is displayed through the address when used at the front-end.
Although a visitor attempts to update the nickname and Avatar address each time they access the homepage, the Avatar cannot be displayed when the visitor modifies the avatar until the visitor accesses the homepage again.
We hope to save the avatar to the database and call it directly to avoid the above situation.


The avatar can be opened smoothly in the browser, but it cannot be obtained through curl or get_file_contents.


Reply to discussion (solution)

Used mobile phone and Weibo login interfaces
The image address they provide is file_get_contents ().
Then, save file_put_content () in the local folder. set the path and naming rules.
A function should be encapsulated to save the remote image.
Save the local path to the server.

Function put_file_from_url_content ($ url, $ saveName, $ path ){
// Set the running time to unlimited
Set_time_limit (0 );

$ Url = trim ($ url );
$ Curl = curl_init ();
// Set the URL you want to capture
Curl_setopt ($ curl, CURLOPT_URL, $ url );
// Set the header
Curl_setopt ($ curl, CURLOPT_HEADER, 1 );
// Set the cURL parameter to save the result to the string or output to the screen.
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 );
// Run cURL to request the webpage
$ File = curl_exec ($ curl );
// Close the URL request
Curl_close ($ curl );
// Write the file into the obtained data
$ Filename = $ path. $ saveName;
$ Write = @ fopen ($ filename, "w ");
If ($ write = false ){
Return false;
}
If (fwrite ($ write, $ file) = false ){
Return false;
}
If (fclose ($ write) = false ){
Return false;
}
}

Put_file_from_url_content ($ userAvatar, $ userOpenId. ". jpg", $ avatarPath );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.