简体   繁体   English

通过Facebook-SDK创建自动发布

[英]Create Auto-POST by Facebook-SDK

I want to enable Facebook Auto Post in my website. 我想在我的网站上启用Facebook Auto Post。

first I created an app, then follow the facebook docs I used the facebook-sdk for PHP, I inserted APP-ID and APP-SECRET, create LOGIN-URL and... to my script (everything like the facebook docs), but I still have problem!! 首先,我创建了一个应用程序,然后按照我使用用于PHP的facebook-sdk的facebook文档,将APP-ID和APP-SECRET插入,创建了LOGIN-URL并...(我的脚本中的所有内容均如此),但是我还是有问题!

The problem is: 问题是:

for first time when user visit my page, he see the login link. 用户首次访问我的页面时,他会看到登录链接。 when he click on it he will redirect to facebook dialog page for allowing app activities. 当他单击它时,他将重定向到Facebook对话框页面,以允许应用程序活动。 after this, when facebook redirect user to my canvas page, he see the login link again!! 之后,当facebook将用户重定向到我的画布页面时,他再次看到登录链接! (It seems the getUser() function not worked correctly in my script!). (似乎getUser()函数在我的脚本中无法正常工作!)。

base on facebook guides the user must see the user profile details... but still the login link is visible. 基于facebook指南,用户必须查看用户个人资料详细信息...但是登录链接仍然可见。

how can I fix this problem...? 我该如何解决这个问题...?

<?php 

        require_once("libs/facebook.php");
        $config = array(
            'appId' => 'XXXX',
            'secret' => 'XXXX'      
        );
        $fbConnect = new Facebook($config);

        $user_id = $fbConnect->getUser();
        if($user_id)
        {
            try {

                $userProfile = $fbConnect->api('/me', 'GET');
                echo "Name: " . $userProfile['name'];

            } catch (FacebookApiException $e) {

                $loginUrl = $fbConnect->getLoginUrl();
                echo "<a href='" . $loginUrl . "'>LOGIN 2</a>";

            }
        }
        else
        {
            $loginUrl = $fbConnect->getLoginUrl(array( 'scope' => 'publish_stream' ));
            echo "<a href='" . $loginUrl . "'>LOGIN 1</a>";
        }
    ?>

User always see "LOGIN 1"! 用户总是看到“ LOGIN 1”! it means the $user_id is always null (before and after app allowing activities)!! 这意味着$ user_id始终为null(在应用允许活动之前和之后)! after app allowing (when user for first time click on loginUrl link) I have 'stat' and 'code' in my url query string! 应用允许后(当用户首次单击loginUrl链接时),我的网址查询字符串中包含“状态”和“代码”! but still "LOGIN 1" is visible! 但是仍然可以看到“ LOGIN 1”!

I guess you want to publish a message like " Hey guys, I am now using Aref's superawsome Facebook app now ", right? 我想您想发布一条消息,例如“ 嘿,我现在正在使用Aref的超棒Facebook应用程序 ”,对吗?

This can be accomplished with the following lines : 这可以通过以下几行完成:

<?php
require_once("/FBAPI/src/facebook.php");

$config = array();
$config['appId'] = 'your_app_id';
$config['secret'] = 'your_app_secret';
$facebook = new Facebook($config);

$user = $facebook->getUser();
if(!$user){
    $loginUrl = $facebook->getLoginUrl(array('scope'=>'publish_stream', 'redirect_uri'=>'http://www.example.com'));
}
if($user){
    try{
        $user_profile = $facebook->api('/me');
        $access_token = $facebook->getAccessToken();
        $vars = array(
        'caption' => 'Aref\'s Facebook Application',
        'message' => 'Hey guys, I am now using Aref\'s superawsome Facebook App :D',
        'name' => 'Test',
        'link' => 'http://www.example.com',
        'description' => 'Aref\'s Facebook Canvas App',
        'picture' => 'http://fbrell.com/f8.jpg'
        );
        $result = $facebook->api('/me/feed', 'post', $vars);
        if($result){
            echo "Post was set";
        }
        else{
            echo "Error!";
        }
    }
    catch(FacebookApiException $e){
        error_log($e);
        $user = NULL;
    }
}
else{
    echo '<a href="'.$loginUrl.'"><img src="img/login.png"/></a>';
}
?>

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

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