Facebook api PHP 及FQL query 抓使用者資訊欄位值
在這裡簡單介紹如何用PHP抓取Facebook API的使用者資訊欄位值(如:Name, Email, Profile Picture, Locale, Timezone, Total Friends等等)首先,必須新建一個Facebook APP建立完成後會有APPID APP secret(金鑰)http://cdn1.4rapiddev.com/wp-content/uploads/2011/09/Facebook-Find-App-ID-and-App-Secret-300x271.jpg Facebook Find App ID and App Secret
如此才可以開始利用Facebook 開始抓Facebook 使用者登入資訊
<?php
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'appId',
'secret' => 'secret',
));
$user_id = $facebook->getUser();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<div style="font-size:13px; color:#333333; line-height:24px;">
<?php
if($user_id) {
try {
$user_profile = $facebook->api('/me','GET');
echo "<h1>1. User profile returned from Graph API</h1>";
echo "Profile ID: " . $user_profile['id'] . "<br>";
echo "Name: " . $user_profile['name'] . "<br>";
echo "Email: " . $user_profile['email'] . "<br>";
echo "Locale: " . $user_profile['locale'] . "<br>";
echo "Timezone: " . $user_profile['timezone'] . "<br>";
echo "<h3>All information in the user_profile array</h3>";
echo "<pre>";
var_dump($user_profile);
echo "</pre>";
echo "<h1>2. User profile returned from FQL query</h1>";
$fql = 'SELECT name, username, pic_square, locale, email, friend_count from user where uid = ' . $user_id;
$ret_obj = $facebook->api(array(
'method' => 'fql.query',
'query' => $fql,
));
echo 'Name: ' . $ret_obj['name'] . '<br>';
echo 'Username: ' . $ret_obj['username'] . '<br>';
echo 'Profile picture: ' . $ret_obj['pic_square'] . '<br>';
echo 'Email: ' . $ret_obj['email'] . '<br>';
echo 'Locale: ' . $ret_obj['locale'] . '<br>';
echo 'Total friends: ' . $ret_obj['friend_count'] . '<br>';
} catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl(array(
'redirect_uri' => $facebook_app_url,
'scope' => "email,publish_stream,user_hometown,user_location,user_photos,friends_photos,
user_photo_video_tags,friends_photo_video_tags,user_videos,video_upload,friends_videos"));
echo "<script type='text/javascript'>top.location.href = '$login_url';</script>";
}
} else {
$login_url = $facebook->getLoginUrl(array(
'redirect_uri' => $facebook_app_url,
'scope' => "email,publish_stream,user_hometown,user_location,user_photos,friends_photos,
user_photo_video_tags,friends_photo_video_tags,user_videos,video_upload,friends_videos"));
echo "<script type='text/javascript'>top.location.href = '$login_url';</script>";
}
?>
</div>
</body>
</html>
$facebook = new Facebook(array(
'appId' => 'appId',
'secret' => 'secret',
));
這裡填入你的APPID及金鑰
$user_profile = $facebook->api('/me','GET');
echo "<h1>1. User profile returned from Graph API</h1>";
echo "Profile ID: " . $user_profile['id'] . "<br>";這裡很重要
Facebook預設$user_profile = $facebook->api('/me');
一定要加GET
不然無法讀取陣列資料
不是很明白
!
頁:
[1]