从Facebook获得类似数据

时间:2019-07-10 16:21:12

标签: php facebook facebook-graph-api facebook-php-sdk

我需要编写一个php程序来从Facebook获取数据,由于我是新手,所以我现在就在自己的Facebook上尝试一下。无论如何,我在网上找到了一个使用PHP SDK v5来获取此数据的教程,并且我正在尝试实现他们用于从Facebook获取喜欢/数据/帖子/照片信息的一些代码。但是我对该程序的某些部分有些困惑。该程序的一部分是登录到我的网站,但是我没有网站或类似的网站,我只需要访问该程序即可访问我的Facebook并返回所有类似的数据,依此类推。我可能对此不了解,或者这可能不是正确的方法?也许我放置网站的那一部分是我放置Facebook URL的位置,因为我根本没有在程序中放置Facebook URL。

<?php
session_start();
require_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk
$fb = new Facebook\Facebook([
  'app_id' => '610350886153182',
  'app_secret' => '812a753d0cc8daeb843a2f07d97d6d50',
  'default_graph_version' => 'v3.3'
  ]);
$helper = $fb->getRedirectLoginHelper();
// app directory could be anything but website URL must match the URL given in the developers.facebook.com/apps
define('APP_URL', 'http://sohaibilyas.com/fbapp/'); // RIGHT HERE, DO NOT KNOW WHAT THIS IS FOR
$permissions = ['user_posts', 'user_photos']; // optional
try {
    if (isset($_SESSION['facebook_access_token'])) {
        $accessToken = $_SESSION['facebook_access_token'];
    } else {
        $accessToken = $helper->getAccessToken();
    }
} catch(Facebook\Exceptions\FacebookResponseException $e) {
    // When Graph returns an error
    echo 'Graph returned an error: ' . $e->getMessage();
    exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
    // When validation fails or other local issues
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    exit;
 }
if (isset($accessToken)) {
    if (isset($_SESSION['facebook_access_token'])) {
        $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
    } else {
        // getting short-lived access token
        $_SESSION['facebook_access_token'] = (string) $accessToken;
        // OAuth 2.0 client handler
        $oAuth2Client = $fb->getOAuth2Client();
        // Exchanges a short-lived access token for a long-lived one
        $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
        $_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
        // setting default access token to be used in script
        $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
    }
    // redirect the user back to the same page if it has "code" GET variable
    if (isset($_GET['code'])) {
        header('Location: ./');
    }
    // validating user access token
    try {
        $user = $fb->get('/me');
        $user = $user->getGraphNode()->asArray();
    } catch(Facebook\Exceptions\FacebookResponseException $e) {
        // When Graph returns an error
        echo 'Graph returned an error: ' . $e->getMessage();
        session_destroy();
        // if access token is invalid or expired you can simply redirect to login page using header() function
        exit;
    } catch(Facebook\Exceptions\FacebookSDKException $e) {
        // When validation fails or other local issues
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
    }
    // getting likes data of recent 100 posts by user
    $getPostsLikes = $fb->get('/me/posts?fields=likes.limit(1000){name,id}&limit=100');
    $getPostsLikes = $getPostsLikes->getGraphEdge()->asArray();
    // printing likes data as per requirements
    foreach ($getPostsLikes as $key) {
        if (isset($key['likes'])) {
            echo count($key['likes']) . '<br>';
            foreach ($key['likes'] as $key) {
                echo $key['name'] . '<br>';
            }
        }
    }
    // getting likes data of recent 100 photos by user
    $getPhotosLikes = $fb->get('/me/photos?fields=likes.limit(1000){name,id}&limit=100&type=uploaded');
    $getPhotosLikes = $getPhotosLikes->getGraphEdge()->asArray();
    // printing likes data as per requirements
    foreach ($getPhotosLikes as $key) {
        if (isset($key['likes'])) {
            echo count($key['likes']) . '<br>';
            foreach ($key['likes'] as $key) {
                echo $key['name'] . '<br>';
            }
        }
    }
    // Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']
} else {
    // replace your website URL same as added in the developers.facebook.com/apps e.g. if you used http instead of https and you used non-www version or www version of your website then you must add the same here
//ALSO HERE, SOMETHING ABOUT LOGGING IN TO WEBSITE BUT I DO NOT KNOW WHAT THAT IS NECESSARY
    $loginUrl = $helper->getLoginUrl(APP_URL, $permissions);
    echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>';

1 个答案:

答案 0 :(得分:1)

该URL是您实施FB登录的URL。这些代码使用了重定向帮助器,这意味着它不会本身呈现oAuth URL,而是使用“登录”按钮/ JS SDK。

请参见https://developers.facebook.com/docs/php/FacebookRedirectLoginHelper/5.0.0或此处的官方示例https://developers.facebook.com/docs/php/howto/example_access_token_from_javascript