Hello,
I am building a facebook page tab, but feeding in gravity froms from a wordpress install on the same server.
The form is an ajax gravity form, which is echo'ed in the index.php file via do_shortcode.
I am getting the wordpress link via calling the wp-load.php
The problem I am having is more facebook related. The problem is I cant seem to echo the current facebook user name.
You will see I'm trying to echo $user['name']; in various places in the code below, but I cannot for the life of me get facebook output any graph data :/
Everything else works, my gravity form works, my signed in request variable works, the facebook sdk appears to be loading ok, my channel file is in place, and I am previewing my tab within facebooks tab iframe.
But I just can't retrieve the graph data - and I have checked all my app secret/app ID about a million times. I am using the newest SDK found on git hub. Here is where I took the source from... https://github.com/facebook/facebook-php-sdk
Can any one please test my code below in a facebook app. Technically I should not have to fill any of the app canvas urls in the facebook app settings, just my page tab settings. But I did both settings to be sure. Please see [[LINK href="http://i.imgur.com/X5LZg.jpg"]]screenshot[[/LINK]] of my settings here... [[LINK href="http://i.imgur.com/X5LZg.jpg"]]http://i.imgur.com/X5LZg.jpg[[/LINK]]
Thanks in advance for anyhelp to see where I am going wrong.
<?php
define('WP_USE_THEMES', false);
$fb_app_id = '000000000000000';
$fb_secret = '000000000000000000000000000000';
require('/home/sites/example.co.uk/www/wp/wp-load.php');
require 'facebook-php-sdk-98f2be1/src/facebook.php';
// CREATE FACEBOOK APPLICATION INSTANCE
$facebook = new Facebook(array(
'appId' => $fb_app_id,
'secret' => $fb_secret,
'cookie' => true
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// GRAVITY FORMS HOOKS
add_filter('gform_field_value_facebook_name', 'facebook_name_population_function');
function facebook_name_population_function($value){
return $user['name'];
}
$signedRequest = $facebook->getSignedRequest();
?>
<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<meta name="robots" content="noindex, nofollow">
<meta property="fb:app_id" content="<?php echo $fb_app_id ?>" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><?php echo $user['name']; ?>, Feedback Form</title>
<link rel="profile" href="http://gmpg.org/xfn/11" />
<link rel="shortcut icon" href="<?php bloginfo('url'); ?>/favicon.ico" />
<link rel="stylesheet" type="text/css" media="all" href="style.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
</head>
<body>
<h1><?php echo $user['name']; ?>, Feedback Form</h1>
<?php if ( ( !$signedRequest['page']['liked'] ) == 1 ) {
echo '<p>You must like us!</p>';
} else {
echo do_shortcode('[gravityform id="2" name="Feedback Form" title="false" ajax="true"]');
} ?>
<div id="fb-root"></div>
<script>
// FACEBOOK
window.fbAsyncInit = function() {
FB.init({
appId: '<?php echo $fb_app_id; ?>',
channelUrl : '//www.example.co.uk/channel.html', // Channel File
cookie: true,
xfbml: true,
oauth: true,
status: true // check login status
});
FB.Canvas.setAutoGrow(true);
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
<?php if ( ( $signedRequest['page']['liked'] ) == 1 ) { ?>
$(document).ready(function($) {
$('#gform_2').attr('action','../wp/#gf_2');
$(document).bind('gform_post_render', function(){
$('#gform_2').attr('action','../wp/#gf_2');
});
});
<?php } ?>
</script>
</body>
</html>
Michael Caputo answers:
Just curious how long you've waited for it to work? I'm not sure, but there may be a delay from when you set up your Facebook app.
Michael Caputo comments:
Also could this line be the problem?
channelUrl : '//www.example.co.uk/channel.html', // Channel File
Michael Caputo comments:
Scratch that, I see now that you've replaced your URL with example.
Josh Cranwell comments:
Set up the app on the friday.
Graph API should be instant I thought - never had this problem in past :(
This how facebook generally ask you to the channel file - I changed my URL to //www.example.co.uk
My channel file consists of...
<?php
$cache_expire = 60*60*24*365;
header("Pragma: public");
header("Cache-Control: max-age=".$cache_expire);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$cache_expire) . ' GMT');
?>
<script src="//connect.facebook.net/en_US/all.js"></script>
Michael Caputo comments:
Just found this, might be related:
http://facebook.stackoverflow.com/questions/6143468/facebook-facebook-getsignedrequest-error
Josh Cranwell comments:
Not sure if it is..
$signedRequest = $facebook->getSignedRequest();
this should work better than this surely?
My signed in request is working tho, If someone has not liked the pages, then they have to like it before the form can be entered.
I tried using this instead but still no graph data echo'ing???
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
Arnav Joy answers:
try getting the values return by facebook by
<?php print_r( $user ) ;?>
where you are using echo $user['name'] ,
also try this
<?php 'I am got printed here'. echo $user['name'] ?>
tell me the output of both the result.
Josh Cranwell comments:
Hello Arnav,
Sorry I made a mistake in the my code above.
It should be this <?php echo $user['name']; ?> - but it echo's nothing?
I tried this... <?php print_r( $user ) ;?> but this does not work either?
Have you got a facebook app you can test it with, shall I make you admin of my app
Thanks