1 Installation
Download Facebook API library for PHP from http://svn.facebook.com/svnroot/platform/clients/packages/facebook-platform.tar.gz
Here is example of how code is work:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | include_once(“facebook/facebook.php"); //path to the facebook library $facebook = new Facebook($api_key, secret_key); //api and secret key of your application which you get in chapter 1.1 $facebook->set_user($session_key, $secret_key, $sig) //session_key, secret_key and sig of user which you get after user registered your Facebook application in chapter 1.2 do { switch($data['type']) { case "post_photos": foreach($data['file'] as $file) { $res = $facebook->api_client->photos_upload($file['tmp_name'],null,$data['title'],$conf['userid']); } break; case "post_status": $res = $facebook->api_client->users_setStatus($data['message'],$conf['userid']); break; case "post_blog": $res = $facebook->api_client->notes_create($data['title'],$data['message'],$conf['userid']); break; case "post_microblog": $res = $facebook->api_client->stream_publish($data['message'],$data['attachment'],$data['links'],$conf['userid'],$conf['userid']); break; default: break; } } while(!$res); |
There is 4 types of posting on facebook, each of them has specific function
Posting photos:
$facebook->api_client->photos_upload($file['tmp_name'],null,$title,$user_id);
Posting status:
$facebook->api_client->users_setStatus($message,$user_id);
Posting blog:
$facebook->api_client->notes_create($title,$’message,$user_id);
Posting stream:
$facebook->api_client->stream_publish($message,$attachment,$links,$user_id_target,$userid_actor);
Attachments and link example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | $attachment = array( 'name' => 'ninja cat', 'href' => 'http://www.youtube.com/watch?v=muLIPWjks_M', 'caption' => '{*actor*} uploaded a video to www.youtube.com', 'description' => 'a sneaky cat', 'properties' => array('category' => array( 'text' => 'pets', 'href' => 'http://www.youtube.com/browse?s=mp&t=t&c=15'), 'ratings' => '5 stars'), 'media' => array(array('type' => 'video', 'video_src' => 'http://www.youtube.com/v/fzzjgBAaWZw&hl=en&fs=1', 'preview_img' => 'http://img.youtube.com/vi/muLIPWjks_M/default.jpg?h=100&w=200&sigh=__wsYqEz4uZUOvBIb8g-wljxpfc3Q=', 'video_link' => 'http://www.youtube.com/watch?v=muLIPWjks_M', 'video_title' => 'ninja cat'))); $links = array( array('text' => 'Upload a video', 'href' => 'http://www.youtube.com/my_videos_upload')); |
And one more thing. There can be trouble with connection to the Facebook API server so in example was used a loop which can repeat calling Facebook API until get an unswer from it.
Hope this helps.