如何保存用户的Facebook个人资料图片保存到使用PHP
问题描述:
我的域名/网站的数据,我想给Facebook用户的个人资料图片保存到我的硬盘,它更像是报废用户的个人资料图片分贝时,他们注册的第一位。”如何保存用户的Facebook个人资料图片保存到使用PHP
例如这里的URL。
https://graph.facebook.com/ {ID} /图片
我想将其保存在特定目录下。 也,如果没有图片,我想要下载默认的占位符,也就是GIF。上面的url实际上只有一个占位符。
我是初学者在PHP,请解释一下我详细。
答
<?php
$image = file_get_contents('https://graph.facebook.com/100003027438870/picture'); // sets $image to the contents of the url
file_put_contents('/path/image.gif', $image); // places the contents in the file /path/image.gif
?>
答
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);
我在Facebook上的版权政策方面的专家,但如果这是T&Cs的允许范围内(你和/或是否还需要询问用户),它可能是值得一试。 – ChrisW 2012-03-17 17:01:03
看起来Facebook允许您通过公开可用的信息(如user_profile中的信息)来做你想做的事情:另外,当您下载或使用此类第三方服务时,他们可以访问您的公开个人资料,其中包括您的用户名或用户ID ,你的年龄范围和国家/语言,你的朋友列表,以及你与他们分享的任何信息。这些应用程序,网站或综合服务收集的信息受其自身条款和政策的约束。 https://www.facebook.com/policy.php中的'如何分享这些信息?'一节。 – Simon 2015-05-03 19:28:40