Daniel
Daniel

Reputation: 1295

Post custom message to facebook wall

I'm working on a script for post a message on a visitors wall. The visitor can enter there own message in a form en then send it to facebook.

index.php:

<form name="form" method="post" action="wall.php">
<textarea name="t_update" cols="50" rows="5" id="t_update" >message</textarea><br>
<input type="submit" name="Submit" value="Post To Your facebook Account!">
</form>

This sends "t_update" to "wall.php". The wall.php script is working fine I tested it with static text. but when I try to insert the "t_update" text into the var $APP_MSG it's empty when send to facebook. This is the complete wall.php script.

wall.php:

<?php 

    /** FB APP DATA **/
    $FB_APPID   =   "Facebook app id";
    $FB_SECRET  =   "Facebook secret code";
    /** MSG DATA **/
    $APP_MSG            =   ($_POST['t_update']); // post message
    $APP_MSG_LINK_TITLE =   "Title";
    $APP_MSG_LINK       =   "www.domain.com";

    global $ch;
    $code       =   $_REQUEST["code"];
    $error      =   $_REQUEST["error"];
    $returnurl  =   "http://".$_SERVER["HTTP_HOST"].$_SERVER["SCRIPT_NAME"];


    function facebook_curl_request($url,$params=array(),$post = false){
        global $ch;
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        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, 20);
        curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11');
        curl_setopt ($ch, CURLOPT_HEADER, false); 

        if($post == true){
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        }
        $response = curl_exec ($ch);
        $temp = json_decode($response,1);
        if(isset($temp["error"]) && is_array($temp["error"])){
            echo $temp["error"]["type"]."<br>";
            echo $temp["error"]["message"]."<br>";
            echo "--------------------------------------------";
            curl_close ($ch);
            exit;
        }
        return $response;
    }

    if(empty($code)) {
        $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" 
        . $FB_APPID . "&redirect_uri=" . urlencode($returnurl) . "&scope=publish_stream,email";
        header("Location: $dialog_url");
    }

    if(!empty($error)){
        echo $_REQUEST["error"]."<br>".$_REQUEST["error_reason"]."<br>".$_REQUEST["error_description"];
    }else{

        if(!empty($code)){
            /** CREATE TOKEN **/
            $ch         =   curl_init();

            $token_url  = "https://graph.facebook.com/oauth/access_token?"
                        . "client_id=" . $FB_APPID . "&redirect_uri=" . $returnurl
                        . "&client_secret=" . $FB_SECRET  . "&code=" . $code;

            $token  =   facebook_curl_request($token_url);
            $tarr   =   explode("&",$token);
            list($token_name,$token) = explode("=",$tarr[0]);

            /**GET USER INFO**/
            $graph_url  = "https://graph.facebook.com/me?".$token_name."=".$token;
            $user       = json_decode(facebook_curl_request($graph_url),1);
            $userid     = $user["id"];

            /* POST TO WALL **/
            $graph_url  = "https://graph.facebook.com/".$userid."/feed";
            $params = array(
                $token_name     =>  $token,
                "message"       =>  $APP_MSG,
                'name'          =>  $APP_MSG_LINK_TITLE,
                'link'          =>  $APP_MSG_LINK
            );
            $response = facebook_curl_request( $graph_url, $params, true);
            list($userid,$postid) = explode("_",$response);
            echo "<html><head></head><body>Bedankt voor het posten op facebook!</body></html>";

            curl_close ($ch);
        }
    }



?>

I've tried everything can anybody point me in the right direction???

Upvotes: 0

Views: 5344

Answers (2)

Joel Dare
Joel Dare

Reputation: 165

You can also do this in HTML and JavaScript without requesting the users permission. First, initialize the Facebook API in your HTML page. Put this at the BOTTOM of the page, before the closing body tag.

<div id="fb-root"></div>
<script src="https://connect.facebook.net/en_US/all.js"></script>
<script>
 FB.init({
  appId : '[your_app_id_goes_here];',
  status : true, // check login status
  cookie : true, // enable cookies to allow the server to access the session
  xfbml : true // parse XFBML
 });
</script>

Don't forget to replace [your_app_id_goes_here] with your Facebook App ID. Next, create a JS function that will post on the users wall. Put this in the head of the page.

<script>
 // A function to post on the users wall
 function wallPost() {
  FB.ui(
   {
    method: 'feed',
    name: 'Go find something!',
    link: 'http://www.google.com',
    picture: 'http://www.google.com/images/logos/ps_logo2&#46;png',
    caption: 'Google',
    description: 'This was posted from a test app I created.',
    message: ''
   },
   function(response) {
    if (response && response.post_id) {
     document.getElementById('message').innerHTML = 'Thanks for sharing!';
    } else {
     document.getElementById('message').innerHTML = 'Hey, you didn\'t share!';
    }
   }
  );
 }
</script>

Finally, use the function you just created in a link of some sort.

<p id="message">
 <a href="#" onclick="wallPost();">Share Me!</a>
</p>

This example is from my book Social App Development by Joel Dare. Get it at social.joeldare.com.

Upvotes: 0

Carl from OZ
Carl from OZ

Reputation: 71

I did test the code and can confirm that it's not working. The reason its not is because when you first hit wall.php, you get redirected to Facebook to authenticate, it then redirects back to your application script "wall.php" with a GET - so you lose your POST variables on the redirect. That's why it ends up empty. Your other variables are still there because it's hard coded and will get called regardless when you run the script. Hope that makes sense.

I've just started working on an application and have found using the Facebook PHP SDK alot easier to work with. Code is much cleaner as well. The Dev area has a sample PHP file you can work with that shows you how to authenticate as well.

Example below:

<?php

require_once 'facebook-php-sdk/src/facebook.php';

$APP_MSG =   ($_POST['t_update']); 

$facebook = new Facebook(array(
  'appId'  => 'APPID',
  'secret' => 'SECRET'
));

$wallArgs = array('message' => $APP_MSG);
try {
   $facebook->api('me/feed', 'post', $wallArgs);

  }
catch(Exception $e) {
    print "<pre>";
    print_r($e);
    print "</pre>";
 }

?>

Upvotes: 2

Related Questions