Wednesday, April 30, 2014

Simple Facebook API PHP Integration

This is a PHP Integration using Facebook PHP SDK (v.3.2.2) This sample code will help you on how to post messages, pictures, links, descriptions,names and captions into your wall.

 Get Your App Id and App Secret 
http://developers.facebook.com

Facebook PHP SDK (v.3.2.2)
https://github.com/facebook/facebook-php-sdk

Source Code


<?php
/**
 * Name         : Simple Facebook API PHP Integration
 * Description  : Post message, picture, link, description, name, caption to your wall!. 
 * Author       : Ryan Tipones
 * Version      : 0.1
 * Author URI   : wpmigrator.wordpress.com
 *  
 */

require 'src/facebook.php';

/* Configure Facebook Apps Settings */

define('APP_ID', '<YOUR_APP_ID>');
define('APP_SECRET', '<YOUR_APP_SECRET>');
define('APP_PERMISSION', 'publish_stream,read_stream');
define('FILE_UPLAOD', FALSE);

$facebook = new Facebook(
                array(
                    'appId'         => APP_ID,
                    'secret'        => APP_SECRET,
                    'fileUpload'    => FILE_UPLAOD
                )
        );

try {
    
    $user = $facebook->getUser();

    if ($user) {
        
        $user_info = $facebook->api('/me'); 
        
        if ($user_info) { 
            
            /* Your basic profile information */

            $id           = $user_info['id'];
            $name         = $user_info['name'];
            $first_nam    = $user_info['first_nam'];
            $last_name    = $user_info['last_name'];
            $username     = $user_info['username'];
            $birthday     = $user_info['birthday'];
            $hometown     = $user_info['hometown']['name'];
            $gender       = $user_info['gender'];
            $email        = $user_info['email'];

        }
        
        $privacy = array(
                'value'     => 'ALL_FRIENDS', // EVERYONE, ALL_FRIENDS, NETWORKS_FRIENDS, FRIENDS_OF_FRIENDS, CUSTOM
                'friends'   => '', //FRIENDS_OF_FRIENDS, ALL_FRIENDS, SOME_FRIENDS, SELF, or NO_FRIENDS
                'allow'     => '', // Change this to your friends ids
                'deny'      => '' // Change this to your friends ids 
            );
                    
        $post_id = $facebook->api('/me/feed', 'post', 
                array(
                        'message'   => 'Simple Facebook API PHP Integration', 
                        'picture'   => '<YOUR-IMAGE-LINK-URL>', 
                        'link'      => 'http://bikolexpressonline.com',
                        'description'   =>'Simple PHP Integration using Facebook PHP SDK (v.3.2.2)', 
                        'name'          => 'Test API Facebook SDK', 
                        'privacy'       => json_encode($privacy),
                        'caption'       => 'BikolExpress Online Free Source Code for life!'
                    )
             );
        
        /* Debug */
        print_r($post_id);
        
    }else{
        echo("<script>top.location.href='" . $facebook->getLoginUrl(array( 'scope' => APP_PERMISSION )) . "'</script>");
    }

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

1 comment:

  1. Thanks for sharing. MsgClub brings SMS API PHP integration for sending bulk SMS to the prospective clients and also the existing customers.

    ReplyDelete

Genesis Framework for WordPress