Error 100 schedule a post on Facebook Page - facebook

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

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

Why can't I redirect to Facebook's getLoginUrl()?

<?php
$fb = new Facebook(array(
'appId' => $fb_config['app_id'],
'secret' => $fb_config['secret'],
));
$params = array(
'scope' => 'email, read_stream, user_interests, user_likes, user_location, user_status',
'redirect_uri' => 'http://myurl/facebook_connect',
);
$fb_login_url = $fb->getLoginUrl($params);
$this->redirect($fb_login_url);
$user = $fb->getUser();
print_r($user); // returns 0
?>
In the example above, if I redirect to the getLoginUrl result, the getUser() method returns zero.
Is there a reason why this is happening?
Replace Facebook PHP SDK path, appId, secret and redirect_uri with your app settings and Try:
require_once dirname(__FILE__) . '/facebook/facebook.php'; // You need Facebook PHP SDK
$facebook = new Facebook(array(
'appId' => $fb_config['app_id'], // Your Facebook app id
'secret' => $fb_config['secret'], // Your Facebook app secret
'cookie' => true
));
$fb_user_id = $facebook->getUser();
if ($fb_user_id) {
try {
$fb_user_profile = $facebook->api($fb_user_id);
var_dump($fb_user_profile);
} catch (FacebookApiException $e) {
error_log($e);
}
} else {
$params = array(
'scope' => 'email, read_stream, user_interests, user_likes, user_location, user_status',
'redirect_uri' => 'http://myurl/facebook_connect', // Replace with your app url
);
$facebook_login_url = $facebook->getLoginUrl($params);
echo '<script>top.location="' . $facebook_login_url . '";</script>';
exit();
}

Get birthdays of all facebook friends in php

My Code
require 'src/facebook.php';
$app_id = 'My App Id';
$app_secret = 'My App Secret Id';
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true,
));
$user = $facebook->getUser();
if ($user) {
$user_albums = $facebook->api('/me/friends?fields=id,name,birthday');
}
if ($user) {
$params = array( 'next' => 'http://localhost/friends_bday/logout.php?logout=1' );
$logoutUrl = $facebook->getLogoutUrl($params);
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_photos'
));
}
What I require
The above code only provides friends id and name but not birthdays. I have done some researching and was unsuccessful in finding a solution.
You probably need to request permission of the user to access the birthday information of their friends.
The Facebook permission you are looking for is 'friends_birthday'.
Request the user their access token with the above permission and set the access token.
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true,
)
);
$facebook->setAccessToken($userAccessToken);
$user = $facebook->getUser();
if ($user) {
$user_albums = $facebook->api('/me/friends?fields=id,name,birthday');
}
if ($user) {
$params = array( 'next' => 'http://localhost/friends_bday/logout.php?logout=1' );
$logoutUrl = $facebook->getLogoutUrl($params);
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_photos, friends_birthday'
));
}
Actually I think you just have to add friends_birthday to the scope.

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! :-)

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