Facebook Fan Page Wall Post from website using php sdk returning code parameter - facebook

I try to post facebook fan page wall from my website using php sdk 3.0.Its Working fine
Sometimes the redirecturl contains Code parameter and stopped all process.
The url look like www.mysite.com/posttofacebook.php?code='somerandomstring'
i am searching lot of times but i cant get correct solution.I have mention my complete code below.
Pls help me.
Thanks.
<?php
require_once 'src/facebook.php';
// configuration
$appid = 'myappid';
$appsecret = 'myappsecret';
$pageId = 'mypageid';
$msg ='Test ';
$title = 'TestTestTestTestTestTest';
$uri = 'http://www.welcome.com';
$desc = 'TestTestTestTestTestTestTestTestTestTestTest';
$pic = 'http://www.welcome.com';
$facebook = new Facebook(array(
'appId' => $appid,
'secret' => $appsecret,
'cookie' => false,
));
$user = $facebook->getUser();
if ($user)
{
try
{
$page_info = $facebook->api("/$pageId?fields=access_token");
if (!empty($page_info['access_token'])) {
$attachment = array(
'access_token' => $page_info['access_token'],
'message' => $msg,
'name' => $title,
'link' => $uri,
'description' => $desc,
'caption' => $caption,
'picture'=>$pic,
);
$status = $facebook->api("/$pageId/feed", "post", $attachment);
$msg="<h2 align='center'>Sucessfully Posted On Facebook Wall</h2>";
}
else
{
$status = 'No access token recieved';
}
}
catch (FacebookApiException $e)
{
error_log($e);
$user = null;
}
}
else
{
header("Location:{$facebook->getLoginUrl(array('scope' =>
'photo_upload,user_status,publish_stream,user_photos,manage_pages'))}");
}
?>

Related

Error 100 schedule a post on Facebook Page

I've used the following code. It works fine without 'scheduled_publish_time', otherwise I get this error "(#100) You cannot specify a scheduled publish time on a published post".
I've previously registered my app with another piece of code. It's so weird.
include_once("inc/facebook.php"); //include facebook SDK
$appId = '21xxxxxxxxxxx'; //Facebook App ID
$appSecret = '6b8f4bxxxxxxxxxxxxxd56'; // Facebook App Secret
$return_url = 'http://localhost:8888/...'; //return url (url to script)
$homeurl = 'http://localhost:8888/...'; //return to home
$fbPermissions = 'publish_stream,manage_pages'; //Required facebook permissions
//Call Facebook API
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret,
'cookie' => true,
'fileUpload' => true
));
$accounts = $facebook->api('/me/accounts');
$PAGE_ID = get_option('fb_post_cron_page'); // it is an option saved in WordPress
foreach($accounts['data'] as $account){
if($account['id'] == $PAGE_ID){
$ACCESS_TOKEN = $account['access_token'];
}
}
$post_url = '/'.$PAGE_ID.'/photos';
$upload_dir = wp_upload_dir();
$upload_dir= $upload_dir['path'];
$timezone= 'Europe/Rome';
$date = new DateTime($dateStr, new DateTimeZone($timezone));
//posts message on page statues
$args = array(
'access_token' => $ACCESS_TOKEN,
'source' => '#' . $image_abs_path,
'message' => $post_message,
'published' => true,
'scheduled_publish_time' => $date->getTimestamp()
);
try {
$postResult = $facebook->api($post_url, 'post', $args );
} catch (FacebookApiException $e) {
echo $e->getMessage();
}
you have to set 'published' to false
$args = array(
'access_token' => $ACCESS_TOKEN,
'source' => '#' . $image_abs_path,
'message' => $post_message,
'published' => false,
'scheduled_publish_time' => $date->getTimestamp()
);

Facebook PHP SDK - An active access token must be used to query information about the current user

I have problem with Facebook PHP SDK. It always throws that exception. I tried many solutions listed here, but nothing works for me.
It seems that Facebook returns to me valid access token, because I tested it with Debug tool in dashboard of my application.
What's my scenario?
I want to post to publish simple content to user's wall by calling static function:
function social_publish($network, $title, $message, $link = '', $image = '') {
global $_config;
// Initialize Facebook SDK
$facebook = new Facebook(array(
'appId' => $_config['fb_app']['app_id'],
'secret' => $_config['fb_app']['app_security_key']
));
// Set data
$attachment = array(
'name' => $title,
'caption' => $title,
'message' => $message,
'link' => $link,
'picture' => $image,
'actions' => array('name' => 'Test', 'link' => 'Link')
);
try {
$access_token = $facebook->getAccessToken(); // returns valid access token
$uid = $facebook->getUser(); // always return 0
$result = $facebook->api( '/' . $_config['fb_profile'] . '/feed/', 'post', $attachment); // $_config['fb_profile'] procudes 'me' in this case
} catch (FacebookApiException $e) {
echo $e->getMessage();
}
}
Just to note: I am not working on local environment.
problem solve it as asked in scope request to Facebook for authentication, as I decided to use the JavaScript SDK-and then, here's the solution:
FB.getLoginStatus(function(response) {
if ( ! response.authResponse ) {
FB.login(function(response) {
// handle the response if needed
}, {scope: 'publish_actions, publish_stream'});
}
});
Thank you! :-)

