简体   繁体   English

如何使用Facebook PHP SDK自动发布到朋友墙

[英]How to post to friends wall automatically using Facebook PHP SDK

Is it possible to post to a friends wall/timeline in facebook using an app even if the user is not currently logged in? 即使用户当前没有登录,是否可以使用应用程序在Facebook中发布朋友墙/时间线? Here's the code that I'm currently working on, as you can see it posts a message on a friends wall(substitute the friend_id with an actual profile id). 这是我正在处理的代码,因为您可以看到它在朋友墙上发布消息(用实际的个人资料ID替换friend_id)。 This works but a user has to logged in in order to perform this operation. 这有效,但用户必须登录才能执行此操作。 I don't have any idea what to do if I want this script to execute automatically(without user intervention). 如果我希望这个脚本自动执行(无需用户干预),我不知道该怎么办。

<?php
require 'php_sdk/src/facebook.php';

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

$session = $facebook->getUser();
$me = null;

if($session){
    try{
        $me = $facebook->api('/me');

        $facebook->api('/friend_id/feed', 'post', array('message'=>'hello without user!'));

    }catch(FacebookApiException $e){
        echo $e->getMessage();
    }
}

if($me){
    $logoutUrl = $facebook->getLogoutUrl();
    echo "<a href='$logoutUrl'>Logout</a>";
}else{
    $loginUrl = $facebook->getLoginUrl(array(
        'scope' => 'user_about_me,user_birthday,user_location,email,read_friendlists,friends_location,friends_birthday,publish_stream'
    ));
    echo "<a href='$loginUrl'>Login</a>";
}
?>

Any ideas? 有任何想法吗? Code samples, and links to specific documents that can help me gain a bit of idea on how this works is greatly appreciated thanks! 非常感谢代码示例,以及可以帮助我了解其工作原理的特定文档的链接!

When the user is logged in, you need to trap their "Access Token" 当用户登录时,您需要捕获他们的“访问令牌”

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

// NOTE: wrap these in try/catch blocks for safety - this is example only
$session = $facebook->getUser(); 
$access_token = $facebook->getAccessToken(); 

To post as them later, you use the access token 要稍后发布,您可以使用访问令牌

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

// NOTE: wrap these in try/catch blocks for safety - this is example only
$facebook->setAccessToken($access_token); 
// Then check /me and you should still have the same user.
// Then post to /me/feed to post to the wall.

The token will be valid for 2 days. 令牌有效期为2天。 You can extend this to 60 days if you want. 如果需要,您可以将此延长至60天。


Edit: For exenteding the token, you call the function 编辑:为了激活令牌,您可以调用该函数

$facebook->setExtendedAccessToken();

immediately before "getAccessToken" call. 紧接在“getAccessToken”调用之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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