Facebook PHP post to fan page with cronjob?

后端 未结 4 521
伪装坚强ぢ
伪装坚强ぢ 2020-12-08 08:44

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

相关标签:
4条回答
  • 2020-12-08 08:57

    You can scrap the entire overhead of the Facebook API and follow this extremely simple approach.

    Check what your page-specific email address is in the "Mobile" section of your page settings (https://www.facebook.com/pages/edit/?id=INSERT_YOUR_PAGE_ID_HERE&sk=mobile) and then use it here:

    <?php
        mail("thatEmailAddress@facebook.com", "New Status Update!", "");
    ?>
    

    The body is empty because Facebook says "to update your status, write in the email subject line and leave the email body blank."

    0 讨论(0)
  • 2020-12-08 09:00

    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

    0 讨论(0)
  • 2020-12-08 09:04

    You can take a look here https://github.com/newbacknew/owloo.com. This is a base of scripts from owloo.com for get data from Facebook via the "ADS" Dashboard, also have to retrive analytics data from Twitter and Instagram.

    With the base script you can get trends, interests, behavior, demographic, ages, gender qty of every country, city, stage, fanpage from facebook

    The base of all script are in the WSERVICE folder of the backups folders.

    0 讨论(0)
  • 2020-12-08 09:20

    This is how you can do it :

    1. Get a short lived accesstoken.
    2. Extend it.
    3. Get a long lived page accesstoken. (Doesn't matter, here we are going to fetch new accesstoken every time. Less complicated, and reliable.)

    Thought I would go with an algo, instead I am adding all the possible codes and documentations since this may help someone else too.

    Getting short lived access token and extending it:

    fetchtoken.php

    <?php
    
    //read more : https://developers.facebook.com/docs/howtos/login/server-side-login/
    session_start();
    $app_id = "xxxxxxxxxxxxxx";
    $app_secret = "xxxxxxxxxxxxxxxx";
    $my_url = "www.stackoverflow.com/";  // redirect url
    
    $code = $_REQUEST["code"];
    
    if(empty($code)) {
        // Redirect to Login Dialog
        $_SESSION['state'] = md5(uniqid(rand(), TRUE)); // CSRF protection
        $dialog_url = "https://www.facebook.com/dialog/oauth?client_id=" 
           . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
           . $_SESSION['state'] . "&scope=publish_stream,read_friendlists,email";
    
    
        echo("<script> top.location.href='" . $dialog_url . "'</script>");
    }
    if($_SESSION['state'] && ($_SESSION['state'] === $_REQUEST['state'])) {
        $token_url = "https://graph.facebook.com/oauth/access_token?"
           . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
           . "&client_secret=" . $app_secret . "&code=" . $code;
    
         $response = file_get_contents($token_url);
         $params = null;
         parse_str($response, $params);
         $longtoken=$params['access_token'];
    
    
    //save it to database   
    } 
    ?>
    

    So you now have a long lived accesstoken ready in your database, a text file or whatever.

    cronjob.php

    <?
    require_once('scripts/facebook.php');
    // Pull access token from the file/db
    $access_token = "access token from file or db";
    $facebook->setAccessToken($access_token); // sets our access token as the access token when we call something using the SDK, which we are going to do now.
    
    $config = array('appId' => 'xxx','secret' => 'xxx');
    
    $params = array('scope'=>'user_likes,publish_actions,email,publish_stream,manage_pages');
    $facebook = new Facebook($config);
    $user = $facebook->getUser();
    if($facebook->getUser()) {
        try {
    
            $user_profile = $facebook->api('/me');
        } 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'  => $page_access_token,
        '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);
    ?>
    

    Make sure you don't grab access_token(user access token / $access_token) again in the script.

    Also, the access_token in $args which is the page access token should be inserted by the $page_access_token variable, and not manually.

    Having offline_access in the scope does nothing than showing them on the permission dialogue box that their data can be accessed any time which can be done without that notification anyway.

    This should work as long as you visit the fetch.php once at the start, and once every 60 days manually.

    Let me know.

    0 讨论(0)
提交回复
热议问题