如何获取Facebook用户ID,更改后在用户墙上发布图片?

时间:2015-03-23 19:26:50

标签: php facebook facebook-graph-api sdk permissions

多年前,我已经为 Facebook 开发了应用程序,但现在似乎没有使用旧方法来获取用户的信息(ID,名称等)并在用户上发布数据&# 39; s。墙。

我已经这样使用了,但是现在它给我留下了空白屏幕:

require_once('FacebookAPI/Facebook.php');

$facebook = new Facebook(array(
        'appId'  => 'xxxxx',
        'secret' => 'xxxxx',
    ));
    $user = $facebook->getUser();
    if ($user) 
    {
        try 
        {
            #Code below to post data on user's wall
            # Photo Caption
            $photoCaption = 'Just used app';
            $imageUrl = ('http://example.com'); // Example URL
            $link = ('http://www.facebook.com/page/app_xxxx?ref=ts');

            # Post Data for Photos API
            $post_data = array(
              'message' => $photoCaption,
              'link' => $link,
              'caption'   => 'Caption text'
            );
            $apiResponse = $facebook->api('/me/feed', 'POST', $post_data);
            ?>
            #Code below to show app  
            <html>
            <body bgcolor="#ffffff">
            <center>
               // link to my app
            </center>
            </body>
            </html>
            // Code below to get user's information (name, id)
            <?php
            $user_profile = $facebook->api('/me');
            $coded = $_REQUEST['code'];
            $name = $user_profile['name'];
            $id = $user_profile['id'];
            session_start();
            $_SESSION['name'] = $name;
            $_SESSION['id'] = $id;
        } 
        catch (FacebookApiException $e) 
        {
            $user = null;
            error_log($e);
        }
    } 
    else 
    {   # Code below to get access from user
        $redirectUri = 'http://www.facebook.com/mypage/app_xxxx?ref=ts';
        $loginUrl = $facebook->getLoginUrl( array(
            'scope' => 'publish_stream,photo_upload',
            'redirect_uri' => $redirectUri
        ));
        echo("<script>top.location.href = '" . $loginUrl . "';</script>");
    }

有人可以帮助我成功获取用户的名字吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用此代码。希望你已经下载了最新的php-sdk,你有appid,app des

 include_once "php-sdk/src/facebook.php";
 $app_id = 'your app id';
 $facebook = new Facebook(array(
  'appId'  => 'your app id',
  'secret' => 'your app secret'
 ));

 $user = $facebook->getUser();
 if ($user) {
      $me = $facebook->api('/me');
      $name= $me['name'];
      $fid=$me['id'];
 }
 else
 {
       $loginUrl = "http://www.facebook.com/dialog/oauth?client_id=" . 
       $app_id . "&redirect_uri=" . urlencode($app_url) . "&scope=email";
       echo("<script> top.location.href='" . $loginUrl  . "'</script>");
 }