简体   繁体   中英

Create Auto-POST by Facebook-SDK

I want to enable Facebook Auto Post in my website.

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!!

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. after this, when facebook redirect user to my canvas page, he see the login link again!! (It seems the getUser() function not worked correctly in my script!).

base on facebook guides the user must see the user profile details... but still the login link is visible.

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"! it means the $user_id is always null (before and after app allowing activities)!! after app allowing (when user for first time click on loginUrl link) I have 'stat' and 'code' in my url query string! but still "LOGIN 1" is visible!

I guess you want to publish a message like " Hey guys, I am now using Aref's superawsome Facebook app now ", right?

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>';
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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