Facebook API daily post from PHP/MySQL - php

I'm working on a problem for many days now without solution.
What I got :
A MySQL database with 1000+ advices
A daily advice displayed on a page with a PHP script
A FB Application + FB SDK
A FB user account
What I'm looking for :
A daily post from my FB account containing the daily advice
The best result I got so far :
A php page which post the daily advice by clicking on a button then login in FB.
Is it possible to save the login informations in the PHP script to get a fully automatic daily post with cron ?
Does anyone know a solution ?
Sincerly,

Ok folks, I did it.
Here is my solution :
1) Download the Facebook PHP SDK from https://github.com/facebook/facebook-php-sdk
2) Generate an extended access token with this code (generate_token.php) :
require_once('src/facebook.php');
$facebook = new Facebook(array(
'appId' => 'YOUR APP ID',
'secret' => 'YOUR APP SECRET ID',
'fileUpload' => true
));
$facebook->setExtendedAccessToken();
$access_token = $_SESSION["fb_".$fb_appId."_access_token"];
$facebook->setAccessToken($access_token);
$accessToken = $facebook->getAccessToken();
The $accessToken is a 60 days long access to your account by the application.
Store this token in your database.
3) Publish script (fb_post.php):
Get the $accessToken from your database, then use it to access to your account without manuel login :
require_once('src/facebook.php');
$app_id = 'YOUR APP ID';
$app_secret = 'YOUR APP SECRET ID';
$config = array(
'appId' => $app_id,
'secret' => $app_secret,
);
$facebook = new Facebook($config);
$facebook->setAccessToken($fbAccessToken);
$user_id = $facebook->getUser();
Use the first page (generate_token.php) once every 2 months to generate a 60 days token.
"Et voila", just configure a daily Cron Job which execute "fb_post.php" and your robotic Facebook will rise !

did you try facebook SDK for PHP, it is simple you have to register your app with facebook and use your application id and application secret following link contains a sample script.
developers.facebook.com/docs/php/howto/postwithgraphapi

1.You need your Website to set as an Facebook App
2.read into https://developers.facebook.com/docs/opengraph/
Basicly it is possible of course. But it for me it needs time to get used to the Graph Api.
means a quick answer for your desire is not possible

Related

Get user access token with permission via php with FB sdk 3.2

I'm admin of a page and I have my app with ID and Secret.
When I go to Graph api Explorer I can easily take my User Access Token with permission "manage-pages".
I need to take the same via php, using the FB sdk 3.2.
I managed to run only this:
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'My app ID',
'secret' => 'My app secret',
));
//This token is: "My app ID|My app secret"
print_r($facebook->getAccessToken());
How Can I get the user access token with permission via php with fb sdk 3.2?
Can somebody help me? Thank you!
You need to generate a Login URL and redirect Users to that one, it´s explained in the docs for 3.x.x: https://developers.facebook.com/docs/reference/php/facebook-getLoginUrl/
With manage_pages, it would be like this:
$params = array(
'scope' => 'manage_pages',
'redirect_uri' => 'https://www.myapp.com/post_login_page'
);
$loginUrl = $facebook->getLoginUrl($params);
I highly suggest upgrading to a server with PHP 5.4+ though.
You shouldn't waste your time with using an outdated version of the PHP SDK, or is there a reason why you chose the old version?
See
https://developers.facebook.com/docs/php/gettingstarted/4.0.0
on how to implement Facebook Login.

Facebook PHP-SDK setAccessToken NOT WORKING

