Showing posts with label Facebook. Show all posts
Showing posts with label Facebook. Show all posts

Wednesday, April 30, 2014

Posting Status in your Facebook Page with PHP API Class Wrapper

Post your Status in Facebook Page with PHP API Class Wrapper

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

/* Post your Status in your Facebook Page with PHP API Integration */

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, manage_pages');
define('FILE_UPLAOD', FALSE);


/* Configure Facebook */

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

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

    if ($user) {
        
        $page_id = '<YOUR-PAGE-ID>';
        $page_info = $facebook->api("/$page_id?fields=access_token");
        
        if( !empty($page_info['access_token']) ) {
            $data = array(
                            'access_token'  => $page_info['access_token'],
                            'message'       => '<YOUR-POST-MESSAGE>', 
                            'picture'       => '<YOUR-PICTURE-URL-LINK>', 
                            'link'          => '<YOUR-WEBSITE-URL-LINK>',
                            'description'   => '<YOUR-POST-DESCRIPTION>', 
                            'name'          => '<YOUR-POST-NAME>'

                        );
            $post_id = $facebook->api("/$page_id/feed","post",$data);
       } else {
        $permissions = $facebook->api("/me/permissions");
        if( !array_key_exists('publish_stream', $permissions['data'][0]) || !array_key_exists('manage_pages', $permissions['data'][0])) {
            echo("<script>top.location.href='" . $facebook->getLoginUrl(array( 'scope' => APP_PERMISSION )) . "'</script>");
        }

    }
        
    }else{
        echo("<script>top.location.href='" . $facebook->getLoginUrl(array( 'scope' => APP_PERMISSION )) . "'</script>");
    }

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

Invite Friends In FACEBOOK with API Apprequest Method Implementation

This sample Code is a Facebook API method implementation which uses Javascript SDK. A simple source code that can invite your friends to use your Facebook Application.

Source Code


 <!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <script type="text/javascript">
            window.fbAsyncInit = function() {
                FB.init({
                    appId: "<YOUR-APP-ID>",
                    status: true,
                    cookie: true
                });
            };
            
            // Load the SDK Asynchronously
            (function(d){
                var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
                if (d.getElementById(id)) {return;}
                js = d.createElement('script'); js.id = id; js.async = true;
                js.src = "//connect.facebook.net/en_US/all.js";
                ref.parentNode.insertBefore(js, ref);
            }(document));
            
            function invite_your_friends() {
                FB.ui({
                    method: "apprequests",
                    message: "Check out this Fantastic App!"
                }, invite_your_friends_callback);
            }
            
            function invite_your_friends_callback(response) {
                console.log(response);
            }

  </script>
  <center>
      <a href="javascript:;" onclick="invite_your_friends();">
          <h3>Invite Your Friends and Share this Fantastic App!</h3>
      </a>
  </center>
    </body>
</html>



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

Genesis Framework for WordPress