Facebook auto post on wall after like - php

So basically i am building a script for a client which is used as an iframe in his page. What it should do is when a user likes his page then he can see the contents and the "app" should auto post something to his wall (sounds like spam but im not sure if there is a problem with that). so i got the first bit working but for the love of god i cant make the second part.. here's my code
<? require 'facebook.php';
$app_id = "my_appid";
$app_secret = "my_secret";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$signed_request = $facebook->getSignedRequest();
function parsePageSignedRequest() {
if (isset($_REQUEST['signed_request'])) {
$encoded_sig = null;
$payload = null;
list($encoded_sig, $payload) = explode('.', $_REQUEST['signed_request'], 2);
$sig = base64_decode(strtr($encoded_sig, '-_', '+/'));
$data = json_decode(base64_decode(strtr($payload, '-_', '+/'), true));
return $data;
}
return false;
}
if($signed_request = parsePageSignedRequest()) {
if($signed_request->page->liked) {
echo "This content is for Fans only!";
} else {
echo "Please click on the Like button to view this tab!";
}
}
?>
i tried adding this on the "logged state"
$result = $facebook->api( '/me/feed/','post',
array('access_token' => $app_secret,
'message' => 'Playing around with FB Graph..')
);
with no luck as i get an error. please let me know if you can help. thanx

The first part, liking a url and autoposting to users stream has nothing todo with the code you show. this code is just a simple fan gateway.
no user is logedin.
The steps you would need are:
let the user login and ask for publish_stream permission (more info)
set a listener on the edge.create event (more info)
if this event fires send an ajax call to server to make the post. (more info)

Related

Post on facebook page by cron

I have read all the postings related to the title. But I have a very simple question. It will be very helpful if anyone can answer that.
What is me/account page in facebook?
I got many postings here such as
Posting to facebook company page with cron php server side
Everything is explained, but I know this sounds funny, but I stuck in the easiest step.
"When using Graph Explorer you would be required to navigate to /me/accounts end point,"
Please help me.
Please check the code
include 'includes/facebook.php';
$app_id = "XXXXXXXXXX";
$app_secret = "XXXXXXXXXXXXXX";
$page_id = "XXXXXXXXXXXX";
$my_url = "http://XXXXXXXXXXX.com";
$page_access_token = "XXXXXXXXXXXXXXX";
//Create the facebook object
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
//Write to the Page wall
try {
$attachment = array(
'access_token' => $page_access_token,
'message'=> "Hello World"
);
$result = $facebook->api('/639386542780904/feed', 'post', $attachment);
} catch(Exception $e) {
echo "error";
// ...
// mail($to, $subject, $message, $headers);
}
me/accounts is used to get a facebook page acces token, so that your app will post as that page. Elsewhere, your app will post as you on that page.

Facebook fan-gate doesn't work consistently

