儲存訪問者微信頭像至伺服器

來源:互聯網
上載者:User
說明:目前是通過授權擷取訪問者頭像地址,並儲存在資料庫中,前台使用時則通過地址顯示。
雖然訪問者每次訪問首頁都會嘗試更新暱稱與頭像地址,但在訪問者修改頭像直至再次訪問首頁時,其頭像無法顯示。
希望能將其頭像儲存至資料庫,直接調用,避免以上情況發生。


頭像在瀏覽器中能順利開啟,但無論是通過 curl 還是 get_file_contents() 都無法正常擷取。


回複討論(解決方案)

做過手機端和微博登陸介面
他們提供的圖片地址是可以file_get_contents()的
然後直接file_put_content()儲存在本地檔案夾,至於路徑和命名規則自己定
儲存遠程圖片應該封裝一個函數
然後把本地路徑儲存在伺服器上

function put_file_from_url_content($url, $saveName, $path) {
// 設定已耗用時間為無限制
set_time_limit ( 0 );

$url = trim ( $url );
$curl = curl_init ();
// 設定你需要抓取的URL
curl_setopt ( $curl, CURLOPT_URL, $url );
// 設定header
curl_setopt ( $curl, CURLOPT_HEADER, 1 );
// 設定cURL 參數,要求結果儲存到字串中還是輸出到螢幕上。
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
// 運行cURL,請求網頁
$file = curl_exec ( $curl );
// 關閉URL請求
curl_close ( $curl );
// 將檔案寫入獲得的資料
$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);

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.