IDEA: Get event information without user interaction from various facebook pages (clubs,bars,etc..). However some pages have set that info not to be public.
WORKING PRINCIPLE: I have the latest facebook PHP-SDK on my site and an APP on facebook, so usually i call
$facebook = new Facebook(array(
'appId' => '387262991341732',
'secret' => '09014d999f6e34d80ca3e62e331834cc',
));
$events = $facebook->api("$page_url/events");
PROBLEM:
When a page is not public I have to use an access_token.
1) This is an APP access-token, which is not permitted to view the events.
$app_token = $facebook->getAccessToken();
So I figure I need to get user access_token and add more code:
if (!$user) {
$args['scope'] = 'offline_access';`<br>`
$loginUrl = $facebook->getLoginUrl($args);`<br>`
}
<?php if (!$user): ?>
Login with Facebook
<?php endif ?>`
I click the link by myself and use getAccessToken() to get the user access_token:
$user_token = $facebook->getAccessToken();// I store this in my database
I check this with:
file_get_contents('http://graph.facebook.com/privatepage/events?access_token=$user_token');
// Everything works, im very happy
2) Now I delete all cookies, etc and try to use $facebook->setAccessToken($user_token) and then $events = $facebook->api("$page_url/events"); and get nothing. I assure you one more time that AccessToken is valid.
After quite a long research im stuck with this.
???) *Any Ideas how use $facebook->api(...) and not file_get_contents?

Facebook - Retrieving recent posts from FB API in PHP

I am working on a script to pull recent Facebook posts/status updates for a specific using the Facebook API. I downloaded the FB SDK and searched all over the FB developers site, SO, and other places and it seems like the code below should accomplish what I need. However, although I am not getting an error, I am getting a NULL result.
require 'facebook-php-sdk/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXX',
));
$fbApiGetPosts = $facebook->api('***My Name***/feed?limit=5');
var_dump($fbApiGetPosts["data"]);
I have tried using my two FB accounts,(my personal acct and a test account that I created just for this project) as well as one for my company and each time the response was NULL. When I misspelled the account name or used a name that did not exist I got an error, which seems to suggest the request is going somewhere.
In the code above, I also tried var_dump-ing $fbApiGetPosts and that result was also null
Can anybody see what the missing piece of the puzzle is?
I've not used the Facebook SDK, but here is how I retrieve Facebook posts:
$appId = 'XXXXXXXXXXXX';
$token = 'XXXXXXXXXXXXXXXXXXXXXXXX';
$posts = json_decode(
file_get_contents('https://graph.facebook.com/' . $appId . '/feed?
access_token=' . $token
)
);
var_dump($posts->data);
Hopefully this helps!
EDIT:
The token is just called an "Access Token". Here are the instructions for obtaining one. You might find the Access Token Tool helpful, but it's more for debugging existing tokens then creating new ones.
Just use the PHP SDK :
download link : https://developers.facebook.com/docs/php/gettingstarted/
Log your app :
$facebook = new Facebook(array('appId' => 'XXXXX','secret' => 'XXXXX'));
$facebook->setAccessToken('stored access token');
then :
$fbApiGetPosts = $facebook->api('/me/feed');
print_r($fbApiGetPosts);
Here is the Graph doc : https://developers.facebook.com/docs/graph-api/reference/
and the /feed ref : https://developers.facebook.com/docs/graph-api/reference/user/feed/

PHP facebook application

I'm starting out to learn how to build a Facebook application, but I've run in to some problems. I can't seem to find a simple tutorial on how to authenticate and ask for permissions from the user. The app itself is in php and consists of a simple quiz. I'm using the php sdk 3.0, but in the example file you have to login before you are asked to allow permissions. I just want the user to be redirected to the permissions prompt when the app is loaded.
So, to be more specific, I need som sort of basic script that authenticates and ask the user for permissions. I'm using the php sdk, but if there is a better way please feel free to suggest this.
Here is a list of all permissions
try getting this url on some link or redirect the user to it:
https://www.facebook.com/dialog/oauth/?scope=email,user_about_me,publish_stream&client_id=xxxxxxxxxxx&redirect_uri=REDIRECT_URL&response_type=token
where the scope are the permission you are asking for, the clien_id sould be your app id (not the secret) and the redirect_uri (its URI not URL) is the site that the user will see after installing (I recomend to get here te full facebook app URL, i.e https://facebook.com/fanpage/?sk=app_xxxxxxxxxxxxxxxx)
and thats the simpliest way, if you feel capable of more stuff you can use this PHP code:
$fbconfig['appid'] = "xxxyyy";
$fbconfig['secret'] = "xxxxxxxxxxxxxxx";
$fbconfig['baseurl'] = "https://www.facebook.com/pages/FANPAGE/xxxxxxxxxxxxxxxx?sk=xxxyyy";
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,
));
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email,user_about_me,publish_stream',
'redirect_uri' => $fbconfig['baseurl'],
'display' => 'popup'
)
);
and redirect user to the $loginUrl.
hope it helps, cheers.

Why can I not establish access to my Facebook app?

I am following the instructions on this page to create a PHP script which will (hopefully) allow me to post to Facebook from my web site.
All I really want to establish is the ability to enter text and a link into a form on my web site and have it be posted to a Facebook wall.
My understanding is that I need to validate a Facebook app in order to do this, which is what brought me to the above web page.
I have followed their instructions dutifully, and by now I have been through it many times, checking to make sure I have followed their steps accurately.
However, when it gets to the part where I run fb_access.php from my web site, I get absolutely no response from Facebook.
I notice that the screen shot examples on the page I am using are not what I am seeing in the Facebook interface. The tutorial is from December 2010, which I would hope is recent enough, but perhaps things have changed...?
How do I get Facebook to respond to my PHP script?
<?
require_once 'facebook.php';
$app_id = "<my App ID";
$app_secret = "<my App Secret>";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
if(is_null($facebook->getUser()))
{
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos'))}");
exit;
}
?>
Try using the Facebook PHP SDK demo script here:
https://github.com/facebook/php-sdk/blob/master/examples/example.php
Once you have this script running - you can then begin to request calls, such as post to wall. If the call is successful, FB will prompt you with a Request for Permission.

Categories