I'm using this code to check if the user has "liked" the page before going into my app.
require_once 'facebook-php-sdk/src/facebook.php';
// Create our Application instance.
$this->facebook = new Facebook(array(
'appId' => 'APPID',
'secret' => 'APPSECRET',
'cookie' => true,
));
$session = $this->facebook->getSession();
if(!empty($session)) {
$access_token = $this->facebook->getAccessToken();
$fql_multiquery_url = 'https://graph.facebook.com/me/likes?access_token='.$access_token;
$fql_multiquery_result = file_get_contents($fql_multiquery_url);
$fql_multiquery_obj = json_decode($fql_multiquery_result, true);
$liked = false;
foreach($fql_multiquery_obj['data'] as $like){
if($like['id'] == 'PageID'){
$liked = true;
}
}
if($liked){
$data['main_content'] = 'welcome_message';
} else {
$data['main_content'] = 'before_like';
}
$this->load->view('includes/template', $data);
} else {
$req_perms = "publish_stream,offline_access,user_status,email,read_stream,user_likes";
$login_url = $this->facebook->getLoginUrl(array('canvas'=> 1,'fbconnect' => 0,'req_perms' => $req_perms, 'redirect_uri' => 'APP REDIRECT URL'));
echo "<script type='text/javascript'>top.location.href = '$login_url';</script>";exit;
}
(I know looping through your likes isn't the best solution, but it seems to work the most consistent for me).
It works for me just fine (and a couple other users in the office works fine too), but it fails for a few users (of course they send me no error message). Is there a better way I can check for likes and have it be consistent?
You should be able to get to a specific page like this:
$resp = $this->api('/me/likes/'.$page_id);
$is_fan = (count($resp['data']) !== 0);
However this doesn't help if facebook's internal cache is acting up, also if this request runs on a facebook tab, the signed request should also have the fan/no_fan information in signed_request (see the part about fields and values). In my experience the signed_request seemed to be the most reliable.
P.S.:
You seem to be using a fairly old version of the php sdk, the getSession() method have been deprecated.
function parsePageSignedRequest() {
if (isset($_REQUEST['signed_request'])) {
$encoded_sig = null;
$payload = null;
list($encoded_sig, $payload) = explode('.', $_REQUEST['signed_request'], 2);
$sig = base64_decode(strtr($encoded_sig, '-_', '+/'));
$data = json_decode(base64_decode(strtr($payload, '-_', '+/'), true));
return $data;
}
return false;
}
$signed_request = $_REQUEST['signed_request'];
//check for page liked or not
if($signed_request = parsePageSignedRequest())
{
if($signed_request->page->liked) {
echo "<link rel='stylesheet' type='text/css' href='style.css' />";
} else {
echo "<link rel='stylesheet' type='text/css' href='notfanstyle.css' />";
}
}
:) try this, it worked for me.

Facebook Like Gate like_status is empty

so basically im trying to get a like gate working but the $like_status variable never changes so my condition to change to the liked content will never work. Does anyone know why this never changes? Have added my app to a page tab on facebook and it gets all the other variables $page_id, $page_admin, $country and $locale but not $like_status.
Thanks Charlie
See Code Below
<?php
enter code here
require_once('AppInfo.php');
// Enforce https on production
if (substr(AppInfo::getUrl(), 0, 8) != 'https://' && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
header('Location: https://'. $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
exit();
}
// This provides access to helper functions defined in 'utils.php'
require_once('utils.php');
require 'includes/facebook.php';
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => AppInfo::appID(),
'secret' => AppInfo::appSecret(),
'cookie' => true
));
$signed_request = $facebook->getSignedRequest();
$page_id = $signed_request["page"]["id"];
$page_admin = $signed_request["page"]["admin"];
$like_status = $signed_request["page"]["liked"];
$country = $signed_request["user"]["country"];
$locale = $signed_request["user"]["locale"];
echo "<br>page id = $page_id";
echo "<br>page admin = $page_admin";
echo "<br>like status = $like_status";
echo "<br>country = $country";
echo "<br>locale = $locale";
function grokSignedRequest()
{
if (isset($_REQUEST['signed_request']))
{
$encoded_sig = null;
$payload = null;
list($encoded_sig, $payload) = explode('.', $_REQUEST['signed_request'], 2);
$sig = base64_decode(strtr($encoded_sig, '-_', '+/'));
$data = json_decode(base64_decode(strtr($payload, '-_', '+/'), true));
return $data;
}
return false;
}
// call the function to parse the signed request
$sr_data = grokSignedRequest();
// check like status
if ($sr_data->page->liked==1)
{
include ('includes/index2.php');
}
else
{
include ('includes/index1.php');
}
?>
If you are running your PHP code on a tab page, like_status is probably actually present, but set to a value of 0 (zero). Thus, the echo statement seems to be outputting nothing for $like_status, but in reality, it has a value of zero. To prove this try code like this:
if ($like_status == 0)
echo "it's zero";
else
echo "it's not zero";
Edit: Sorry, I didn´t see that #robbie solved this in the comments of the question
The user must arrive to the App from the page tab, and:
As with a Canvas Page, you will not receive all the user information accessible to your app in the signed_request until the user authorizes your app.
https://developers.facebook.com/docs/appsonfacebook/pagetabs/#integrating

