douyan8772 2011-08-13 21:03
浏览 100
已采纳

在PHP中使用Facebook API

I am currently learning PHP and I want to start incorporating Facebook basic elements such as friend lists etc just to do something more fun than building a web shop. However, I have followed the guides on Facebook and added an app and I got the canvas to display what I want, but how do I start getting data from Facebook? I have downloaded this https://github.com/facebook/php-sdk#readme and I added it to my project.

For example, how would I ask for permissions, pull data from my own profile? Do I need to parse JSON? Where is the API for the PHP SDK? This http://developers.facebook.com/docs/reference/api/ does not look like the get user id part here:

require 'php-sdk/src/facebook.php';

$facebook = new Facebook(array(
  'appId'  => 'YOUR_APP_ID',
  'secret' => 'YOUR_APP_SECRET',
));

// Get User ID
$user = $facebook->getUser();

Any points in the right direction would be highly appreciated.

  • 写回答

2条回答 默认 最新

  • donglv7097 2011-08-13 21:10
    关注

    There is no need to parse JSON, you would have to go about doing an API request:

    try {
        $user_profile = $facebook->api('/me');
    } catch (FacebookApiException $fbException) {
        die('Houston, we got problems.');
        return;
    }
    

    This would leave you with either a $user_profile containing every single element you've previously requested permission for (such as first_name, etc), put into an associative array ($user_profile in this case). Obviously, you would first have to check for getUser()'s success or failure (if it returns 0, it failed), as the API call would behave as if you were logged out otherwise.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?