Facebook graph api /comment breaking whole program - php

Am, building an application which would post comments on users birthday wish post, but,the problem is with comment graph api, the statement is executing once and the whole application is breaking up, I did debug with echos, and its the same, no code is executing after the one comment. please help
<?php
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => '',
'secret' => ''
));
$app_url = "https://apps.facebook.com/appleappspace/";
$uid = $facebook->getUser();
?>
<html>
<head>
<body>
<?php
if(!$uid){
$params = array(
'scope' => 'user_birthday,user_friends,friends_birthday,friends_status,
read_stream,email,status_update,publish_actions',
'redirect_uri' => 'http://apps.facebook.com/appleappspace/'
);
$loginUrl = $facebook->getLoginUrl($params);
echo "<script type='text/javascript'> top.location.href = '$loginUrl';</script>";
}else if($uid){
try{
$strings = array("b'day","birthday","bday","hbd","bdae","Birthday");
echo "Successfully Logged In <br/>";
$yesterday = mktime(0,0,0,date("m"),date("d")-3,date("Y"));
$yesterday = date("Y-m-d",$yesterday);
$user_wishes = $facebook->api('/me/feed?since='.$yesterday,'GET');
$length_user_wishes = count($user_wishes['data']);
$string_length = count($strings);
for($i=0;$i<=$length_user_wishes;$i++){
for($j=0;$j<=$string_length;$j++){
if(strchr($user_wishes['data'][$i]['message'],$strings[$j]) !== FALSE){
$POST_ID = $user_wishes['data'][$i]['id'];
$comment = "Its not Working";
$facebook->api('/'.$POST_ID.'/likes','POST');
$facebook->api('/'.$POST_ID.'/comments','POST',array('message' => $comment));
// nothing executes after this statement, whole program breaks up.
}
}
}
} catch(FacebookApiException $e) {
$echo($e);
$uid = NULL;
}
}
?>
</body>
</head>
</html>

Related

facebook php sdk not working

