2

私は自分の名前をエコーし​​ようとしていますが、エラーが発生しました: 非オブジェクトのプロパティを取得しようとしています。助けてください。アプリアクセストークンの取得に問題はありませんが、ユーザーアクセストークンの取得にも問題があります。

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title></title>
</head>
<body>
<?php 

require_once('facebook.php');
$config = array();
$config['appId'] = "MY_APP_ID";
$config['secret'] ="MY_APP_SECRET";
$facebook = new Facebook($config);

$app_id = "MY_APP_ID";
$app_secret = "MY_APP_SECRET";
$my_url = "http://localhost:8080/samplewebsite/index.php/";



$code = $_REQUEST['code'];

if(empty($code))
{
    $_SESSION['state'] = md5(uniqid(rand(),TRUE));
    $dialog_url = "https://www.facebook.com/dialog/oauth?client_id="
               . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state=" . $_SESSION['state']
               . "&scope=publish_actions,read_stream";
    header("Location: " . $dialog_url);
}
else 
{
    echo "Hello, your OAuth code is " . $code . "<br>";           
}

if ($_REQUEST['state'] == $_SESSION['state'])
{
    $facebook->api('oauth/access_token',array(
       'client_id' => $app_id,
       'client_secret' => $app_secret,
       'type' => 'client_cred',
       'code' => $code,
    ));

    $token = $facebook->getAccessToken();
    echo "<br>" . "Hello, Your access_token is: " .  $token;


    $graph_url = "https://graph.facebook.com/me?access_token=" . $token;
    //$user = json_decode(file_get_contents($graph_url));
    //echo $user->name;

    function curl($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
        curl_setopt($ch, CURLOPT_URL, $url);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;

    }
    $user = json_decode(curl($graph_url));
    echo $user->name;
}




?>
</body>
</html>
4

1 に答える 1