facebook iframe app; php sdk getUser() returns valid id on page one but not for any other page

I have a facebook iframe app which correctly logs in and authorizes the app, but getUser() only works on the first page. As soon as a user clicks a link to a new page within the iframe, getUser() returns 0.
What's strange is that this same code works for another app... I do all the clicking I want and getUser() returns a valid ID.
The app that doesn't work: https://apps.facebook.com/celestial_glory/
The one that does (same codebase): https://apps.facebook.com/uprisingstlouis/
Here's the code I am using:
require_once ('fb/facebook.php');
// snip... set $app_id, $secret, and $canvas_page
// first, try normal facebook getUser(). If that works, awesome.
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $secret,
));
$signed_request = $_REQUEST['signed_request'];
// Get User ID
$user = $facebook->getUser();
if ($user != '0') return 'fb=' . $user; // works once
// getUser() didn't work. Try oAuth. Maybe user needs to log in or
// authorize the game?
$auth_url = 'http://www.facebook.com/dialog/oauth?client_id='
. $app_id . '&redirect_uri=' . urlencode($canvas_page);
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
if (empty($data["user_id"])) {
echo '<a target="_top" href="' . $auth_url . '">Login to Facebook</a>';
exit;
// normally we would auto-redirect, but with a uid of 0, this just auto-redirects
// echo("<script> top.location.href='" . $auth_url . "'</script>");
} else {
return 'fb=' . $data['user_id'];
}
any ideas? I have triple-checked app ids and secrets and canvas pages. If those were wrong, I expect no page, not even the first, would work.
Change Facebook PHP-SDK initialization to:
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $secret,
'cookie' => true // this!
));
getUser works on the first page because it can get the user from signed_request (POST'ed by Facebook to your canvas page URL). Thus you need some way to track your user once he starts navigation deeper within your application. You could pass signed_request somehow all by yourself or simply enable built-in PHP-SDK cookie support as suggested above.

Facebook: Check if User liked Facebook Fanpage Tab

I know, that my question was asked a lot of times here, but for me no answer worked.
I've created a Facebook Fanpage Tab. All the files are stored at my private Webspace.
Now I want to determine if a user already liked the page or haven't liked it yet!
So I used this code:
<?php
function parsePageSignedRequest() {
if (isset($_REQUEST['signed_request'])) {
$encoded_sig = null;
$payload = null;
list($encoded_sig, $payload) = explode('.', $_REQUEST['signed_request'], 2);
$sig = base64_decode(strtr($encoded_sig, '-_', '+/'));
$data = json_decode(base64_decode(strtr($payload, '-_', '+/'), true));
return $data;
}
return false;
}
if($signed_request == parsePageSignedRequest()) {
if($signed_request->page->liked) {
$isteinfan = "false";
}
else {
$isteinfan = "true";
}
}
//PHP Variable an JavaScript übergeben
echo "<script>";
echo "isteinfan = '$isteinfan';";
echo "console.log('ist ein fan: ');";
echo "console.log(isteinfan);";
echo "</script>";
?>
But it doesn't work.
Can u give me help, please!!!
Yours, Raphael
I would recommend you include the facebook php library, which you can download from https://github.com/facebook/php-sdk/tree/master/src. You have to place all the three files in the same directory. Then you can get the liked status very easily:
define('APP_ID','xxxxxxxx');
define('APP_SECRET','xxxxxxxx');
require ("facebook.php");
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret' => APP_SECRET,
'cookie' => true,
));
$signed_request = $facebook->getSignedRequest();
$liked = $signed_request["page"]["liked"];
Now $liked is a boolean which can be true or false

Categories