i used two codes and its not working the first one :
<?php
require_once('fb/facebook.php');
$config = array(
'appId' => 'xxxxx',
'secret' => 'xxxxx',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?php
function render_login($facebook) {
$canvas_page = 'http://fbbost.eb2a.com/';
// HERE YOU ASK THE USER TO ACCEPT YOUR APP AND SPECIFY THE PERMISSIONS NEEDED BY
$login_url = $facebook->getLoginUrl(array('scope'=>'email,user_photos,friends_photos', 'redirect_uri'=>$canvas_page));
echo 'Please login.';
}
if($user_id) {
try {
$user_profile = $facebook->api('/me','GET');
echo "Name: " . $user_profile['name'];
} catch(FacebookApiException $e) {
render_login($facebook);
echo "1";
error_log($e->getType());
error_log($e->getMessage());
}
} else {
render_login($facebook);
echo "2";
}
?>
</body>
</html>
and the second one :
<html>
<head>
<title>NNN</title>
</head>
<body>
<?php
include "fb/facebook.php";
$facebook=new Facebook(array(
'appId' => 'xxxxx',
'secret' => 'xxxxx',
'cookie' => true
));
$session=$facebook->getUser();
$me=null;
if($session){
try{
$me=$facbook->api('/me');
print_r($me);
}
catch (FacebookApiException $e){
echo $e->getMessage();
}
}
if($me){
$logoutUrl=$facbook->getLogoutUrl();
echo "<a href='$logoutUrl'>Logout</a>";
}
else{
$loginUrl=$facebook->getLoginUrl(array(
'scope' => 'publish_stream,read_friendlists'
));
echo "<a href='$loginUrl'>Login</a>";
}
?>
</body>
</html>
both codes return the same error when i login :
App Not Setup: The developers of this app have not set up this app properly for Facebook Login.
I think your app is not public. You need to set it to public to use it with any Facebook account other than the developer's own account. Go to "Status and Review" and make the app public.
Besides, check that you have properly setup the app domain.

How can I get facebook statuses from an account

I'm trying to get facebook user statuses from their feed. Here is my code:
<?php
require_once('facebook.php');
$config = array(
'appId' => 'my_app_id',
'secret' => 'my_app_secret',
'allowSignedRequest' => false
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();//get user_id here...
?>
<html>
<head></head>
<body>
<?php
if($user_id) {
try {
$status = $facebook->api('/me/posts?limit=1');//get posts here...
echo "The status: " . $status['message'];//print the message of posts...
} catch(FacebookApiException $e) {
$params = array(
'scope' => 'email, read_stream, user_interests, user_likes, user_location, user_status',
'redirect_uri' => 'http://localhost/facebook/index.php', // Replace with your app url
); //permission = read_stream, user_status
$login_url = $facebook->getLoginUrl($params);
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
}
?>
</body>
</html>
But the result is empty. Could anyone advise me on how to solve this problem?
Thank you so much!
You are reading returned data in wrong way, it should be;
$response = $facebook->api('/me/posts?limit=1');//get posts here...
$status = null;
foreach($response["data"] as $item) {
$status = $item["message"];
}
echo "The status: " . $status;
You can test your queries here for returned data format

Not getting any information from Facebook login

I'm implementing Facebook login on my website, it's working without error but ultimately I'm not able to get any information of a user.
I've following code.
<?php
include 'library.php';
include 'facebook.php';
$app_id = "XXXXXXXXX";
$app_secret = "XXXXXXXXX";
$site_url = "XXXXXXXXX";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
$user = $facebook->getUser();
if($user){
try{
$user_profile = $facebook->api('/me');
}catch(FacebookApiException $e){
$user = NULL;
}
}
if($user){
$logoutUrl = $facebook->getLogoutUrl();
}else{
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email',
'redirect_uri' => $site_url,
));
}
if($user){
Echo "Email : " . $user_profile['email'];
}
$user_fbid = $fbuser;
$user_email = $user_profile["email"];
$user_fname = $user_profile["first_name"];
$user_image = "https://graph.facebook.com/".$user_fbid."/picture?type=large";
echo $loginUrl = $facebook->getLoginUrl( array('scope' => 'email,read_stream'));
What's wrong with this? Can anyone point out? I've gone though a number of questions but none of them seemed to work for me & I finally ended up posting a question myself.
EDIT
When asking for login, it displays permissions correctly like email, friend list, your likes etc. So basically it's asking correctly & even though I approve, information is not coming up. Tried with a couple of users.
EDIT
Try use this code, and work from there ... That's how I figured it out
<HTML>
<BODY>
<?php
require_once("fb/facebook.php");
$config = array(
'appId' => 'XXXXX',
'secret' => 'XXXXXXX',
'fileUpload' => false, // optional
'allowSignedRequest' => false, // optional, but should be set to false for non-canvas apps
);
$facebook = new Facebook($config);
$uid = $facebook->getUser();
$loginParams = array('scope' => 'email');
$loginUrl = $facebook->getLoginUrl($loginParams);
$logoutParams = array('next' => 'http://fb_logouturi');
$logoutUrl = $facebook->getLogoutUrl($logoutParams);
echo $uid . '</br>';
echo 'Login:' . $loginUrl . '</br>';
echo 'Logout:' . $logoutUrl . '</br>';
if($uid) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$user_profile = $facebook->api('/me','GET');
echo "</br>First Name: " . $user_profile['first_name'];
echo "</br>Last Name: " . $user_profile['last_name'];
echo "</br>Email: " . $user_profile['email'];
echo "</br>Gender: " . $user_profile['gender'];
echo "</br><img src='http://graph.facebook.com/" . $uid ."/picture?type=large'>";
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, print a link for the user to login
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
}
?>
</BODY>
</HTML>

Facebook app reloads himself after permission grant

