Php 使用cURL保存facebook个人资料图像
我正在尝试使用CURL在facebook上保存用户档案图像。当我使用下面的代码时,我保存了一个jpeg图像,但其中没有字节。但是,如果我将url值交换到,它是where'$用户标识。”/picture?type=large可重定向浏览器,图像保存时不会出现问题。我做错了什么Php 使用cURL保存facebook个人资料图像,php,facebook,curl,Php,Facebook,Curl,我正在尝试使用CURL在facebook上保存用户档案图像。当我使用下面的代码时,我保存了一个jpeg图像,但其中没有字节。但是,如果我将url值交换到,它是where'$用户标识。”/picture?type=large可重定向浏览器,图像保存时不会出现问题。我做错了什么 <?php $url = 'http://graph.facebook.com/' . $user_id . '/picture?type=large'; $file_handler = fopen('pic_f
<?php
$url = 'http://graph.facebook.com/' . $user_id . '/picture?type=large';
$file_handler = fopen('pic_facebook.jpg', 'w');
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_FILE, $file_handler);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_exec($curl);
curl_close($curl);
fclose($file_handler);
设置
CURLOPT_FOLLOWLOCATION
为true
因此,它遵循301/302重定向,从最终位置读取图像文件。
i、 e
有一个重定向,因此您必须为curl添加此选项
// safemode if off:
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
但如果您的safemode处于启用状态,则:
// safemode if on:
<?php
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/' . $user_id . '/picture?type=large';
$file_handler = fopen('pic_facebook.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);
//安全模式如果打开:
如果无法处理重定向,请尝试以下方法:
向https://graph.facebook.com/?fields=picture
并解析响应,该响应将采用JSON格式,如下所示-例如,对于您,将获得以下响应:
{
"picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/157340_4_3955636_q.jpg"
}
然后直接发出curl请求,从云存储URL检索图像我这样做了,效果非常好:
$data = file_get_contents('https://graph.facebook.com/[App-Scoped-ID]/picture?width=378&height=378&access_token=[Access-Token]');
$file = fopen('fbphoto.jpg', 'w+');
fputs($file, $data);
fclose($file);
您只需要一个应用程序访问令牌(APPID.'|'.APPSECRET),并且可以指定宽度和高度
您还可以将“redirect=false”添加到URL,以获取带有URL的JSON对象(例如:。)
CURLOPT\u FOLLOWLOCATION
已在PHP5.4中删除,因此它不再是一个真正的选项。这可能会有所帮助:谢谢您的回答!Hovwewer,它似乎对我不起作用。可能是我把事情搞砸了。我得到这个错误:警告:curl_setopt()[function.curl setopt]:当启用安全模式或在/home/web81206/domains/utvecklingtesttco.se/public_html/fb/index.php第83行的/code>file\u get\u contents
中设置了open\u basedir时,无法激活CURLOPT followllocation,这将自动完成这部分。谢谢你的回答!Hovwewer,它似乎对我不起作用。可能是我把事情搞砸了。我得到这个错误:警告:curl_setopt()[function.curl setopt]:当启用安全模式或在第83行的/home/web81206/domains/utvecklingtesttco.se/public_html/fb/index.php中设置了open_basedir时,无法激活CURLOPT_FOLLOWLOCATION任何建议?该死。试一下这个函数:不,不能让它工作,这是一个非常简单的事情,我想做,但我似乎没有找到任何有效的代码。真奇怪!谢谢你的帮助!好主意,我也会尝试这个解决方案,即使@deadrunk给了我另一个很好的解决方案,我得到“https://wrapper在服务器配置中被allow\u url\u fopen=0禁用”,这可能会让您感兴趣:
$data = file_get_contents('https://graph.facebook.com/[App-Scoped-ID]/picture?width=378&height=378&access_token=[Access-Token]');
$file = fopen('fbphoto.jpg', 'w+');
fputs($file, $data);
fclose($file);