简体   繁体   English

Facebook Graph API,PHP SDK:如何回显用户帖子?

[英]Facebook Graph API, PHP SDK: How to echo out User posts?

I'm trying to use the Facebook SDK PHP edition. 我正在尝试使用Facebook SDK PHP版本。 I've been using this resource to get everything I have done completed: https://developers.facebook.com/docs/php/gettingstarted 我一直在使用此资源来完成我已经完成的所有事情: https : //developers.facebook.com/docs/php/gettingstarted

<?php
    require_once '../vendor/autoload.php';
    $fb = new Facebook\Facebook([
        'app_id' => 'MYID',
        'app_secret' => 'MYSECRET',
        'default_graph_version' => 'v2.5',
    ]);
    $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
    try {
        $response = $fb->get('/me');
        $userNode = $response->getGraphUser();
    }
    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;
    }
    echo 'Logged in as ' . $userNode->getName();
?>

As far as this goes, everything works and spits out "Logged in as MY NAME" 就目前而言,一切正常,并弹出“以我的名字登录”

What if I were to want to echo out all the post information for a particular ID. 如果我想回显所有特定ID的帖子信息怎么办。 Say for example, https://www.facebook.com/EuropeanCommission 举例来说, https://www.facebook.com/EuropeanCommission

and I wanted to change my code on the 10th line to be: 我想将第10行的代码更改为:

$response = $fb->get('/EuropeanCommission/posts');

What else would I have to put and what would the echo statement be to echo out all the posts for this "page"? 我还要输入什么,echo语句将回显此“页面”的所有帖子? Is my get statement even correct to use in line with https://developers.facebook.com/tools/explorer?method=GET&path=EuropeanCommission%2Fposts&version=v2.6 我的get语句是否正确使用以符合https://developers.facebook.com/tools/explorer?method=GET&path=EuropeanCommission%2Fposts&version=v2.6

just use print_r or var_dump to echo the data you want to. 只需使用print_r或var_dump即可回显您想要的数据。

in your case: 在您的情况下:

var_dump($userNode); var_dump($ userNode);

or 要么

print_r($userNode); print_r($ userNode);

This will print an array but if you want to print it in json. 这将打印一个数组,但是如果要在json中打印它。

echo json_encode($userNode); 回声json_encode($ userNode);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM