Search the Community
Showing results for tags 'autopost'.
-
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.