Publish / post on a user facebook wall using CURL

The purpose is to write something (comment or any kind of action) on the wall of one of your users. I won’t use Symfony but only php with curl because it’s easier. I suppose you already have a working connexion with php-sdk installed.

Add “publish_actions” in your permission list

Here is my source in php for Symfony. I get parameters from parameters.yml and the facebookId from the current user. $msg is the message you want to post on fb wall.

        $config = array(
            'appId' => $this->container->getParameter('api_facebook_id'),
            'secret' => $this->container->getParameter('api_facebook_secret'),
            'fileUpload' => false, // optional
            'allowSignedRequest' => false, // optional, but should be set to false for non-canvas apps
        );
 
        $facebook = new \Facebook($config);
 
        $attachment =  array(
            'access_token' => $facebook->getAccessToken(),
            'message' => $msg
        );
 
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$this->user->getFacebookId().'/feed');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  //to suppress the curl output
        $result = curl_exec($ch);
        curl_close ($ch);

Leave a Reply

Your email address will not be published. Required fields are marked *