TShopping

 找回密碼
 註冊
搜索
查看: 3648|回復: 1

[教學] Facebook api PHP 及FQL query 抓使用者資訊欄位值

  [複製鏈接]
發表於 2013-2-1 19:14:12 | 顯示全部樓層 |閱讀模式
 
Push to Facebook Push to Plurk Push to Twitter 
在這裡簡單介紹如何用PHP抓取Facebook API的使用者資訊欄位值(如:Name, Email, Profile Picture, Locale, Timezone, Total Friends等等)

首先,必須新建一個Facebook APP
建立完成後會有APPID APP secret(金鑰)
Facebook Find App ID and App Secret

如此才可以開始利用Facebook 開始抓Facebook 使用者登入資訊


  1. <?php
  2.   require 'src/facebook.php';

  3.   $facebook = new Facebook(array(
  4.     'appId' => 'appId',
  5.     'secret' => 'secret',
  6.   ));

  7.   $user_id = $facebook->getUser();
  8. ?>
  9. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  10. <html xmlns="http://www.w3.org/1999/xhtml">
  11. <head>
  12. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  13. <title>Untitled Document</title>
  14. </head>

  15. <body>
  16. <div style="font-size:13px; color:#333333; line-height:24px;">
  17. <?php
  18.   if($user_id) {
  19.     try {
  20.       $user_profile = $facebook->api('/me','GET');
  21.       echo "<h1>1. User profile returned from Graph API</h1>";
  22.       echo "Profile ID: " . $user_profile['id'] . "<br>";
  23.       echo "Name: " . $user_profile['name'] . "<br>";
  24.       echo "Email: " . $user_profile['email'] . "<br>";
  25.       echo "Locale: " . $user_profile['locale'] . "<br>";
  26.       echo "Timezone: " . $user_profile['timezone'] . "<br>";

  27.       echo "<h3>All information in the user_profile array</h3>";
  28.       echo "<pre>";
  29.       var_dump($user_profile);
  30.       echo "</pre>";

  31.       echo "<h1>2. User profile returned from FQL query</h1>";

  32.       $fql = 'SELECT name, username, pic_square, locale, email, friend_count from user where uid = ' . $user_id;

  33.       $ret_obj = $facebook->api(array(
  34.         'method' => 'fql.query',
  35.         'query' => $fql,
  36.       ));

  37.       echo 'Name: ' . $ret_obj[0]['name'] . '<br>';
  38.       echo 'Username: ' . $ret_obj[0]['username'] . '<br>';
  39.       echo 'Profile picture: ' . $ret_obj[0]['pic_square'] . '<br>';
  40.       echo 'Email: ' . $ret_obj[0]['email'] . '<br>';
  41.       echo 'Locale: ' . $ret_obj[0]['locale'] . '<br>';
  42.       echo 'Total friends: ' . $ret_obj[0]['friend_count'] . '<br>';
  43.     } catch(FacebookApiException $e) {
  44.       $login_url = $facebook->getLoginUrl(array(
  45.       'redirect_uri' => $facebook_app_url,
  46.       'scope' => "email,publish_stream,user_hometown,user_location,user_photos,friends_photos,
  47.       user_photo_video_tags,friends_photo_video_tags,user_videos,video_upload,friends_videos"));

  48.       echo "<script type='text/javascript'>top.location.href = '$login_url';</script>";
  49.     }
  50.   } else {
  51.     $login_url = $facebook->getLoginUrl(array(
  52.     'redirect_uri' => $facebook_app_url,
  53.     'scope' => "email,publish_stream,user_hometown,user_location,user_photos,friends_photos,
  54.     user_photo_video_tags,friends_photo_video_tags,user_videos,video_upload,friends_videos"));

  55.     echo "<script type='text/javascript'>top.location.href = '$login_url';</script>";
  56.   }
  57. ?>
  58. </div>
  59. </body>
  60. </html>
複製代碼
  1. $facebook = new Facebook(array(
  2.   'appId' => 'appId',
  3.   'secret' => 'secret',
  4. ));
複製代碼
這裡填入你的APPID及金鑰

  1. $user_profile = $facebook->api('/me','GET');
  2. echo "<h1>1. User profile returned from Graph API</h1>";
  3. echo "Profile ID: " . $user_profile['id'] . "<br>";
複製代碼
這裡很重要
Facebook預設$user_profile = $facebook->api('/me');
一定要加GET
不然無法讀取陣列資料




 

臉書網友討論
發表於 2014-7-10 19:24:18 | 顯示全部樓層


  不是很明白

版主招募中

您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2016-12-6 06:47 , Processed in 0.056751 second(s), 21 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表