Facebook connect and Share

PHP TECHNOLOGY

Posted on 2014-11-24 04:11:39


Login and use account Facebook This code for get info from facebook

require_once(WWW_ROOT."facebook" . DS . 'facebook.php');
require_once(WWW_ROOT."facebook" . DS . 'base_facebook.php');

$facebook = new Facebook(array(
    'appId' => APP_ID,
    'secret' => APP_SECRET,
));
 
$user = $facebook->getUser();

if ($user) 
{
    try 
    {
        // Proceed knowing you have a logged in user who's authenticated.
        $user_profile = $facebook->api('/me');
   } 
    catch (FacebookApiException $e) 
    {error_log($e.'==');
        $user = null;
    }
 
    if (!empty($user_profile )) 
    {
        // do something
    }
}
else 
{
    # There's no active session, let's generate one
    $login_url = $facebook->getLoginUrl(array( 'scope' => 'email,manage_pages')); // refere scope apiheader("Location: " . $login_url);	
}

Include two files attach facebook Post to wall facebook. Example with behind script.

<a href = "#" onclick = "shareFB()">Share</a>
 
<script src="https://connect.facebook.net/en_US/all.js"></script>
<script>
 
function shareFB()
{
    FB.init({ldelim}appId: "{$fbID}", status: true, cookie: true{rdelim});
 
    var obj = 
    {
        method: 'feed',
        link: 'http://yourdomain.com',
        picture: 'image_path',
        name: "Title of post",
        description: "Description of post"
    };
 
    FB.ui(obj);
}
</script>