Jump to content
dsp77

Facebook page/wall post din cron

Recommended Posts

M-am saturat sa tot postez pe pagina de facebook manual si m-am decis sa fac un script care sa posteze pe facebook automat o poza cu mesaj folosind cron.

<?php
require_once("/var/www/facebook/post/facebook-php-sdk-master/src/facebook.php");

$token_file = file_get_contents("token.txt");
$page_id = 45336456546456;
$message = 'Post test';
$img_path = '/var/www/facebook/post/1.jpg';

$config = array();
$config['appId'] = '435646456456';
$config['secret'] = '456456dfge56t4e5tgedrg546te';
$config['fileUpload'] = true;

$fb = new Facebook($config);

$user_id = $fb->getUser();

if($user_id) { //comment this line after application approval
if(empty($token_file)){
$json_response = file_get_contents("https://graph.facebook.com/me/accounts?access_token=".$fb->getAccessToken());
$decoded = json_decode($json_response, TRUE);
foreach($decoded['data'] as $k => $v){
if(in_array($page_id, $v)){
file_put_contents("token.txt", $v['access_token']);
}
}
} else {
$params = array(
"access_token" => $token_file,
"message" => $message,
"source" => "@" . $img_path,
);

try {
$ret = $fb->api('/'.$page_id.'/photos', 'POST', $params);
echo 'poza uploadata';
var_dump($ret);
} catch(Exception $e){
echo $e->getMessage();
}
}
} else { //comment this line after application approval
$login_url = $fb->getLoginUrl( array( 'scope' => 'manage_pages,publish_stream') ); //comment this line after application approval
echo 'Please <a href="' . $login_url . '">login.</a>'; //comment this line after application approval
} //comment this line after application approval
?>

Scriptul este departe de a fi perfect dar functioneaza dupa cum urmeaza:

1. inlocuiti variabile "page_id" cu id-ul paginii/contului

2. creati aplicatia in facebook developer si inlocuiti "appId" si "secret"

3. in setarile aplicatie trebuie sa adaugati adresa url unde va fi pus scriptul

4. creati fisierul "token.txt" cu drepturi de citire/scriere (se va stoca token-ul pt publicarea offline)

5. accesati pagina, apasati log in, acceptati aplicatia (de 3 ori ar trebui sa apara)

6. dupa care comentati liniile care contin comentul "comment this line after application approval"

7. puneti un cron din ora in ora ca de ex: 10 * * * * /usr/bin/php /var/www/script.php

Have fun.

Link to comment
Share on other sites

Am observat o nemernicie a celor de la facebook de cand folosesc script-ul. Si anume reach-ul postului nu se mai duce departe, de ex am pus 2 poze una manual alta automat iar in cateva minute cea pusa manual avea 200 views si cea automat 1.

Daca testati va rog sa-mi confirmati/infirmati treba asta cu post reach

EDIT: problema este ca am aplicatia in development.

Edited by dsp77
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...