Post automatically on the wall of user (facebook App)

16,698

To solve your problem , I will share my code with you . this worked for me .

You need just to to write some loop while to select all users id from your database .

    <?php 

     //// publish as status 

     //// publish post to users 


         require_once("facebook.php");


       $app_id = "xxxxxxxxxxx";
       $app_secret = "xxxxxxxxxxxxxx";
       $my_url = "http://t.xxxxx.net/facebook/publish.php/"; // refer number 

    ///
       $config = array();
      $config['appId'] = 'xxxxxxxx';
      $config['secret'] = 'xxxxxxxxxxxx';
      $config['fileUpload'] = false; // optional

        $facebook = new Facebook($config);
       ///

    ////

      function getAccessToken() {
        if ($this->accessToken !== null) {
          return $this->accessToken;
        }

        $this->setAccessToken($this->getApplicationAccessToken());
        $user_access_token = $this->getUserAccessToken();
        if ($user_access_token) {
          $this->setAccessToken($user_access_token);
        }

        return $this->accessToken;
      }

       function getApplicationAccessToken() {
        return $this->appId.'|'.$this->appSecret;
    }


    /////////////////////////////// Update status Function 
    //xxxxxxxx
    $session = $facebook->getUser();



    // New SDK
    $facebook->api ( array(
        'method' => 'users.setStatus',
     'status' => 'Hi 
this new status by my app 

     ',
      'uid'    => '21511',/// user_id 
      'session'=>$session,
    ) );


     ?>
Share:
16,698
UDE_Student
Author by

UDE_Student

IS-Student, interested in Java, R, SQL

Updated on June 08, 2022

Comments

  • UDE_Student
    UDE_Student almost 2 years

    i want for my app to post on a user's wall on behalf (for advertising e.g.). I think about creating a php script, which i will execute with a cron job on my server every week. I have the userids in my database. Now i want that the script gets an userid and then posts on the wall of the user. (of course if the user has got still installed the app and granted the publish stream permission)

    is it possible to create a script that triggers this?
    $post = $facebook->api("/$user1/feed","POST",$params); or
    $post = $facebook->api("/$user2/feed","POST",$params); etc...?

    Thank for advise

    <?php
    
    require 'src/facebook.php';
    $app_id = 'yourappid';
    $app_secret = 'yourappsecret';
    $app_namespace = 'appname';
    $app_url = 'https://apps.facebook.com/' . $app_namespace . '/';
    $scope = 'email,publish_actions';
    
    
    // Init the Facebook SDK
    $facebook = new Facebook(array(
     'appId'  => $app_id,
     'secret' => $app_secret,
    ));
    
    
    
    // Get the current user
    $user = $facebook->getUser();
    
    // If the user has not installed the app, redirect them to the Auth Dialog
    if (!$user) {
      $loginUrl = $facebook->getLoginUrl(array(
       'scope' => $scope,
       'redirect_uri' => $app_url,
     ));
    
      print('<script> top.location.href=\'' . $loginUrl . '\'</script>');
    
       }
    
    
        else {
    
           try {
            $params = array(
                'message'       =>  "your message",
                'name'          =>  "hello world",
                'description'   =>  "hello world",
                'link'          =>  "hello world",
                'picture'       =>  "hello world",
            );
    
            $post = $facebook->api("/$user/feed","POST",$params);
    
            echo "";
    
         }
          catch (FacebookApiException $e) {
           $result = $e->getResult();
         }
    
      }
    
     ?>
    
  • UDE_Student
    UDE_Student almost 11 years
    Thank you very much :) But after a little bit reading here on stackoverflow, i saw this post: stackoverflow.com/a/15948611/2466312 So i think your (good) solution won't work anymore :/ ??