Facebook Graph API - post to users wall - facebook

I have a form which is redirecting to a php script to add fields to a mysql database. Once it has been added I want the app to ask permission to publish to the users stream and once authorised I want a message to be published to the users wall.
here is what ive got so far. the mysql bit is working but the post to wall is not.
<?php
require_once 'config.php';
mysql_connect("$host","$username","$password") or die(mysql_error());
mysql_select_db("$database") or die(mysql_error());
$mysql="INSERT INTO `dhxmascomp`.`users` (`user_id` , `name` , `user_email`) VALUES (NULL, '{$_GET[name]}','{$_GET[name2]}')";
if(!mysql_query($mysql))
die(mysql_error());
mysql_close();
require_once 'facebook-php-sdk/src/facebook.php';
$facebook = new Facebook(array(
'appId' => $FB_AppID,
'secret' => $FB_AppSecret,
'cookie' => true,));
$user = $facebook->getUser($_GET['access_token']);
try {
$publishStream = $facebook->api("/$user/feed", 'post', array(
'message' => 'test',
'link' => $FB_canvas_page,
'picture' => $submitPrefix. 'ibizasocial.com/plastik/images/logo_fb.png',
'name' => 'Plastik',
'description'=> 'test'));
} catch (FacebookApiException $e) {
//echo($e);
}
?>
<html>
<body style="810px; overflow:hidden">
<div style="overflow:hidden; width:810px;">
<img src="http://ibizasocial.com/dhapp/images/DHAPP3.jpg" border="0" />
</div>
</body>
</html>

Related

Photo_upload permission depracated in facebook, how to fix this PHP script to working?

I have a php script below, to upload photo to facebook cover
<?php
if(is_single()){
if($_REQUEST['state']){
$imgsource = show_src('851','315');
}else{
$imgsource = $_POST['o'];
}
if($imgsource){
require_once "script/facebook.php";
$config = array(
'appId' => get_theme_option('app_id'),
'secret' => get_theme_option('secret'),
'fileUpload' => true,
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$photo = str_replace(home_url( '/' ),'',$imgsource);
$message = get_theme_option('descover');
if($user_id) {
try {
$ret_obj = $facebook->api('/me/photos', 'POST', array(
'source' => '#' . $photo,
'message' => $message,
)
);
$hasil3 = '<div id="modal-from-dom" class="modal hide fade">
<div class="modal-header">
×
<h2>You\'re almost done!</h2>
</div>
<div class="modal-body">
<img src="'.get_bloginfo("template_directory").'/images/eee.jpg" />
</div>
<div class="modal-footer">
Go to Facebook Profile
</div>
</div>
<button data-controls-modal="modal-from-dom" data-backdrop="true" data-keyboard="true" class="btn large success btnsingle">Preview Timeline</button>';
} catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl( array(
'scope' => 'photo_upload'
));
error_log($e->getType());
error_log($e->getMessage());
}
} else {
$login_url = $facebook->getLoginUrl( array( 'scope' => 'photo_upload') );
if($imgsource){header('Location: '.$login_url);}
}
}
}
?>
its not working now, because photo_upload permission in API depracated, how to fix this PHP script to working?
i already tried change photo_upload with these, but looks like still not right:
publish_stream
publish_actions
user_photos

atal error: Uncaught OAuthException: Error validating application. Application has been deleted.

