Save visitors ' WeChat avatar to server

Source: Internet
Author: User
Description: Currently is authorized to obtain the visitor's avatar address, and saved in the database, the foreground when used by the address display.
Although visitors try to update their nickname and Avatar address every time they visit the home page, their avatar cannot be displayed when the visitor modifies the avatar until the page is accessed again.
Want to be able to save their avatar to the database, directly called, to avoid the above situation.


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


Reply to discussion (solution)

have done mobile phone and microblogging login interface
They provide a picture address that can be file_get_contents ()
Then directly file_put_content () is saved in the local folder, as the path and naming rules are set by themselves
Saving a remote picture should encapsulate a function
Then save the local path on the server

function Put_file_from_url_content ($url, $saveName, $path) {
Set run time to unlimited
Set_time_limit (0);

$url = Trim ($url);
$curl = Curl_init ();
Set the URL you need to crawl
curl_setopt ($curl, Curlopt_url, $url);
Set Header
curl_setopt ($curl, Curlopt_header, 1);
Sets the curl parameter, which requires the result to be saved to a string or output to the screen.
curl_setopt ($curl, Curlopt_returntransfer, 1);
Run Curl, request a Web page
$file = curl_exec ($curl);
Close URL Request
Curl_close ($curl);
Write a file to 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);

  • Related Article

    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.