Real-Time Checkins for a page? - facebook

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);

Related

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

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'))}");
}
?>

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 API sometimes doesn't load

I'm having problem when using
$facebook->api('/me','GET');
When loading the page for the first time, nothing happen. I have to reload the page again to make the script work. Not sure where the problem is.
require_once('src/facebook.php');
$app_id = "APP_ID";
$app_secret = "APP_SECRET";
// Init facebook api.
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true,
));
//Facebook Authentication part
$user_id = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'publish_stream, user_likes'
)
);
if (!$user_id) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
$user_profile = $facebook->api('/me','GET');
$user_gender = $user_profile['gender'];
if($user_gender == 'male'){
echo "you are male";
} else {
if ($user_gender == 'female'){
echo "you are female";
} else{
echo "gender not specified";
}
}
I tried take the $facebook->api('/me','GET'); out and echo $user_id, for example, it works.

Facebook photo tagging app not tagging people

I have a facebook photo tagging app that used to work a few months ago but no longer does. The user authenticates the app and it uploads a picture on their facebook account and tags their friends. The app uploads the picture but stopped tagging for some reason.
require_once('facebook.php');
$_SESSION['init'] = true; $current_date=date('m/d/Y'); $facebook =
new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET,
'cookie' => true,
));
$facebook->setFileUploadSupport(true);
$session = $facebook->getSession(); $tokenorig =
$facebook->getAccessToken();
$friends =
file_get_contents("https://graph.facebook.com/me/friends?access_token="
. $tokenorig);
$friends = json_decode($friends, true);
$friends = $friends['data'];
foreach($friends as $friend) {
$uids[] = $friend['id'];
}
function makeTagArray($userId) {
$x=1; $y=1;
foreach($userId as $id) {
$tags[] = array('tag_uid'=>$id, 'x'=>$x,'y'=>$y);
$x+=1;
$y+=1;
}
$tags = json_encode($tags);
return $tags;
}
$arguments = array(
'message' => 'hi guys ',
'tags' => makeTagArray($uids),
'source' => '#' .realpath('pic2.jpg'),
);
$alb = "13378";
uploadPhoto(
$facebook,
$alb,
$arguments,
$tokenorig);
function uploadPhoto($facebook,$albId,$arguments,$tokenorig) {
//https://graph.facebook.com/me/photos
try {
$fbUpload =
$facebook->api('/'.$albId.'/photos?access_token='.$tokenorig,'post',
$arguments);
return $fbUpload;
} catch(FacebookApiException $e) {
echo "eror";
echo $e;
// var_dump($e);
return false;
}
}
//////////end
First off all,
You need user's to authorize your application to upload photos and tag on behalf of them and
you need publish_stream and user_photos permissions to tag user's friends