I am gone mad since i cannot solve this.
Please help me. when i goto the php (Which is clixbox.net/fbupload.php)i created, after getting the user permissions, it gives me this error.
Fatal error: Uncaught OAuthException: Error validating application. Application has been deleted. thrown in /home/clixcube/public_html/clixbox.net/library/base_facebook.php on line 1271
my fbupload.php file is this
<?php
require_once 'library/facebook.php';
$facebook = new Facebook(array(
'this is where i put my app id' => $app_id,
'my app secret' => $app_secret,
'fileUpload' => true
));
//It can be found at https://developers.facebook.com/tools/access_token/
$access_token =
'this is where i put my token';
$params = array('access_token' => $access_token);
//The id of the fanpage
$fanpage = '232241216922182';
//The id of the album
$album_id ='281594988653471';
//Replace Wa33D94 with your Facebook ID
$accounts = $facebook->api('/Wa33D94/accounts', 'GET', $params);
foreach($accounts['data'] as $account) {
if( $account['id'] == $fanpage || $account['name'] == $fanpage ){
$fanpage_token = $account['access_token'];
}
}
$valid_files = array('image/jpeg', 'image/png', 'image/gif');
if(isset($_FILES) && !empty($_FILES)){
if( !in_array($_FILES['pic']['type'], $valid_files ) ){
echo 'Only jpg, png and gif image types are supported!';
}else{
#Upload photo here
$img = realpath($_FILES["pic"]["tmp_name"]);
$args = array(
'message' => 'This photo was uploaded via Clixbox.net',
'image' => '#' . $img,
'aid' => $album_id,
'no_story' => 1,
'access_token' => $fanpage_token
);
$photo = $facebook->api($album_id . '/photos', 'post', $args);
if( is_array( $photo ) && !empty( $photo['id'] ) ){
echo '<p><a target="_blank" href="http://www.facebook.com/photo.php?fbid='.$photo['id'].'">Click here to see this photo on
Facebook.</a></p>';
}
}
}
?>
<html>
<head>
<title>Upload a picture to facebook</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<!-- Form for uploading the photo -->
<div class="main">
<p>Select a photo to upload on Facebook Fan Page</p>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<p>Select the image: <input type="file" name="pic" /></p>
<p><input class="post_but" type="submit" value="Upload to my album" /></p>
</form>
</div>
</body>
</html>
And the base_facebook.php line (1271) is this
$e = new FacebookApiException($result);
Any idea on how to solve this?
Consider checking in your application list to see if the application is still there
developers.facebook.com/apps
If it is, consider submitting a bug developers.facebook.com/bugs

After Facebook installa facebook app redirects me to the url but outside of Facebook

I have the following problem:
I'm trying to do my first application for Facebook and after Facebook connection and install the application not automatically redirects me to the url but outside of Facebook. This only happens the first time, once it has been installed if the url is recharged and stays within Facebook.
Any ideas to fix it?
I do not know if the problem is the code or application configuration:(
// PHP SDK v.3.0.0 de Facebook
require 'includes/facebook.php';
$facebook = new Facebook(array(
'appId' => '*******',
'secret' => '*************',
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'read_stream,publish_stream,user_about_me,user_likes,email,user_status'
));
}
if (!$user) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk 3.0.0</title>
<style typ="text/css">
html, body { width: 520px;}
</style>
</head>
<body>
<h1>php-sdk</h1>
<h3>PHP Session</h3>
<?php foreach($_SESSION as $key=>$value){
echo '<strong>' . $key . '</strong> => ' . $value . '<br />';
}
?>
<h3>Tu</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Tus datos (/me)</h3>
<?php foreach($user_profile as $key=>$value){
echo '<strong>' . $key . '</strong> => ' . $value . '<br />';
}
?>
</body>
</html>
You didn't specify a redirect_uri in your getLoginUrl method and hence the SDK constructed one for you that is based on your current URL.
So all you need to do is just set the canvas URL as your redirect_uri:
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'read_stream,publish_stream,user_about_me,user_likes,email,user_status',
'redirect_uri' => 'CANVAS_URL'
));
P.S: $user_profile and $me holds the same value, just use one of them instead of making two Graph API calls.

Facebook App Not Working in Safari

