CREATE TABLE `fblogin` ( `id` int(10) NOT NULL auto_increment, `fb_id` int(20) NOT NULL, `name` varchar(300) NOT NULL, `email` varchar(300) NOT NULL, `image` varchar(600) NOT NULL, `postdate` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
โค๊ด:
###library.php --> ตั้งค่าเชื่อมต่อฐานข้อมูล <?php mysql_connect("localhost", "root", "password") or die ("Oops! Server not connected"); // Connect to the host mysql_select_db("db_facebook") or die ("Oops! DB not connected"); // select the database ?>
โค๊ด:
###index.php --> ไฟล์หน้าแรกคลิกเข้าสู่เข้า Loging <?php error_reporting(0); include 'library.php'; ?> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Asif18 tutorial about facebook login for mywebsite using php sdk</title> <script type="text/javascript"> window.fbAsyncInit = function() { FB.init({ appId : '1393348287579521', // replace your app id here channelUrl : '//http://localhost/test_code/facebook-login/', status : true, cookie : true, xfbml : true }); }; (function(d){ var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; if (d.getElementById(id)) {return;} js = d.createElement('script'); js.id = id; js.async = true; js.src = "//connect.facebook.net/en_US/all.js"; ref.parentNode.insertBefore(js, ref); }(document));
protected function getCode() { if (isset($_REQUEST['code'])) { if ($this->state !== null && isset($_REQUEST['state']) && $this->state === $_REQUEST['state']) {
// CSRF state has done its job, so clear it $this->state = null; $this->clearPersistentData('state'); return $_REQUEST['code']; } else { self::errorLog('CSRF state token does not match one provided.'); return false; } }
return false; }
นำโค๊ตนี้ไป copy ทับ
โค๊ด:
protected function getCode() { $server_info = array_merge($_GET, $_POST, $_COOKIE);
if (isset($server_info['code'])) { if ($this->state !== null && isset($server_info['state']) && $this->state === $server_info['state']) {
// CSRF state has done its job, so clear it $this->state = null; $this->clearPersistentData('state'); return $server_info['code']; } else { self::errorLog('CSRF state token does not match one provided.'); return false; } }
$albums = $this->facebook->api('/me/albums'); foreach($albums['data'] as $album){ // get all photos for album $photos = $this->facebook->api("/{$album['id']}/photos"); foreach($photos['data'] as $photo){ echo "<img src='{$photo['source']}' />", "<br />"; } }
ดึงข้อความที่เรา Post ทั้งหมดจาก Facebook มาแสดง
โค๊ด:
// show statuses $statuses = $this->facebook->api('/me/statuses'); foreach($statuses['data'] as $status){ echo $status["message"], "<br />"; }
เขียนโปรแกรมให้ Post ข้อความไป Facebook
โค๊ด:
$status = $this->facebook->api('/me/feed', 'POST', array('message' => 'ข้อความที่ต้องการ Post เข้าไปที่ Facebook')); var_dump($status); echo 'This is working';