Facebook authenticating issue , 500 server erorr

From yesterday my web site getting 500 server error from facebook server side while trying to log in to the site,Im sure I have done nothing updating my code,I searched everything but havent found any solutions.
please check my code and help me.
<?php
session_start();
require 'config/fbconfig.php';
require 'facebook/facebook.php';
include"core.php";
connectdb();
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret' => APP_SECRET,
));
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
if (!empty($user_profile )) {
# User info ok? Let's print it (Here we will be adding the login and registering routines)
$username = $user_profile['name'];
$uid2 = $user_profile['id'];
$email = $user_profile['email'];
$validated = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM users WHERE email='".$email."'"));
if($validated[0]== 1){
$get_data = mysql_fetch_array(mysql_query("SELECT * FROM users WHERE email='".$email."'"));
$visits = $get_data['visits'];
$visits2 = $get_data['visits']+1;
$sid= keygen(30);
mysql_query("UPDATE users SET fbid='".$uid2."', visits= '".$visits2."', sid='".$sid."' WHERE email='".$email."'");
$_SESSION['sid'] = $sid;
$expire=time()+60*60*24*30;
setcookie("sid", $_SESSION['sid'], $expire);
if($get_data['cat_sec']==0){
header('Location: cat_select.php');
exit();
}else{
if(isset($_SESSION['curl'])){
header('Location:'.$_SESSION['curl']);
}else{
header('Location: index.php');
}
exit();
}
}else{
$sid= keygen(30);
$register = mysql_query("INSERT INTO users SET fname='".$username."', email='".$email."', fbid='".$uid2."', active='1' , sid='".$sid."'");
include_once"reg_fb_mail.php";
header('Location: cat_select.php');
exit();
}
$permissions = $facebook->api("/me/permissions");
if (array_key_exists('publish_stream', $permissions['data'][0])) {
$attachment = array(
'message' => 'my web site message',
'name' => 'www.mywebsite.com!',
'caption' => "caption.",
'link' => 'http://www.website.com',
'description' => 'website description gose here ',
'picture' => 'http://website.com/images/fb.png',
'actions' => array(
array(
'name' => 'www.website.com',
'link' => 'http://www.website.com'
)
)
);
$result = $facebook->api('/me/feed/', 'post', $attachment);
}
} else {
# For testing purposes, if there was an error, let's kill the script
die("There was an error.");
}
} else {
# There's no active session, let's generate one
$login_url = $facebook->getLoginUrl(array( 'scope' => 'email,publish_stream'));
header("Location: " . $login_url);
}
?>
I have attached all the code.Please help me to sort this issue .
Thank you.
Error figure outs ! The issue was not at this code.In facebook settings page I found there is a single space before the APP ID . its now fixed..
Facebook dosent trim or remove white spaces from get parameters in Authentication.
If there is a invalid character its getting a 500 server error in facebook.
I think this is a critical security issue.
Hal Rotholz - Thanks for your clue..

Facebook. After approve permission wrong redirect link

