从php脚本中检索Facebook照片

时间:2011-05-18 11:11:52

标签: php facebook

我想在php中开发一个应用程序,我可以在我的Facebook个人资料(或我的所有照片)中链接特定的相册,以便了解每张照片的直接网址链接。

这个想法是制作一个php脚本,按照时间顺序显示我的Facebook照片就像一个演示文稿。我是PHP程序员,但我对Facebook集成API一无所知。所以,如果你能告诉我如何做到这一点,那将是件好事。对不起我的英语不好。谢谢!

1 个答案:

答案 0 :(得分:0)

这是一个用于检索特定用户个人资料图片(PHP)的类,您可以从中获得创建所需内容的想法:

<?php

class FBPicture {
    public $uid;
    public $dir;
    public $type = 'large';

    public function setUId ($id) {
        $this->uid = $id;
    }

    public function setDir ($dir) {
        $this->dir = $dir;
    }

    public function fetch ($_uid=null, $_dir=null, $_type=null) {
        if ($_uid === null)
            throw new CHttpException ('Facebook User ID or Username is not set.');
        if ($_dir === null)
            $_dir = '/storage/';
        if ($_type === null)
            $_type = 'large';

        $this->uid = $_uid;
        $this->dir = $_dir;
        $this->type = $_type;

        $dir = getcwd () . $this->dir;
        $type = $this->type;

        // request URI
        $host = 'http://graph.facebook.com/' . $_uid;
        $request = '/picture';
        $type = '?type=' . $type;

        $contents = file_get_contents ($host.$request.$type);

        // create the file (check existance);
        $file = 'fb_' . $uid . '_' . rand (0, 9999);
        $ext = '.jpg';

        if (file_exists ($dir)) {
            if (file_exists ($dir.$file.$ext)) {
                $file .= $dir.$file.'2'.$ext;

                if ($this->appendToFile ($file, $contents)) {
                    return str_replace ($dir, '', $file);
                }
            } else {
                $file = $dir.$file.$ext;
                touch ($file);

                if ($this->appendToFile ($file, $contents)) {
                    return str_replace ($dir, '', $file);
                }
            }
        } else {
            // false is returned if directory doesn't exist
            return false;
        }
    }

    private function appendToFile ($file, $contents) {
        $fp = fopen ($file, 'w');
        $retVal = fwrite ($fp, $contents);
        fclose ($fp);

        return $retVal;
    }
}

// sample usage:
// $pic will be the filename of the saved file...
$pic = FBPicture::fetch('zuck', 'uploads/', 'large'); // get a large photo of Mark Zuckerberg, and save it in the "uploads/" directory
// this will request the graph url: http://graph.facebook.com/zuck/picture?type=large

?>