Retrieving Facebook photos from a php script
I want to develop an app in php that I can link with a particular photo album in my Facebook profile (or with all my photos) in order to know the direct url link of each photo.
The idea is 开发者_C百科to make an php script who shows in chronological order my facebook photos like a presentation. Im php programmer, but I know nothing about Facebook integration API. So guys if you can suggest me ways to do this it will be nice. Sorry for my English. Thanks!
here is a class for retriving specific user profile pictures (PHP), you'll get the idea from it to create what you want:
<?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
?>
精彩评论