So, I tore my hair, trying to do this job. I ran into a stack overflow looking at existing issues and found this: Facebook: Post to the page as a page question (token / php) , but it still does not look to solve my problem.
I try to post on my facebook page every day using cron's work. I have authentication problems. Here is my code:
//Post to Facebook //Variables $app_id = "..."; $app_secret = "..."; $page_id = "..."; $my_url = "http://.../"; $access_token = "taken from page access token (developers.facebook.com/tools/explorer/)"; //Create the facebook object $facebook = new Facebook(array( 'appId' => $app_id, 'secret' => $app_secret, 'cookie' => true )); //Get the access token using Facebook Graph API //This gives permission to post to the wall $token_url="https://graph.facebook.com/oauth/access_token?client_id=" . $app_id . "&client_secret=" . $app_secret . "&code=" . $access_token . "&redirect_uri=" . $my_url; $response = file_get_contents($token_url); $params = null; parse_str($response, $params); $user_access_token = $params['access_token']; //Get a list of pages and loop through //If one matches one in the variables above, that the one //We're posting to $attachment_1 = array( 'access_token' => $user_access_token ); $result = $facebook->api("/me/accounts", $attachment_1); foreach($result["data"] as $page) { if($page["id"] == $page_id) { $page_access_token = $page["access_token"]; break; } } //Write to the Page wall try { $attachment = array( 'access_token' => $page_access_token, 'message'=> "Hello World" ); $result = $facebook->api('/me/feed','POST', $attachment); } catch(Exception $e) { //Send me an email ... mail($to, $subject, $message, $headers); }
This works if I access the script in the browser (I assume that it uses my facebook session then), but not when it starts with the cron job.
I would really appreciate any help. Thanks.
source share