I have recently developed a facebook app using php. Though the app works in all browsers, it does not in Safari, it returns me to the first app step, after i try posting on a users wall. Following is my code.
My app displays a users friend list, and allows one to select friends, and post on their wall.
<?php
require ("facebook-api/src/facebook.php");
$facebook = new Facebook(array(
'appId' => '####',
'secret' => '####',
'cookie' => true,
));
// Login or logout url will be needed depending on current user state.
$params = array(
'scope' => 'email,user_birthday,friends_photos,publish_stream',
'redirect_uri' => 'https://apps.facebook.com/appname/'
);
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl($params);
}
$user = $facebook->getUser();
if ($user) {
$friends = $facebook->api('me/friends');
$uids = "";
foreach($friends as $f) {
$uids .= "uid=$f OR ";
}
$user_id_sess = $_SESSION['userid'];
$query_uids = substr($uids,0,strlen($query_uids)-4);
date_default_timezone_set('UTC');
$today = date('m\/d');
$data = $facebook->api(array('method'=>'fql.query','req_perms' => 'friends_birthday','query'=>"select uid,sex,name,birthday from user where uid in (select uid2 from friend where uid1=me()) ORDER by name ASC"));
echo "
<div class='span12 center-align' style='display:none;' id='block3'>
<form id='frmFb' name='frmFb' action='".$_SERVER['PHP_SELF']."' method='post' >
<p><img src='../static/images/logo.png' alt='logo' /></p>
<br />
<h1 style='margin-bottom:10px;'>Share with your friends</h1>
<p id='progress' style='display:none;'><img src='static/img/processing.gif' alt='Processing' /></p>
<div class='center-align well pull-center' style='max-height:300px; overflow:auto;'>
";
foreach($data as $today_bday) {
//$table='user_recipient';
$remfbid = $today_bday['uid'];
$fb_bday = $today_bday['birthday'];
echo "
<div style='width:50px; height:110px; margin:15px; float:left;' class='center-align'>
<a href='#' class='thumbnail'><img src='https://graph.facebook.com/".$today_bday['uid']."/picture' class='friend' alt='".$today_bday['name']."'></a>
<input type='checkbox' name='fbchoose[]' value='".$today_bday['uid']."'>
<p><small>".$today_bday['name']."</small></p>
<br/><br/>
</div>
";
}
echo "
</div>
<br />
<a href='#' class='btn btn-inverse btn-large'><i class='icon-ban-circle icon-white' style='margin-top:4px;'></i> CANCEL</a><input type='submit' value='SHARE NOW' name='btnAddReminder' class='btn btn-success btn-large' onclick='show()'>
</form>
</div>
";
} else {
echo "<p><script>top.location.href='$loginUrl';</script></p>
";
}
?>
<?
if(isset($_REQUEST['btnAddReminder'])){
if($_POST['fbchoose'] == "") {
$error_msg = "<div class='alert pull-center alert-error'><i class='icon-warning-sign' style='margin-top:2px;'></i> Please select at least one friend to share</div><br/>";
}
else {
foreach($_POST['fbchoose'] as $row) {
$vars = array(
'message' => 'Message...',
'name' => 'Name..',
'caption' => 'Caption',
'link' => 'Link',
'description' => 'Description',
'picture' => 'Picture...'
);
$facebook->api("/".$row ."/feed", 'post', $vars);
}
echo "<script>location.href='http://www.mywebsite.com/';</script>";
}
}
?>
in safari preferences, under "Privacy", set "Block cookies" to Never

Facebook - How to make the authorization dialog open in a popup?

I have the following code:
$facebook = new Facebook(array(
'appId' => 'id',
'secret' => 'secret',
));
$user = $facebook->getUser();
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array(
'req_perms' => 'email,user_birthday,user_about_me'
));
}
and HTML:
<a class="button-fb" href="<?php echo $loginUrl; ?>" title="Connect with Facebook">Connect with Facebook</a>
The problem is that the <a> will open in the current page, I want to open in a popup (like most site do). How to do? =)
You can use this snippet to create a popup.. It uses Javascript.
<a class="button-fb" href="javascript: void(0)"
onclick="window.open('<?php echo $loginUrl; ?>',
'windowname1',
'width=200, height=77');
return false;" title="Connect with Facebook">Connect with Facebook</a>
I hope this helps.