so I have a little problem, when first time user use my app It is redirected to:
http://www.facebook.com/MY_APP.../?state=f5c913239670dc934fae1274a81fa538&code=AQAmY5k8gRRt7woNKmpc_6z2eHgT-2B3prjNEDYFma1KqpQDKob-CS_bWfqd0qPkQD-tGoVdKUK8twGZRnLhoiotpZ3SlBeo3uusfmMyWDYStv-D79RvQEP8m9M8S8GYlbN3x1eLEd4D3RajWokTNAhRP8a1VB9VSKAb35xXFzg3PkheRKHqjr-nex5jcjM7Nv2B_JEV1w8AzM2LJg2M9kAb#_=_
And I get error: page not found
Durring permission approvig my link is: https://www.facebook.com/dialog/oauth?client_id=456080124457246&redirect_uri=http%3A%2F%2Fpadekime.wu.lt%2Fplaukai%2Findex.php&state=2c46a86dae43af0467da756bc3e52779&scope=publish_stream%2Cphoto_upload
I added redirect link and now It go to:
http://www.padekime.wu.lt/plaukai/kontekstas/?state=5abfd521df3c68ae55f26ad51a704743&code=AQDMmpZC-_Yxatq4xuAE60Tl2qW4pXCvDeghrbMGGu3Lg1X8zqrk-lvxRc3VOoYvgoLvATJhcsyykZnPoM7XI2sXih-nJhKrjx-HS3GFapELmC4KvX4KyN-VU-znHopkA-q_zcuIffJ0tY79CXPn7mo05BpxfvPntXaUAe7ymLf1p8Kg29eERaP5nw1dChbwwZ13FNY7BXD2ymAsFHPNH5zw#_=_
Possible to make that after permission approved go normally to my app http://www.facebook.com/Padekime/app_456080124457246 without /?state=..... ?
My code now looks like:
<?php
require_once('images/Facebook.php');
$facebook = new Facebook(array(
'appId' => '456080124457246',
'secret' => 'e2956a6e1de8791363faedb1ef44c408',
));
# Get User ID
$user = $facebook->getUser();
if ($user) {
try {
$redirectUri = 'http://www.padekime.wu.lt/plaukai/kontekstas/';
# Photo Caption
$photoCaption = 'Patarimų plaukams sužinojau čia http://goo.gl/otwhf';
# Absolute Path to your image.
$imageUrl = 'http://padekime.wu.lt/plaukai/images/PlaukaiNeuzvedus.jpg'; // Example URL
# Post Data for Photos API
$post_data = array(
'message' => $photoCaption,
'url' => $imageUrl,
'redirect_uri' => $redirectUri
);
$apiResponse = $facebook->api('/me/photos', 'POST', $post_data);
} catch (FacebookApiException $e) {
$user = null;
error_log($e);
}
} else {
$redirectUri = 'http://www.padekime.wu.lt/plaukai/kontekstas/';
$loginUrl = $facebook->getLoginUrl( array(
'scope' => 'publish_stream,photo_upload',
'redirect_uri' => $redirectUri
));
echo("<script>top.location.href = '" . $loginUrl . "';</script>");
}
?>
As I understand problem is somewhere here, but I cant solve it.
$loginUrl = $facebook->getLoginUrl( array(
'scope' => 'publish_stream,photo_upload'
));
echo("<script>top.location.href = '" . $loginUrl . "';</script>");
So if you know something about this, please help me, thank you very much.
SOLUTION:
I added ?ref=ts to $redirectUri:
$redirectUri = 'http://www.facebook.com/Padekime/app_456080124457246?ref=ts';
SOLUTION:
I added ?ref=ts to $redirectUri:
$redirectUri = 'http://www.facebook.com/Padekime/app_456080124457246?ref=ts';

Real-Time Checkins for a page?

cannot figure out how to get page checkins working through the real time api. Facebook says that page checkins are available through the API, but i'm not seeing how the appid connects with pageids. I do have my application added to the page i want to track. Here is the code I'm working against:
// Please make sure to REPLACE the value of VERIFY_TOKEN 'abc' with
// your own secret string. This is the value to pass to Facebook
// when add/modify this subscription.
define('VERIFY_TOKEN', 'acheckin');
$method = $_SERVER['REQUEST_METHOD'];
// In PHP, dots and spaces in query parameter names are converted to
// underscores automatically. So we need to check "hub_mode" instead
// of "hub.mode".
if ($method == 'GET' && $_GET['hub_mode'] == 'subscribe' &&
$_GET['hub_verify_token'] == VERIFY_TOKEN)
{
echo $_GET['hub_challenge'];
exit;
} else if ($method == 'POST')
{
//$updates = json_decode(file_get_contents("php://input"), true);
$message = 'wedidit!';//file_get_contents("php://input");
mail('ivan#ivanmayes.com', 'test', $message);
error_log('updates = ' . print_r($updates, true));
exit;
} else if ($method == 'GET' && $_GET['check_subscription'] == 'true' )
{
require_once 'facebook/facebook.php';
$facebook = new Facebook(array(
'appId' => '284647438227103',
'secret' => '162817ff51aacfb7c0d1420ee0f687ef'
));
$access_token = $facebook->getAccessToken();
$param = array('access_token' => $access_token);
$subs = $facebook->api('/284647438227103/subscriptions', $param);
var_dump($subs);
$message = 'checkemail';//file_get_contents("php://input");
mail('ivan#ivanmayes.com', 'test', $message);
error_log('checkingerrorlog');
exit;
}
require_once 'facebook/facebook.php';
$facebook = new Facebook(array(
'appId' => '284647438227103',
'secret' => '162817ff51aacfb7c0d1420ee0f687ef'
));
$access_token = $facebook->getAccessToken();
$param = array('access_token' => $access_token,
'object' => 'page',
'fields' => 'checkins',
'callback_url' => 'http://www.ivanmayes.com/arduino/checkins/checkins.php',
'verify_token' => 'acheckin'
);
$subs = $facebook->api('/284647438227103/subscriptions', 'POST', $param);