无法从图API中检索任何数据

时间:2012-02-23 22:13:26

标签: php facebook-graph-api

您好我使用以下代码部分。对于背景,上一次工作我因为ssl相关问题而得到了Curl Exception 77。我的域名尚未启用HTTPS。有了这个小信息。我正在粘贴下面的代码部分。我正在使用一些打印件进行调试。 (我是PHP的新手,请耐心等待我)

<?php
    include_once('./php-sdk/facebook.php');
    include_once('./php-sdk/fbhelper.php');

    $config = array(
        'appId' => FACEBOOK_APP_ID,
        'secret' => FACEBOOK_SECRET,
    );

    try {
        $facebook = new Facebook($config);
    }
    catch (FacebookApiException $e) {
        echo 'Excetption is' . $e->__toString();
    }
?>

<?php

    $user_id = $facebook->getUser();

    if ($user_id) {
        echo "user exists and is: " .$user_id;
    } else {
        echo "user DONT exists";
    }

    // If i comment out this next try catch block for $session, then my page renders
    // fine, but when I am using this code block, then my page doesn't render beyond 
    // this code section. This is very strange. Why not an exception is thrown if there
    // are any issues? Can anybody help me understand this?

    try {
        $session = $facebook->getSession();

        if ($session) {
            echo "user exists and is: " . $session;
        } else {
            echo "user DONT exists";
        }
    } catch (FacebookApiException $e) {
        print_r($e);
    }

    // now when I comment out above $session related part, then page renders fine, but 
    // no where any prints show me anything. Why the name is not getting reflected?
    if ($user_id) {
        try {
            $user_profile = $facebook->api('/727850431', 'GET');
            echo 'Name: ' . $user_profile['name'];
        } catch (FacebookApiException $e) {
            print_r($e);
            $login_url = $facebook->getLoginUrl();
            echo 'Please <a href="' . $login_url . '">login.</a>';
        }
    } else {
        $login_url = $facebook->getLoginUrl();
        echo 'Please <a href="' . $login_url . '">login.</a>';
    }

    try {
        $url = 'https://graph.facebook.com/oauth/access_token?client_id=FACEBOOK_APP_ID&client_secret=FACEBOOK_SECRET& grant_type=client_credentials';
        $app_access_token = json_decode(file_get_contents($url));

        $graph_url = "https://graph.facebook.com/me?access_token=" . $app_access_token;
        $result = json_decode(file_get_contents($graph_url));
        print_r($result);
    } catch (FacebookApiException $e) {
        print_r($e);
    }

?>

0 个答案:

没有答案