Search code examples
facebookfacebook-wall

How to post to my facebook wall from my own adminpages(CMS)?


How can I post a ordinary post to my facebook wall from my administration pages where I upload content to my webpage?

So I upload content to my webpage from my CMS and next to where I display my uploaded content in my adminpages I would like to have a button that can publish that post to my facebook wall. As an ordinary post and not like a LIKE post or Comment post!


Solution

  • First you need to create an facebook app. Then you will get an app id and a secret key.

    Using this details you can do post into ur wall using facebook php library or u can use the following function

    <?php
    
    
        function doWallPost($postName='',$postMessage='',$postLink='',$postCaption='',$postDescription='')
        {
        $FB_APP_ID='xxxxxxxxxxxxxxxxxxxxxxxx';
        $FB_APP_SECRET='xxxxxxxxxxxxxxxxxxxxxxxxxxx';
    
        $APP_RETURN_URL=((substr($_SERVER['SERVER_PROTOCOL'],0,4)=="HTTP")?"http://":"https://").$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
    
        $code = $_REQUEST["code"];
    
        if(empty($code)) 
        {
            $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=".$FB_APP_ID."&redirect_uri=".$APP_RETURN_URL."&scope=publish_stream";                  
            header("Location:$dialog_url");
        }
    
        $token_url = "https://graph.facebook.com/oauth/access_token?client_id=".$FB_APP_ID."&redirect_uri=".urlencode($APP_RETURN_URL)."&client_secret=".$FB_APP_SECRET."&code=".$code;
        $access_token = file_get_contents($token_url);
    
        $param1=explode("&",$access_token);
        $param2=explode("=",$param1[0]);
        $FB_ACCESS_TOKEN=$param2[1];
    
    
        $url = "https://graph.facebook.com/me/feed";
        $attachment =  array(   'access_token'  => $FB_ACCESS_TOKEN,                        
                        'name'          => $postName,
                        'link'          => $postLink,
                        'description'   => $postDescription,
                        'message'       => $postMessage,
                        'caption'       => $postCaption,
                    );
    
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,2);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
        $result=curl_exec($ch);
        header('Content-type:text/html');
        curl_close($ch);
    
        return $result
        }
    
    
    
    
    
    
    
        ?>
    

    For details follow How to post wall in facebook using API in PHP?