PHP Script problems in posting on the the wall of my facebook fan page and permanent value for $access_token [duplicate] - facebook

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
PHP SCRIPT issue with passing the uploaded image to facebook->api function
I have this code and its working awsomely fine just and just only one problem i am not able to pass my uploaded image to facebook->api (whether its valid or not) and the following always echo
echo 'Only jpg, png and gif image types are supported!';
i even remove the check on image type and try to get the image from
$img = realpath($_FILES["pic"]["tmp_name"]);
but $img gets nothing in it and uploads a by default empty image on facebook as my page
Kindly check my following code and let me know what should is wrong with my code so i'll become able to upload images
<?
require 'src/facebook.php';
$app_id = "364900470214655";
$app_secret = "xxxxxxxx";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true,
'fileUpload' => true,
));
$user = $facebook->getUser();
//echo $user;
if(($facebook->getUser())==0)
{
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos,offline_access,manage_pages'))}");
exit;
}
else {
$accounts_list = $facebook->api('/me/accounts');
echo "i am connected";
}
$valid_files = array('image/jpeg', 'image/png', 'image/gif');
//to get the page access token to post as a page
foreach($accounts_list['data'] as $account){
if($account['id'] == 194458563914948){ // my page id =123456789
$access_token = $account['access_token'];
echo "<p>Page Access Token: $access_token</p>";
}
}
//posting to the page wall
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"]);
$attachment = array('message' => 'this is my message',
'access_token' => $access_token,
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'example.org',
'description' => 'this is a description',
'picture' => '#' . $img,
'actions' => array(array('name' => 'Get Search',
'link' => 'http://www.google.com'))
);
$status = $facebook->api('/194458563914948/feed', 'POST', $attachment); // my page id =123456789
var_dump($status);
}
}
?>
<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>

Creating a New Album and Adding a Photo
This is the scenario where you upload a photo to the ALBUM_ID/photos Graph API endpoint. The user interface for this example allows the user to select a photo and add a caption before submitting the new photo.
Using PHP:
<?php
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$post_login_url = "YOUR_POST-LOGIN_URL";
$album_name = 'YOUR_ALBUM_NAME';
$album_description = 'YOUR_ALBUM_DESCRIPTION';
$code = $_REQUEST["code"];
//Obtain the access_token with publish_stream permission
if(empty($code))
{
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode($post_login_url)
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url .
"'</script>");
}
else {
$token_url= "https://graph.facebook.com/oauth/"
. "access_token?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
// Create a new album
$graph_url = "https://graph.facebook.com/me/albums?"
. "access_token=". $access_token;
$postdata = http_build_query(
array(
'name' => $album_name,
'message' => $album_description
)
);
$opts = array('http' =>
array(
'method'=> 'POST',
'header'=>
'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = json_decode(file_get_contents($graph_url, false,
$context));
// Get the new album ID
$album_id = $result->id;
//Show photo upload form and post to the Graph URL
$graph_url = "https://graph.facebook.com/". $album_id
. "/photos?access_token=" . $access_token;
echo '<html><body>';
echo '<form enctype="multipart/form-data" action="'
.$graph_url. ' "method="POST">';
echo 'Adding photo to album: ' . $album_name .'<br/><br/>';
echo 'Please choose a photo: ';
echo '<input name="source" type="file"><br/><br/>';
echo 'Say something about this photo: ';
echo '<input name="message" type="text"
value=""><br/><br/>';
echo '<input type="submit" value="Upload" /><br/>';
echo '</form>';
echo '</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.