FACEBOOK GRAPH / rest api:如何登录我的OWN USER使用PHP更新我的STATUS [英] FACEBOOK GRAPH/rest api: how to LOGIN my OWN USER to update my STATUS with PHP

查看:113
本文介绍了FACEBOOK GRAPH / rest api:如何登录我的OWN USER使用PHP更新我的STATUS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Facebook图形API来帮助PHP更新FAN-PAGE的状态。 google说:不行。



现在我想用PHP更新自己的用户状态。我的主要问题是如何登录自己的用户到图形API(使用PHP),而不使用浏览器,没有有趣的PHP解决方法。

解决方案>

您需要几件事来更新您的Facebook个人资料或页面的Feed:Facebook应用程序( client_id client_secret ), profile_id access_token (publish_stream,manage_pages,offline_access权限)



您需要offline_access,因为如果没有,则访问令牌将过期。如果你已经读过你不需要offline_access,如果你已经发布了publish_stream,那么它们只是意味着你不需要它。



发布一篇文章很简单:

  $ data = array(
'access_token'=> $ access_token,
'消息'=>'状态消息',
);
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ data);
curl_setopt($ ch,CURLOPT_URL,https://graph.facebook.com/{$profile_id}/feed);

现在,如何获取 profile_id access_token ,您可以使用我的应用程序后熊猫,或制作自己的脚本。我会在这里加入:

 #arvin castro 
#http://codecri.me/
#2011年1月16日

$ client_id =''; #application id
$ client_secret =''; #application secret
$ callbackURL ='http://'; #这个脚本的URL
$ extendedPermissions ='publish_stream,manage_pages,offline_access';

session_name('facebookoauth');
session_start();

if(isset($ _ GET ['logout'])和$ _SESSION ['loggedin']){
$ _SESSION = array();
session_destroy();
}

if(isset($ _ GET ['signin'])){

#步骤1:将用户重定向到Facebook,为我们的应用程序授予许可
$ url ='https://graph.facebook.com/oauth/authorize?'。 xhttp :: toQueryString(array(
'client_id'=> $ client_id,
'redirect_uri'=> $ callbackURL,
'scope'=> $ extendedPermissions,
));
header(Location:$ url,303);
die();
}

if(isset($ _ GET ['code'])){

#步骤2:交换我们有一个访问令牌的代码
$ data = array();
$ data ['get'] = array(
'client_id'=> $ client_id,
'client_secret'=> $ client_secret,
'code'=> $ _GET ['code'],
'redirect_uri'=> $ callbackURL,
);

$ response = xhttp :: fetch('https://graph.facebook.com/oauth/access_token',$ data);

if($ response ['success']){

$ var = xhttp :: toQueryArray($ response ['body']);
$ _SESSION ['access_token'] = $ var ['access_token'];
$ _SESSION ['loggedin'] = true;

} else {
print_r($ response ['body']);
}
}

if($ _ SESSION ['loggedin']){
//获取配置文件ID
$ data = array();
$ data ['get'] = array(
'access_token'=> $ _SESSION ['access_token'],
'fields'=>'id,name,accounts'
);
$ response = xhttp :: fetch('https://graph.facebook.com/me',$ data);
echo'< pre>';
print_r(json_decode($ response ['body'],true));
echo'< / pre>';

} else {
echo'< a href =?signin>使用Facebook登录< / a>';
}

?>

我正在使用我的cURL包装类, xhttp


I want to update the status of a FAN-PAGE by PHP with the help of the Facebook graph api. google says: doesn't work.

Now I want to update my own user status by PHP. My main problem is how to login my own user to the graph api (with PHP), without using a browser and without funny PHP workarounds.

解决方案

You need several things to update your facebook profile or a page's feed: a facebook application (client_id, client_secret), profile_id, and access_token (publish_stream, manage_pages, offline_access permissions)

You need offline_access because if not, then the access token will expire. If you've read that you don't need offline_access if you already have publish_stream specified, they just meant that you don't need it always.

To publish a post is easy:

$data = array(
    'access_token' => $access_token,
    'message' => 'status message',
    );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/{$profile_id}/feed");

Now how to get the profile_id and access_token, you can use my app post panda, or make your own script. I'll include it here:

# arvin castro
# http://codecri.me/
# January 16, 2011

$client_id     = ''; # application id
$client_secret = ''; # application secret
$callbackURL   = 'http://'; # the URL of this script
$extendedPermissions = 'publish_stream,manage_pages,offline_access';

session_name('facebookoauth');
session_start();

if(isset($_GET['logout']) and $_SESSION['loggedin']) {
    $_SESSION = array();
    session_destroy();
}

if(isset($_GET['signin'])) {

    # STEP 1: Redirect user to Facebook, to grant permission for our application
    $url = 'https://graph.facebook.com/oauth/authorize?' . xhttp::toQueryString(array(
        'client_id'    => $client_id,
        'redirect_uri' => $callbackURL,
        'scope'        => $extendedPermissions,
    ));
    header("Location: $url", 303);
    die();
}

if(isset($_GET['code'])) {

    # STEP 2: Exchange the code that we have for an access token
    $data = array();
    $data['get'] = array(
        'client_id'     => $client_id,
        'client_secret' => $client_secret,
        'code'      => $_GET['code'],
        'redirect_uri'  => $callbackURL,
        );

    $response = xhttp::fetch('https://graph.facebook.com/oauth/access_token', $data);

    if($response['successful']) {

        $var = xhttp::toQueryArray($response['body']);
        $_SESSION['access_token'] = $var['access_token'];
        $_SESSION['loggedin']     = true;

    } else {
        print_r($response['body']);
    }
}

if($_SESSION['loggedin']) {
    // Get Profile ID
    $data = array();
    $data['get'] = array(
            'access_token'  => $_SESSION['access_token'],
            'fields' => 'id,name,accounts',
            );  
    $response = xhttp::fetch('https://graph.facebook.com/me', $data);
    echo '<pre>';
    print_r(json_decode($response['body'], true));
    echo '</pre>';

} else {
    echo '<a href="?signin">Sign in with Facebook</a>';
}

?>

I'm using my cURL wrapper class, xhttp

这篇关于FACEBOOK GRAPH / rest api:如何登录我的OWN USER使用PHP更新我的STATUS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