如何使用PHP Graph API保存Facebook个人资料图片

5
我正在使用这个Curl类来保存文件 ->
class CurlHelper
{
  /**
   * Downloads a file from a url and returns the temporary file path.
   * @param string $url
   * @return string The file path
   */
  public static function downloadFile($url, $options = array())
  {
    if (!is_array($options))
      $options = array();
    $options = array_merge(array(
        'connectionTimeout' => 5, // seconds
        'timeout' => 10, // seconds
        'sslVerifyPeer' => false,
        'followLocation' => false, // if true, limit recursive redirection by
        'maxRedirs' => 1, // setting value for "maxRedirs"
        ), $options);

    // create a temporary file (we are assuming that we can write to the system's temporary directory)
    $tempFileName = tempnam(sys_get_temp_dir(), '');
    $fh = fopen($tempFileName, 'w');

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_FILE, $fh);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $options['connectionTimeout']);
    curl_setopt($curl, CURLOPT_TIMEOUT, $options['timeout']);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $options['sslVerifyPeer']);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, $options['followLocation']);
    curl_setopt($curl, CURLOPT_MAXREDIRS, $options['maxRedirs']);
    curl_exec($curl);

    curl_close($curl);
    fclose($fh);

    return $tempFileName;
  }
}

    $url = 'http://graph.facebook.com/shashankvaishnav/picture';
$sourceFilePath = CurlHelper::downloadFile($url, array(
  'followLocation' => true,
  'maxRedirs' => 5,
));

以上代码将在$sourceFilePath变量中给出临时URL,现在我想将该图像存储在我的图像文件夹中。 我被卡住了......请帮助我...提前感谢您。


$tempFileName = tempnam(sys_get_temp_dir(), ''); 这段代码将文件保存为xxxx.tmp,但根据@senthilbp在他的回答中提到的,它应该保存为.jpg。 - thomasbabuj
4个回答

19

这个问题有一个相当简单的解决方案:

$url = 'http://graph.facebook.com/shashankvaishnav/picture';
$data = file_get_contents($url);
$fileName = 'fb_profilepic.jpg';
$file = fopen($fileName, 'w+');
fputs($file, $data);
fclose($file);

你甚至不需要任何其他东西。

或者,如果 file_get_contents 被禁用(通常情况下不会被禁用),这个方法也可以工作:

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, 'http://graph.facebook.com/shashankvaishnav/picture');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
$fileName = 'fb_profilepic.jpg';
$file = fopen($fileName, 'w+');
fputs($file, $data);
fclose($file);

编辑:由于您现在不能再使用用户名进行API调用,因此这已不再可能。在使用Facebook API授权用户后,您必须使用(应用程序作用域)ID而不是用户名。


我也包含了一个使用curl的版本,虽然没有测试过,但应该基本相同。 - andyrandy
@luschn $timeout 的初始化是用来干什么的?它好像没有被使用啊 O.o - jave.web
好问题,这里真的不需要,我会把它删除。谢谢提醒 :) - andyrandy
当然,这可能在您的服务器上有所不同。 - andyrandy
非常感谢,你节省了我的时间。方法一完美地运行了。投票支持。 - Sumith Harshan
显示剩余2条评论

0
$profile_Image = 'http://graph.facebook.com/shashankvaishnav/picture';
$userImage = $picturtmp_name . '.jpg'; // insert $userImage in db table field.
$savepath = '/images/';
insert_user_picture($savepath, $profile_Image, $userImage);

function insert_user_picture($path, $profile_Image, $userImage) {
  $thumb_image = file_get_contents($profile_Image);
  $thumb_file = $path . $userImage;
  file_put_contents($thumb_file, $thumb_image);
}

我认为这是获取API响应图像的大部分内容,所有函数都在allow_url_fopen = on(copy,fputs,file_put_contents)上工作,您可以使用curl获取图像,但要将其保存到文件夹中,您需要在php.ini中设置allow_url_fopen = on。 - Rakesh Sharma

0
$fbprofileimage = file_get_contents('http://graph.facebook.com/senthilbp/picture/'); 
file_put_contents('senthilbp.gif', $fbprofileimage);

这个不起作用,会给出非常大的错误信息 --> HTTP/1.1 302 Found Access-Control-Allow-Origin: * Cache-Control: private, no-cache, no-store, must-revalidate Content-Type: image/jpeg Expires: Sat, 01 Jan 2000 00:00:00 GMT Location: http://profile.ak.fbcdn.net/hprofile-ak-snc6/203228_100000407223160_1936571676_s.jpg Pragma: no-cache X-FB-Rev: 661961 X-FB-Debug: +t1EQ3gRio5a6XjuG0Q+uQnUlM+M78/2EZYBXkIsyV4= Date: Fri, 02 Nov 2012 11:08:19 GMT Connection: keep-alive Content-Length: 0 - Shashank M Vaishnav
将以下代码复制到一个 PHP 文件中,创建名为“fbimg”的文件夹并赋予该文件夹写权限。我提到了 facebookid -- 输入你的 Facebook ID 并在浏览器中运行,你的个人资料图片将会下载到“fbimg”文件夹中。 - senthilbp
我尝试了相同的方法,但是上面提到的文本出现了,而图像没有出现 :( - Shashank M Vaishnav
在我们的服务器上禁用了此文件获取内容功能...他们不允许我们这样做。 - Shashank M Vaishnav

0
$filename = 'abcd.jpg';    
$to = 'image/'.$filename;
if(copy($sourceFilePath,$to))
echo 'copied';
else
echo 'error';

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接