เทคนิค php ดึงข้อมูล comment จาก facebook มาใช้งาน
07 July 2010ตัวอย่างโค้ดต่อไปนี้เป็นเทคนิคการ ดึงข้อมูลจาก facebook มาใช้งาน โดยสามารถกำหนดรูปแบบ ข้อมูลที่ต้องการ ได้ทั้งไฟล์ xml หรือ json
ซึ่งในที่นี้จะเป็นการดึงข้อมูลเป็น xml แล้วใช้ฟังก์ชัน php แปลงเป็น array ไว้ใช้งาน
อ่านวิธีการใช้ฟังก์ชัน php แปลง xml เป็น array
http://www.ninenik.com/ดึงค่า_ข้อมูล_จาก_xml_ไฟล์_มากำหนดเป็นตัวแปร_array_ด้วย_php-281.html
อ่านวิธี สร้าง comment ด้วย social plugins ใน facebook api
http://www.ninenik.com/สร้าง_comment_ด้วย_social_plugins_ใน_facebook_api_อย่างง่ายดาย-291.html
โค้ดตัวอย่าง
<?php
function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
$arrData = array();
// if input is object, convert into array
if (is_object($arrObjData)) {
$arrObjData = get_object_vars($arrObjData);
}
if (is_array($arrObjData)) {
foreach ($arrObjData as $index => $value) {
if (is_object($value) || is_array($value)) {
$value = objectsIntoArray($value, $arrSkipIndices); // recursive call
}
if (in_array($index, $arrSkipIndices)) {
continue;
}
$arrData[$index] = $value;
}
}
return $arrData;
}
$typeFormat="xml"; // xml or json
$commentXID="0fdd585c77c41e220b47b99a2760fde8"; // xid ของ fb:comment
$api_keyFB="134358299911812"; // api key
$fbs_full="fbs_".$api_keyFB;
$fbs_token=explode("&",$_COOKIE[$fbs_full]);
$fbs_access_token=substr($fbs_token[0],2);
$commentURL="https://api.facebook.com/method/comments.get?xid=";
$commentURL.=$commentXID."&";
$commentURL.=$fbs_access_token."&";
$commentURL.="format=".$typeFormat;
// การใช้งาน
$xmlUrl = $commentURL; // XML ไฟล์
$xmlStr = file_get_contents($xmlUrl);
$xmlObj = simplexml_load_string($xmlStr); // สร้างเป็น xml object
$arrXml = objectsIntoArray($xmlObj); // แปลงค่า xml object เป็นตัวแปร array ใน php
// การนำค่าไปใช้
echo "<pre>";
print_r($arrXml); // เพื่อดูโครงสร้างของตัวแปร array
echo "</pre>";
?>
นอกจากโค้ดข้างต้น สามารถใช้คำสั่ง fql คล้าย sql เพื่อดึงข้อมูลมาใช้งานได้เช่นกัน ตัวอย่าง
<?php
function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
$arrData = array();
// if input is object, convert into array
if (is_object($arrObjData)) {
$arrObjData = get_object_vars($arrObjData);
}
if (is_array($arrObjData)) {
foreach ($arrObjData as $index => $value) {
if (is_object($value) || is_array($value)) {
$value = objectsIntoArray($value, $arrSkipIndices); // recursive call
}
if (in_array($index, $arrSkipIndices)) {
continue;
}
$arrData[$index] = $value;
}
}
return $arrData;
}
$typeFormat="xml"; // xml or json
$commentXID="0fdd585c77c41e220b47b99a2760fde8"; // xid ของ fb:comment
$api_keyFB="134358299911812"; // api key
// field ของตาราง comment ดูได้ที่
// http://developers.facebook.com/docs/reference/fql/comment
$fql=urlencode("SELECT text FROM comment WHERE xid='".$commentXID."'"); // fql คล้าย sql
$fbs_full="fbs_".$api_keyFB;
$fbs_token=explode("&",$_COOKIE[$fbs_full]);
$fbs_access_token=substr($fbs_token[0],2);
$commentURL="https://api.facebook.com/method/fql.query?query=";
$commentURL.=$fql."&";
$commentURL.=$fbs_access_token."&";
$commentURL.="format=".$typeFormat;
// การใช้งาน
$xmlUrl = $commentURL; // XML ไฟล์
$xmlStr = file_get_contents($xmlUrl);
$xmlObj = simplexml_load_string($xmlStr); // สร้างเป็น xml object
$arrXml = objectsIntoArray($xmlObj); // แปลงค่า xml object เป็นตัวแปร array ใน php
// การนำค่าไปใช้
echo "<pre>";
print_r($arrXml); // เพื่อดูโครงสร้างของตัวแปร array
echo "</pre>";
?>
สามารถประยุกต์ใช้กับการใช้งานส่วนอื่นๆ ได้ เช่น การดึงรายชื่อเพื่อน ใน facebook , การดึงรูปใน album
เป็นต้น
รวมทั้งสามารถนำค่าที่ได้ไปบันทึกในฐานข้อมูล หรือนำมาประยุกต์แสดงข้อมูลในรูปแบบที่ต้องการ
หมายเหตุ: การจะเข้าถึงข้อมูลได้ จำเป็นต้อง login เข้าระบบ facebook ก่อนเ ถึงจะสามารถดึงข้อมูลได้
ลองดูเนื้อหาเพิ่มเติมได้ที่ คลิก
บทความคนเข้าอ่านวันนี้
22 Aug 09 เรียกใช้งาน jQuery ผ่าน Google API อ่าน 3889 20 Oct 10 สร้าง video playlist ของ jwplayer jquery plugin ด้วย xml และการใช้งาน อ่าน 2598 22 Sep 11 แนวทาง การเลื่อนแล้ว fixed ตำแหน่งเนื้อหาที่ต้องการ ด้วย jQuery อ่าน 1105 13 Oct 08 คำสั่ง SQL LIKE อ่าน 4497 21 Dec 10 กำหนด ปุ่ม ให้ทำงาน เมื่อคลิกที่ checkbox ยอมรับเงื่อนไข ด้วย jQuery อ่าน 2204 25 Sep 08 การจัดรูปแบบทศนิยมของตัวเลขใน javascript อ่าน 5635 26 Oct 08 แสดง popup window ตรงกลาง อ่าน 10183 05 Apr 10 กำหนด link ลิ้งค์ ทั้งหมด ให้ เปิดหน้าต่างใหม่ ด้วย jQuery อย่างง่าย อ่าน 2351 19 Jul 10 การนำ ระบบสมาชิก ของ facebook มาประยุกต์ใช้ อ่าน 5415 25 Sep 08 มารู้จัก effect ของ jquery อย่างง่าย ตอนที่ 3 อ่าน 3733 22 Feb 11 ตัวอย่าง การสร้างรูปแบบ ตารางรายงานข้อมูล ด้วย css อย่างง่าย อ่าน 2171 21 Oct 10 ประยุกต์ ใช้ jQuery สร้างข้อความเลื่อนขึ้น เลื่อนลง คล้าย marquee แนวตั้ง อ่าน 2386 25 Sep 08 การกำหนด selectors ด้วย jQuery แบบตัวกรอง Attribute อ่าน 2750 25 Sep 08 สิ่งที่เรียกว่า Selects, Properties , and Values สิ่งที่เป็นองค์ประกอบของ CSS อ่าน 2833 04 Jan 10 ดึงไฟล์ xml (rss) ไฟล์แบบ cross domain มาแสดงด้วย ajax ใน jQuery อย่างง่าย อ่าน 3641 16 Jul 11 แทรก +1 button ในเว็บจาก google plus อ่าน 927 15 Nov 09 สร้างลิ้งค์เมนู 2 ภาษา กรณี ไทย อังกฤษ ด้วย php อย่างง่าย อ่าน 1860 21 Mar 11 การสร้างปุ่ม facebook ล็อกอิน เว็บไซต์ แบบแสดงรูป และกำหนดเพิ่มเติม อ่าน 2588 16 Dec 11 ใช้ jQuery กับ CSS สร้างเมนูย่อย แนวนอน รูปแบบคล้ายแท็บเมนู อ่าน 1307 18 Mar 09 สร้างฟังก์ชัน PHP กำหนดเวลาหมดอายุให้ัตัวแปร SESSION อย่างง่าย อ่าน 3680
