說明:目前是通過授權擷取訪問者頭像地址,並儲存在資料庫中,前台使用時則通過地址顯示。
雖然訪問者每次訪問首頁都會嘗試更新暱稱與頭像地址,但在訪問者修改頭像直至再次訪問首頁時,其頭像無法顯示。
希望能將其頭像儲存至資料庫,直接調用,避免以上情況發生。
頭像在瀏覽器中能順利開啟,但無論是通過 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);