I have a little Facebook app, that posts some preloaded images. I changed the code structure, so i moved all the variables into config.php, so it would be more comfortable. But after that the app doesn't work and i really don't know why... After the permission dialog it realoads himself over and over... Can someone fing the bug?
This is the index.php:
<?php
require 'facebook.php';
require 'config.php';
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret,
'baseUrl' => $baseUrl,
'appBaseUrl' => $appBaseUrl,
'fileUpload' => 'true',
'cookie' => 'true'
));
if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) {
$baseUrl = str_replace('http','https',$baseUrl);
$fbPageURL = str_replace('http','https',$fbPageURL);
$fbPageAppURL = str_replace('http','https',$fbPageAppURL);
}else{
// not https
}
$signed_request = $facebook->getSignedRequest();
$page_id = $signed_request["page"]["id"];
$like_status = $signed_request["page"]["liked"];
if(!$like_status){
header("Location: notfan.php");
exit;
}
$user = $facebook->getUser();
$params = array(
scope => 'publish_stream,user_photos',
redirect_uri => $fbPageAppURL
);
$loginUrl = $facebook->getLoginUrl($params);
$app_data = $signed_request["app_data"];
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title><?php echo $appName; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style>
a:link {color:#f1effc;}
a:visited {color:#f1effc;}
a:hover {color:#19378d;}
a:active {color:#19378d;}
a:link {text-decoration: none}
</style>
</head>
<body>
<script type="text/javascript">
function NotInFacebookFrame() {
return top === self;
}
function ReferrerIsFacebookApp() {
if(document.referrer) {
return document.referrer.indexOf("apps.facebook.com") != -1;
}
return false;
}
if (NotInFacebookFrame() || ReferrerIsFacebookApp()) {
top.location.replace("<?= $fbPageAppURL ?>");
}
</script>
<?php if($user){
try {
$user_profile = $facebook->api('/me');
$scope = 'publish_stream,user_photos';
$scope_params = explode(',',$scope);
$permissions = $facebook->api("/me/permissions");
if( array_key_exists('publish_stream', $permissions['data'][0]) && array_key_exists('user_photos', $permissions['data'][0])) {
// the main app logic here
} else {
$user = null;
}
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
// logged in user
} else {
// not logged in or not authenticated
echo '<script type="text/javascript">top.location.href = "'.$loginUrl .'";</script>';
}
?>
</body>
</html>
and the config.php:
<?php
$appName = 'the name';
$appId = '123456789';
$appSecret = '1324v3434v34v';
$baseUrl = 'http://www.domain.com/app-name/';
$appBaseUrl = 'http://apps.facebook.com/zzzzzzz/';
$fbPageURL = 'http://www.facebook.com/xxxxxxx';
$fbPageAppURL = $fbPageURL.'?sk=app_'.$appId;
?>
Thank you very much!!
As it turned out, it was a server configuration failure.
Wrap $user = $facebook->getUser(); in a try/catch and echo the exception that gets caught. That will tell you why the user is not getting logged on.
More details - after the comments below.
You check document referrer in your Javascript - but the actual referrer in my tests was "http://static.ak.facebook.com/platform/page_proxy.php?v=4" so this would fail that condition. This will causes a loop.
To check if you're in a canvas, just get the signed_request using the PHP SDK
$facebook->signed_request();
If this is empty then you're not in a canvas (or a page tab) so redirect to the canvas. Do that in PHP and not in Javascript.

APP on redirect_url shows blank page

I have a Facebook PHP SDK APP in development phase and i'm stuck on something...
The logic of the APP is this:
first the user have to like the page (app is on this) // works very well :)
after that comes the permissions request
if permission is granted: the app_data variable is pass through and the user recieves a picture.
app_data variable is necessary because, if the user clicks the app after permission granted again, it gets new picture automaticly. with the app_data don't.
my problem is this link:
$params = array(
scope => 'publish_stream,user_photos',
redirect_uri => 'http://www.facebook.com/XXXXX?sk=app_YYYYYY&app_data=1'
);
Accept
If i click on it, the app shows blank page, and nothing happening... :(
On direct call, that app runs without any error.
Can someone help me out?
// sorry for my english...
Thank you!
and the whole index.php is:
this is the whole index.php:
<?php
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxxxx',
'secret' => 'yyyyy',
'baseUrl' => 'http://hosting.address/',
'appBaseUrl' => 'http://apps.facebook.com/app-name/',
'fileUpload' => 'true',
));
// Get User ID
$user = $facebook->getUser();
$params = array(
scope => 'publish_stream,user_photos',
redirect_uri => 'http://www.facebook.com/xxxxx?sk=app_yyyyy&app_data=1'
);
$signed_request = $facebook->getSignedRequest();
$app_data = $signed_request["app_data"];
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>the title</title>
<style>
a:link {color:#ffffff;}
a:visited {color:#ffffff;}
a:hover {color:#ececec;}
a:active {color:#1f1f1f;}
a:link {text-decoration: none}
</style>
</head>
<body>
<?php if ($user){
try {
$user_profile = $facebook->api('/me');
$likes = $facebook->api("/me/likes/123123123"); //page ID
if( !empty($likes['data']) ){
$scope = 'publish_stream,user_photos';
$scope_params = explode(',',$scope);
$permissions = $facebook->api("/me/permissions");
if( array_key_exists('publish_stream', $permissions['data'][0]) && array_key_exists('user_photos', $permissions['data'][0]) && isset($app_data)) {
$file = "1.jpg";
$message = 'bla bla';
$ret_obj = $facebook->api('/me/photos', 'POST', array(
'source' => '#' . $file,
'message' => $message,
)
);
echo "<img width=\"520px\" src=\"1.jpg\" />";
} else {
?>
accept</td>
deny</td>
<?
}
}else{
echo "<img width=\"520px\" src=\"no-fan.jpg\" />";
}
} catch (FacebookApiException $e) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
}
if ($user) {
} else {
$loginUrl = $facebook->getLoginUrl($params);
echo '<script type="text/javascript">top.location.href = "'.$loginUrl .'";</script>';
}
?>
</body>
</html>
I've had this before when I was delving into the world of Facebook apps.
What I eventually done was for my redirect_uri I used a page on my domain, e.g. https://domain.com/facebook/redirect.php and in redirect.php I would have:
// user didn't give app permissions
if (isset($_GET['error_reason']))
{
header( 'Location: http://www.facebook.com/FANPAGE' ) ;
}
else
{
header( 'Location: http://www.facebook.com/XXXXX?sk=app_YYYYYY' ) ;
}

Categories