简体   繁体   中英

PHP Facebook SDK

Hi i need help with a small error i received

Warning: Illegal string offset 'name' in C:\\xampp\\htdocs\\facebooktest.php on line 41 You are logged into centralrp using your facebook account! [1] when im tring to show the name

<?php
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require_once('php-sdk/facebook.php');

$config = array(
'appId' => '*',
'secret' => '*',
'allowSignedRequest' => false // optional but should be set to false for noncanvas apps
);

$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>

<?php
if($user_id) {

  // We have a user ID, so probably a logged in user.
  // If not, we'll get an exception, which we handle below.
  try {
  if (!isset($_SESSION['user']['facebookposted']))
  {
    $ret_obj = $facebook->api('/me/feed', 'POST',
                                array(
                                  'link' => 'app.com',
                                  'message' => 'Just logged into App with      Facebook.'
                             ));
    echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
    $_SESSION['user']['facebookposted'] = true;
    //ADD THE LOGIN STUFF HERE LATER

    }
    else
    {
        $_SESSON['user']['loggedin'] = true;

        echo 'You are logged into centralrp using your facebook account! ['.$user_id['name'].']';
    }


    // Give the user a logout link 
    echo '<br /><a href="' . $facebook->getLogoutUrl() . '">logout</a>';
  } catch(FacebookApiException $e) {
    // If the user is logged out, you can have a 
    // user ID even though the access token is invalid.
    // In this case, we'll get an exception, so we'll
    // just ask the user to login again here.
    $login_url = $facebook->getLoginUrl( array(
                   'scope' => 'publish_stream'
                   )); 
    echo 'Please <a href="' . $login_url . '">login.</a>';
    error_log($e->getType());
    error_log($e->getMessage());
  }   
} else {

  // No user, so print a link for the user to login
  // To post to a user's wall, we need publish_stream permission
  // We'll use the current URL as the redirect_uri, so we don't
  // need to specify it here.
  $login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
  echo 'Please <a href="' . $login_url . '">login.</a>';

} 

?>      

There is no name key in $user_id because $user_id is not an array.

if you need the name, get it like this:

try {
    $user_info = $facebook->api('/me/');
    echo '<pre>Name: ' . $user_info['name'] . '</pre>';
} catch(FacebookApiException $e) {
    echo 'There was an error';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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