本文整理汇总了PHP中Facebook\Facebook::api方法的典型用法代码示例。如果您正苦于以下问题:PHP Facebook::api方法的具体用法?PHP Facebook::api怎么用?PHP Facebook::api使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Facebook\Facebook
的用法示例。
在下文中一共展示了Facebook::api方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: callback
public function callback()
{
$code = Input::get('code');
if (strlen($code) == 0) {
return \Redirect::to('/');
}
$facebook = new Facebook(\Config::get('facebook'));
$uid = $facebook->getUser();
if ($uid == 0) {
return \Redirect::to('/');
}
$me = $facebook->api('/me');
$profile = User::where('user_id', $uid)->first();
if (empty($profile)) {
$user = new User();
$user->username = $me['name'];
$user->photo = 'https://graph.facebook.com/' . $me['id'] . '/picture?type=large';
$user->user_id = $uid;
$user->save();
$x = new leaderboard();
$x->user_id = $uid;
$x->user_name = $me['name'];
$x->round_id = 1;
$x->save();
}
$user = User::where('user_id', $uid)->select('user_id', 'username', 'photo')->first();
session()->put(['user_id' => $uid, 'name' => $user['username']]);
return redirect('/');
}
示例2: Facebook
use Facebook\Facebook;
use Facebook\FacebookApiException;
require '../src/Facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array('appId' => '344617158898614', 'secret' => '6dc8ac871858b34798bc2488200e503d'));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$statusUrl = $facebook->getLoginStatusUrl();
$loginUrl = $facebook->getLoginUrl();
}
// This call will always work since we are fetching public data.
$naitik = $facebook->api('/naitik');
?>
示例3: Facebook
<?php
use Facebook\Facebook;
use Facebook\FacebookApiException;
require '../src/Facebook.php';
$facebook = new Facebook(array('appId' => '344617158898614', 'secret' => '6dc8ac871858b34798bc2488200e503d'));
// See if there is a user from a cookie
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
echo '<pre>' . htmlspecialchars(print_r($e, true)) . '</pre>';
$user = null;
}
}
?>
<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<body>
<?php
if ($user) {
?>
Your user profile is
<pre>
<?php
print htmlspecialchars(print_r($user_profile, true));
?>
</pre>
<?php
示例4: syncUser
/**
* Sync the user back to the linked Facebook account.
*
* <h3>Usage:</h3>
* <pre class="code">
* $auth->syncUser($_POST['access-token']);
* </pre>
*
* @param string $access_token A valid access token for the user to sync up.
*
* @return bool True or false on success.
*/
public function syncUser($access_token)
{
try {
$facebook = new \Facebook(['appId' => FACEBOOK_APP_ID, 'secret' => FACEBOOK_APP_SECRET]);
$facebook->setAccessToken($access_token);
/** @var array $user_profile The array of user data from Facebook */
$user_profile = $facebook->api('/me');
} catch (\Exception $e) {
return false;
}
$user = $this->_usermodel;
if (!$user->exists()) {
// Some config options for new accounts only.
$profiles = $user->get('external_profiles');
if (!is_array($profiles)) {
$profiles = [];
}
$profiles[] = [['type' => 'facebook', 'url' => $user_profile['link'], 'title' => 'Facebook Profile']];
$user->set('external_profiles', $profiles);
// Another component from the user-social component.
// This needs to be unique, so do a little fudging if necessary.
try {
$user->set('username', $user_profile['username']);
} catch (\ModelValidationException $e) {
$user->set('username', $user_profile['username'] . '-' . \Core\random_hex(3));
}
// Sync the user avatar.
$f = new \Core\Filestore\Backends\FileRemote('http://graph.facebook.com/' . $user_profile['id'] . '/picture?type=large');
$dest = \Core\Filestore\Factory::File('public/user/avatar/' . $f->getBaseFilename());
$f->copyTo($dest);
$user->set('avatar', 'public/user/avatar/' . $dest->getBaseFilename());
}
// Get all user configs and load in anything possible.
$user->set('first_name', $user_profile['first_name']);
$user->set('last_name', $user_profile['last_name']);
$user->set('gender', ucwords($user_profile['gender']));
$user->set('facebook_id', $user_profile['id']);
$user->set('facebook_link', $user_profile['link']);
$user->set('facebook_access_token', $facebook->getAccessToken());
}