Search code examples
phpfacebookfacebook-graph-api

Why I get that error Call to undefined method Facebook\Facebook::api()?


I am trying to post a link to facebook page using facebook SDK PHP, while trying my code I get "Call to undefined method Facebook\Facebook::api()", I am unaware what is the error it is the first time I work with facebook SDK. I am using facebook-php-sdk-v4-5.0.0

my code is:

        <?php
        session_start();
        require_once("FacebookSDK/src/Facebook/autoload.php");

        use Facebook\Facebook;

         $config = array_merge([
            'app_id' => 'APP_ID',
            'app_secret' => 'SECRET',
            'enable_beta_mode' => false,
            'http_client_handler' => null,
            'persistent_data_handler' => null,
            'pseudo_random_string_generator' => null,
            'url_detection_handler' => null,
        ]);

        $fb = new Facebook($config);

        $params = array(
          "access_token" => "TOKEN", 
          "message" => "Here is a blog post about auto posting on Facebook using PHP #php #facebook",
          "link" => "http://example.com/",
          "picture" => "http://example.com/image1.jpg",
        );

        try {
          $ret = $fb->api('/ID/feed', 'POST', $params);
          echo 'Successfully posted to Facebook';
        } catch(Exception $e) {
          echo $e->getMessage();
        }
        ?>

Solution

  • The facebook methods are different here. Use the code below

     $ret = $fb->post('/page_id/feed',$params,  $pageaccesstoken );
    

    instead of

    $ret = $fb->api('/ID/feed', 'POST', $params);