(ERROR) Trying to get property of non-object (Facebook PHP SDK) -


i trying echo name got error: trying property of non-object. please help. have problem getting user access token though have no problem getting app access token.

<!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, oauth code " . $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, 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 return data instead of printing 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> 

you trying echo $user->name json_decode not return object default. returns associative array unless pass in true second parameter.

so if change:

$user = json_decode(curl($graph_url));

to:

$user = json_decode(curl($graph_url), true);

it might work want to. if not, try looking @ curl() call returns , $user contains var_dump().


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -