Post data to facebook page with no user interaction - php

After I was searching how to post data to facebook page, now I can do it well.
<?php
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => '491652477512398',
'secret' => 'de3d98a619ade1afb4152d6be90acdb9',
));
$user_id = $facebook->getUser();
echo "User data ".$user_id." \n";
try {
if($user_id) {
$ret_obj = $facebook->api('/microdigit.it/feed/', 'POST',
array(
//'access_token' => 'AAACEdEose0cBAExkj6B4lnjpUtfSSVTOZA9CuvMA1SDjyCQWRnfZCbc1SimyRsZCTa6CUFZB2Q3ZBfVIv0qJmW13XkPZASpt9UDE4qQI488mDqlznDZA2ih',
'message' => '*** Microdigit Microdigit 000 ****',
'name' => 'name 777 after log out test',
'caption' => 'this is caption for action link',
'description' => 'here goes description and links http:anotherfeed.com | http://facebook.com/anotherfeed',
'picture' => 'http://www.google.com/tv/images/slivetv.png',
'link' => 'www.yahoo.com',
'properties' => array(
array('text' => 'test link 1', 'href' => 'http://anotherfeed.com/?ref=1'),
array('text' => 'test link 1', 'href' => 'http://anotherfeed.com/?ref=1'),
),
));
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
}else
{
$login_url = $facebook->getLoginUrl( array( 'scope' => 'offline_access,publish_stream' ) );
echo 'Please login.';
}
} catch(FacebookApiException $e) {
echo $e;
$login_url = $facebook->getLoginUrl( array(
'scope' => 'offline_access,publish_stream'
));
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
?>
But problem is I want to post
that information to page without needing any user interaction.
I mean whenever I run this code, If user is already log off,
my program need user interaction (such as login to facebook) before it post to facebook page.
So I will be appreciated every suggestion that can show me how to post data to facebook without needing any user interaction.

One way of doing it, depending on the trigger you want to use is to make use of a Cron Jobs . You can schedule to run php scripts.

Related

how to auto share in facebook once form is submitted

I have a php form for facebook for a certain media house.To fill the form, the media house facebook profile should be liked first. When the profile is liked then only the form is accessible. I have so far succeeded in that. Now when the form is successfully submitted I want to share some info as XXX applied for "YYYY" campaign automatically in the fb profile of XXX. Is it possible? I have done facebook sharing with asking permission only. Is it possible to share some info after successfully form submission without asking for permission.
Any help / suggestions are welcome. Thanks in advance.
In success page I added the following code:
Before
<?php
require_once('php-sdk/facebook.php');
$config = array(
'appId' => 'XXX',
'secret' => 'YYY',
'allowSignedRequest' => false,
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 2;
$message = 'Message Here;
?>
Then in body part:
<?php
if($user_id){
try(){
$ret_obj = $facebook->api('/me/feed', 'POST', array(
'link' => 'https://www.facebook.com/myrepublica',
'message' => $message,
)
);
}catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream',
));
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
}else{
$login_url = $facebook->getLoginUrl( array( 'scope' => 'photo_upload') );
echo 'Please login.';
}
?>
Hope this helps someone.

Redirect loop when trying to publish a checkin via Facebook API

I am trying to publish a checkin using facebook api. Sometimes, it works well and posts the checkin, but mostly 99% of the times it produces the error: "This webpage has a redirect loop"
checkin.php:
<?php
require("../src/facebook.php");
// construct the object with your facebook app data
$facebook = new Facebook(array(
'appId' => 'XXXXX',
'secret' => 'XXXX',
'cookie' => false
));
$token = $facebook->getAccessToken();
//echo $token;exit())
try {
// to get the id of the currently logged in user
// if, you want you can manually set a user id here like this:
//$uid = '[FB USER ID]';
$uid = $facebook->getUser();
$facebook->setAccessToken($token);
$facebook->api('/'.$uid.'/checkins', 'POST', array(
'access_token' => $facebook->getAccessToken(),
'place' => '101697613295949',
'message' => 'Enjoying Chill Beer with Team',
'picture' => 'http://test.com/someplace.png',
'coordinates' => json_encode(array(
'latitude' => '28.541203543000023',
'longitude' => '77.15503053709995',
'tags' => 'XXXX'))
));
echo 'You are checked in';
} catch (Exception $e){
// No user found - ask the person to login
$login_url = $facebook->getLoginUrl();
header("Location: ".$login_url);
}
?>
Thanks in advance.

Facebook post wall data when facebook offline ( access_token = staticValue )

I am trying to post dynamic data to facebook wall.
I downloaded facebook api for php.
First of all, as for testing, I use below code.
<?php
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require '../src/facebook.php';
$config = array(
'appId' => 'myAPPID',
'secret' => 'MySecretCode'
);
//'access_token' => $access_token = $facebook->getAccessToken(),
$facebook = new Facebook($config);
$attachment = array(
'access_token' => 'my_tokenkey',
'message' => 'Test Message offline test message',
'name' => 'Name Test offline test...',
'description' => 'here goes description and links http:anotherfeed.com | http://facebook.com/anotherfeed',
'caption' => 'this is caption for action link',
'picture' => 'http://www.google.com/tv/images/slivetv.png',
'link' => 'www.yahoo.com',
'properties' => array(
array('text' => 'test link 1', 'href' => 'http://anotherfeed.com/?ref=1'),
array('text' => 'test link 1', 'href' => 'http://anotherfeed.com/?ref=1'),
),
);
try {
$facebook->api('/me/feed', 'POST', $attachment);
} catch(FacebookApiException $e) {
echo $e;
# handling facebook api exception if something went wrong
}
?>
As for my program requirement, I want my program to post data to facebook's wall whether facebook is online or offline.
But when I try this code, I get error.
OAuthException: An active access token must be used to query information about the current user.
Could anyone please give me suggestions?

Facebook PHP SDK app to upload photo to page

I am trying to create a Facebook application that uploads images to a page. I can manage to upload photos to the user's photo album but not to the page's.
Here's my code:
require_once('php-sdk/facebook.php');
$config = array(
'appId' => 'XXXXXXXX',
'secret' => 'YYYYYYYYYYYYYYYY',
'fileUpload' => true,
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$photo = $_POST['image'];
$message = "Lorem ipsum";
echo $user_id;
if ($user_id) {
$ret_obj = $facebook->api('/XXXXXXXXXX/photos', 'POST', array(
'access_token' => $access_token,
'source' => '#' . $photo,
'message' => $message
)
);
} catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl( array(
'scope' => 'user_photos,publish_stream,photo_upload'
));
echo 'Please login.';
print_r($e);
error_log($e->getType());
error_log($e->getMessage());
}
echo '<br />logout';
} else {
$login_url = $facebook->getLoginUrl( array( 'scope' =>user_photos,publish_stream,photo_upload,manage_pages' ) );
echo 'Please login.';
}
Any help would be much appreciated.
When you do:
$facebook->api('/XXXXXXXXXX/photos', 'POST', array(
... is XXXXXXXXXX your appId or the albumId? You must do the call to your album ID.
Also make sure you are using the correct $access_token. To make sure look at:
https://graph.facebook.com/yourPageID?fields=access_token&access_token=YourPersonalAccessToken
where yourPersonalAccessToken is:
A Page admin access_token for this page; The current user must be an
administrator of this page; only returned if specifically requested
via the fields URL parameter.
Your must grant to graph api manage_pages permissions.

Problem with posting to Facebook Page using PHP script as Page account

I am using the following PHP code to post to my page wall. it is okay I can post successfully as admin but I want to post to the wall using the page name (page account). how do I post using the page name or page id. I tried to find good resource to explain Facebook API function but i did not find good documentation for it.
<?
require '../src/facebook.php';
$app_id = "XXX";
$app_secret = "YYY";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$user = $facebook->getUser();
//echo $user;
if(($facebook->getUser())==0)
{
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos,offline_access'))}");
exit;
}
else {
echo "i am connected";
}
$attachment = array('message' => 'this is my message',
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'example.org',
'description' => 'this is a description',
'picture' => 'http://example.org/image.gif',
'actions' => array(array('name' => 'Get Search',
'link' => 'http://www.google.com'))
);
$status = $facebook->api('/123456789/feed', 'POST', $attachment); // my page id =123456789
?>
I found the solution to the problem.
To post or to do any task as a page , you need an access token.
Here is my final code after the update, I want to share it with you because I found many people having difficulty with getting the correct access code for a page.
<?
require '../src/facebook.php';
$app_id = "XXX";
$app_secret = "YYY";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$user = $facebook->getUser();
//echo $user;
if(($facebook->getUser())==0)
{
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos,offline_access,manage_pages'))}");
exit;
}
else {
$accounts_list = $facebook->api('/me/accounts');
echo "i am connected";
}
//to get the page access token to post as a page
foreach($accounts_list['data'] as $account){
if($account['id'] == 123456789){ // my page id =123456789
$access_token = $account['access_token'];
echo "<p>Page Access Token: $access_token</p>";
}
}
//posting to the page wall
$attachment = array('message' => 'this is my message',
'access_token' => $access_token,
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'example.org',
'description' => 'this is a description',
'picture' => 'http://example.org/image.gif',
'actions' => array(array('name' => 'Get Search',
'link' => 'http://www.google.com'))
);
$status = $facebook->api('/123456789/feed', 'POST', $attachment); // my page id =123456789
var_dump($status);
?>
My Facebook OAuth is a little rusty, but I believe this will help:
http://bugs.developers.facebook.net/show_bug.cgi?id=10005
It is the exact bug you are describing, but I have no way of testing that it corrects the issue specifically.
This should help: http://developers.facebook.com/docs/reference/api/page/#feed
It looks like you need the manage_pages permission.

Categories