เทคนิค php ดึงข้อมูล comment จาก facebook มาใช้งาน

เขียนเมื่อ 13 ปีก่อน โดย Ninenik Narkdee
comment facebook php

คำสั่ง การ กำหนด รูปแบบ ตัวอย่าง เทคนิค ลูกเล่น การประยุกต์ การใช้งาน เกี่ยวกับ comment facebook php





ตัวอย่างโค้ดต่อไปนี้เป็นเทคนิคการ ดึงข้อมูลจาก 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 ก่อนเ ถึงจะสามารถดึงข้อมูลได้
 



กด Like หรือ Share เป็นกำลังใจ ให้มีบทความใหม่ๆ เรื่อยๆ น่ะครับ











URL สำหรับอ้างอิง











เว็บไซต์ของเราให้บริการเนื้อหาบทความสำหรับนักพัฒนา โดยพึ่งพารายได้เล็กน้อยจากการแสดงโฆษณา โปรดสนับสนุนเว็บไซต์ของเราด้วยการปิดการใช้งานตัวปิดกั้นโฆษณา (Disable Ads Blocker) ขอบคุณครับ