Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook PHP post to fan page with cronjob?

Tags:

php

facebook

cron

I am using the following code to post to my facebook fan page and it is working fine. Now I want to use cronjob in order to post to Facebook. I know I have to use as access token but I am not sure how to set it up. I tried to use echo my own access token and the page's access token and use it in the post api but that did not work I got this error:

Uncaught OAuthException: An active access token must be used to query information about the current user.

Here is my code that I tried:

require_once('scripts/facebook.php');
    $config = array('appId' => 'xxx','secret' => 'xxx');

    $params = array('scope'=>'user_likes,publish_actions,email,offline_access,publish_stream,manage_pages');
    $facebook = new Facebook($config);
    $user = $facebook->getUser();
    if($facebook->getUser()) {
    try {

        $user_profile = $facebook->api('/me');
        $access_token = $facebook->getAccessToken();
        //echo "1. ".$access_token;

      } catch(FacebookApiException $e) {
                        $login_url = $facebook->getLoginUrl($params);
                        error_log($e->getType());
                        error_log($e->getMessage());
      }   
    } else {
        $login_url = $facebook->getLoginUrl($params);

    }    

$page_id = "xxxxxxxxxxxxx";
            $page_access_token = "";
            $result = $facebook->api("/me/accounts");
            foreach($result["data"] as $page) {
                if($page["id"] == $page_id) {
                $page_access_token = $page["access_token"];
                //echo '<br>';
                //echo "2. ".$page_access_token;
                break;
                    }
                }


        $args = array(
            'access_token'  => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
            'message'       => stripslashes($image_caption).$animaged_gif,
            'name' => stripslashes($image_caption).$animaged_gif,
            'link' => "http://www.example.com/images.php?i=".$image_name,
            'picture' => "http://www.example.com/thumbnails/".$image_name.".png",
            'actions' => array(
            'name' => 'See Pic',
            'link' => "http://www.example.com/images.php?i=".$image_name
            )

        );
       $post = $facebook->api("/$page_id/feed","post",$args);

As you can see that I tried to use the actual access token that I got when I echo out my own access token. But that did not work. I also used the page's access token, but that also did not work. Can you please tell what I am missing here or what is the proper way to do this ? After some further search I did, I came across setAccessToken and $page_info = $fb->api("/".$sInfo['pageId']."?fields=access_token"); but there is very limited resources on how to use them. I am not even sure if those will be appropriate to this problem. All I need to know is which access token I need to use and what is the appropriate code to set it up ?

like image 341
Max Pain Avatar asked Nov 04 '12 09:11

Max Pain


1 Answers

This should probably fix it.

offline_access is deprecated. It is now being replaced by a system where you get an extended expiry access_token.

When you run it manually, You generate a live access token, which is invalid after 1-2 hours after you acutally run it. And there is no way you can generate another access_token with a cron. Generating access token should be done manually.

So to fix it, have a read : https://developers.facebook.com/roadmap/offline-access-removal/

and implement this : https://developers.facebook.com/docs/howtos/login/server-side-login/

And save the Access token to your database. When you run the cron, get the token from your database.

P.S : The access_token is valid for 60 days, and it should be extended again and can be done by the user logging in manually. The access_token generally "may" not change but just gets the expiry extended.

ADD :

Scheduling Posts : You can also schedule the post for upto 6 months too. Just read about it and thought I would point out.

Read : https://developers.facebook.com/docs/reference/api/page/ scroll down to Posts Create sections

like image 127
Kishor Avatar answered Nov 08 '22 17:11

Kishor