【问题标题】:How to get Facebook user ID, post images on user's wall after changes?如何获取 Facebook 用户 ID,更改后在用户墙上发布图片?
【发布时间】:2015-05-26 22:37:34
【问题描述】:

几年前我为 Facebook 开发了应用程序,但现在似乎无法使用旧方法来获取用户信息(id、姓名等)并将数据发布到用户的墙上。

我曾经这样使用过,但是现在它返回给我空白屏幕:

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>");
    }

有人可以帮我成功获取用户名,id吗?

【问题讨论】:

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


【解决方案1】:

你可以试试这个代码。希望你已经下载了最新的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>");
 }

【讨论】:

  • 那不是使用最新的SDK。
  • 非常好,因为现在我只从 PHP 5.4 和我的服务器使用 5.3 读取了新的 SDK。所以我没有机会使用新的 SDK。去检查。
  • 使用你的代码我得到了错误:The parameter app_id is required
  • 你把你的 app_id , $app_id = 'your app id'
  • 真的,该睡觉了,错过了类似的东西......现在奇怪的事情发生了,弹出带有Give permissions的窗口,我给了它,它开始重新加载页面。就像一个循环重新加载。也许是因为我设置了$app_url = 'http://www.facebook.com/myPage/app_xxxxxxx?ref=ts'; 不应该在我授予权限后显示我的应用程序(来自if ($user) { ... link to my app } 部分)?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-05
  • 1970-01-01
  • 2011-08-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多