[英]How to save save user's facebook profile picture to my domain/website data using php
[英]How To Load And Save Facebook Profile Picture Of User
我想要用戶個人資料圖片的直接鏈接
我有以下代碼
$img = file_get_contents("https://graph.facebook.com/" . $fid . "/picture?type=large");
$file = dirname(__file__). "/" . $fid . ".jpg";
file_put_contents($file, $img);
但是https://graph.facebook.com/" . $fid . "/picture?type=large
具有重定向。 我如何遵循重定向? 有什么辦法可以通過file_get_contents做到這一點? 我知道我可以通過curl
做到這一點,但似乎很復雜,我遇到了一個錯誤,那就是safe_mode
已打開,我不知道如何關閉它。
謝謝
通過為它提供第三個參數$context
您應該能夠使用file_get_contents進行重定向。在該參數中,您將HTTP上下文選項 follow_location
為1。
(盡管這應該已經是默認設置,但在我的測試中,僅使用file_get_contents即可獲取圖像數據。)
她是我正在使用的代碼,它對我來說很完美。 它還將圖片保存到我的服務器,因此我有了一個本地URL(然后可以將其發布回同一用戶的個人資料或其他用戶/頁面/事件/等的牆上。)它在您的代碼中$ user有值,它應該可以正常工作。
<?
$uid = $user;
function GetTheImage($linky) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, $linky);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
# ADDED LINE:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$userpicture = "http://graph.facebook.com/$uid/picture?type=large";
$sourceurl = GetTheImage($userpicture);
$save = fopen("/home/arose/public_html/mydomain.com/tmp/$uid-large.jpg", "w"); //this is name of new file that i save
fwrite($save, $sourceurl);
fclose($save);
?>
<html>
<head>
</head>
<body>
<img src="./tmp/<? echo $uid; ?>-large.jpg" />
</body>
</html>
<?php
$fbid=$user_profile['id'];
$pic = file_get_contents("https://graph.facebook.com/$fbid/picture?type=large");
$filename = $fbid.".jpg";
$path="images/".$filename;
file_put_contents($path, $pic);
?>
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
function curl_redir_exec($ch)
{
static $curl_loops = 0;
static $curl_max_loops = 20;
if ($curl_loops++ >= $curl_max_loops)
{
$curl_loops = 0;
return FALSE;
}
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
@list($header, $data) = @explode("\n\n", $data, 2);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 301 || $http_code == 302)
{
$matches = array();
preg_match('/Location:(.*?)\n/', $header, $matches);
$url = @parse_url(trim(array_pop($matches)));
if (!$url)
{
//couldn't process the url to redirect to
$curl_loops = 0;
return $data;
}
$last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));
if (!$url['scheme'])
$url['scheme'] = $last_url['scheme'];
if (!$url['host'])
$url['host'] = $last_url['host'];
if (!$url['path'])
$url['path'] = $last_url['path'];
$new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . (@$url['query']?'?'.$url['query']:'');
return $new_url;
} else {
$curl_loops=0;
return $data;
}
}
function get_right_url($url) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
return curl_redir_exec($curl);
}
$url = 'http://graph.facebook.com/' . $fbid . '/picture?type=large';
$file_handler = fopen('img/avatar/'.$fbid.'.jpg', 'w');
$curl = curl_init(get_right_url($url));
curl_setopt($curl, CURLOPT_FILE, $file_handler);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_exec($curl);
curl_close($curl);
fclose($file_handler);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.