Hi Guys i'm e newbie in php programin and facebook app, i found this code and it works very well for TEXT posts, but my requirements are that I have to UPLOAD a picture too, i tried by addin ['source' => '$image_source'] on the arrey but does not work...
How can I upload a PHOTO ?
<?php
include_once("config.php");
if($_POST)
{
//Post variables we received from user
$userPageId = $_POST["userpages"];
$userMessage = $_POST["message"];
$image_source = "http://www.example.com/image.jpg"
if(strlen($userMessage)<1)
{
//message is empty
$userMessage = 'No message was entered!';
}
//HTTP POST request to PAGE_ID/feed with the publish_stream
$post_url = '/'.$userPageId.'/feed';
//posts message on page statues
$msg_body = array(
'message' => $userMessage,
'source' => '$image_source'
);
if ($fbuser) {
try {
$postResult = $facebook->api($post_url, 'post', $msg_body );
} catch (FacebookApiException $e) {
echo $e->getMessage();
}
}else{
$loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>$homeurl,'scope'=>$fbPermissions));
header('Location: ' . $loginUrl);
}
//Show sucess message
if($postResult)
{
echo '<html><head><title>Message Posted</title><link href="style.css" rel="stylesheet" type="text/css" /></head><body>';
echo '<div id="fbpageform" class="pageform" align="center">';
echo '<h1>Your message is posted on your facebook wall.</h1>';
echo '<a class="button" href="'.$homeurl.'">Back to Main Page</a> <a target="_blank" class="button" href="http://www.facebook.com/'.$userPageId.'">Visit Your Page</a>';
echo '</div>';
echo '</body></html>';
}
}
?>
You cannot use the feed endpoint to post photos. You should be using /me/photos with the publish_stream permission. To post to an album you need the /ALBUM_ID/photos endpoint
$msg_body = array(
'message' => $userMessage,
'source' => '#'.'$image_source'
);
$postResult = $facebook->api('/me/photos/','post', $msg_body);
For using urls not associated with your site you need to use url
$msg_body = array(
'message' => $userMessage,
'url' => 'http://somepage.com/img.png'
);
Related
My app's access_token will expire in 60 days according to fb documentation.
When I try to post the status to the user profile it works correctly without an accesstoken
my code
if ($user_id) {
try {
$post_url = '/' . $user_id . '/feed';
$msg_body = array(
'link'=> $link,
'picture'=> $picture,
'name'=> $linkname,
'caption'=> $caption,
'description'=> $description,
'message' => $userMessage
);
$postResult = $facebook->api($post_url, 'post', $msg_body );
} catch (FacebookApiException $e) {
echo $e->getMessage();
}
}
but when I upload a photo to a user profile it doesn't work without the access token
if ($user_id) {
try {
$post_url = '/' . $user_id . '/photos';
$access = $row['access'] ;
$msg_body = array(
'access_token'=>$access,
'source'=>'#'.$userPhoto,
'message' => $userMessage
);
$postResult = $facebook->api($post_url, 'post', $msg_body );
} catch (FacebookApiException $e) {
echo $e->getMessage();
}
}
my app permissions:
$fbPermissions =
'publish_stream,photo_upload,user_photos,read_stream,email,user_birthday';
The question is: if an access token expires, does it affect the ability of my app to post status and upload photos to a user's profile?
Will it affect both of them or upload a photo only?
I'm trying to publish checkin using Facebook Graph API. I've gone through Facebook API documentation (checkins) and also have the publish_checkins permission. However, my checkin is not getting published. May I know is there anything wrong or am I missing anything else? Thank you for your time :)
fbmain.php
$user = $facebook->getUser();
$access_token = $facebook->getAccessToken();
// Session based API call
if ($user) {
try {
$me = $facebook->api('/me');
if($me)
{
$_SESSION['fbID'] = $me['id'];
$uid = $me['id'];
}
} catch (FacebookApiException $e) {
error_log($e);
}
}
else {
echo "<script type='text/javascript'>top.location.href='$loginUrl';</script>";
exit;
}
$loginUrl = $facebook->getLoginUrl(
array(
'redirect_uri' => $redirect_url,
'scope' => status_update, publish_stream, publish_checkins,
user_checkins, user_location, user_status'
)
);
main.php - Using PHP SDK (Wrong SDK used in this example, should use JavaScript SDK instead)
<?php
include_once "fbmain.php";
if (!isset($_POST['latitude']) && !isset($_POST['longitude']))
{
?>
<html>
<head>
//ajax POST of latitude and longitude
</head>
<body>
<input type="button" value="Check In!" onclick="checkin();"/></span>
</body>
</html>
<?php
}
else
{
?>
<script type="text/javascript">
function checkin()
{
try
{
$tryCatch = $facebook->api('/'.$uid.'/checkins', 'POST', array(
'access_token' => $facebook->getAccessToken(),
'place' => '165122993538708',
'message' =>'MESSAGE_HERE',
'coordinates' => json_encode(array(
'latitude' => '1.3019399200902',
'longitude' => '103.84067653695'
))
));
}
catch(FacebookApiException $e)
{
$tryCatch=$e->getMessage();
}
return $tryCatch;
}
</script>
<?php
}
?>
Question solved - Things to take note when publishing checkin
Make sure publish_checkins permission is granted.
Must use json_encode() to encode coordinates parameter for PHP SDK.
place and coordinates parameters are compulsory.
A re-authentication is required if you have just added publish_checkins permission to your existing list of allowed permissions.
Apparently, the configuration and the PHP checkin function that I have posted in the question are correct. However, I should use JavaScript SDK instead of PHP SDK for my case as pointed out by Nehal. For future references...
Using JavaScript SDK
function checkin()
{
FB.api('/me/checkins', 'post',
{ message: 'MESSAGE_HERE',
place: 165122993538708,
coordinates: {
'latitude': 1.3019399200902,
'longitude': 103.84067653695
}
},
function (response) {
alert("Checked in!");
}
);
}
You also need to learn PHP and variable scoping.
$facebook = new Facebook(array(
'appId' => FB_API_KEY,
'secret' => FB_SECRET_KEY,
'cookie' => true,
));
$loginUrl = $facebook->getLoginUrl(
array('scope' => 'status_update,publish_stream,publish_checkins,user_checkins,user_location,user_status,user_checkins')
);
// Session based API call
if ($user) {
try {
$me = $facebook->api('/me');
if($me)
{
$_SESSION['fbID'] = $me['id'];
}
} catch (FacebookApiException $e) {
error_log($e);
}
}
else {
echo "<script type='text/javascript'>top.location.href='$loginUrl';</script>";
exit;
}
function checkin($fb)
{
try
{
$tryCatch = $fb->api('/'.$_SESSION['fbID'].'/checkins', 'POST', array(
'access_token' => $fb->getAccessToken(), //corrected
'place' => '165122993538708',
'message' =>'I went to placename today',
'coordinates' => json_encode(array(
'latitude' => '1.3019399200902',
'longitude' => '103.84067653695'
))
));
}
catch(FacebookApiException $e)
{
$tryCatch=$e->getMessage();
}
return $tryCatch;
}
checkin($facebook); //calling the function and passing facebook object to function
Using the Graph API Explorer, I tried posting the following: /[another_user_id]/links and a link field
but I am getting this error:
(#240) Requires a valid user is specified (either via the session or via the API parameter for specifying the user.
What am I missing here? Using /[another_user_id]/feed works but I want it to be a shared link.
The Following Code Posts In Users Friends wall.
try {
$newStatus = $facebook->api("/$FRIENDSUSERID/feed", 'POST',
array(
'link' => "https://apps.facebook.com/XXXXXXXXX",
'picture' => "https://XXXXXXXXX", //colour
'name' => "XXXXXXXXX",
'description' => "XXXXXXXXX",
'access_token' => $access_token
));
//echo '<pre>Post ID: ' . $newStatus['id'] . '</pre>';
} catch(FacebookApiException $e) {
error_log($e->getType());
error_log($e->getMessage());
//echo '<textarea style="width: 300px; height: 200px;">' . $e->getMessage() . '</textarea>';
}
I want to change my code so, that first it will check if the user has liked the page. After this (if yes) a question will come up, to accept the terms and if he clicks on the accept button the Facebook permissions request dialog will come up. How can i do this?
The problem is that everytime a user clicks on the app link the $user variable is always false, but he is logged in...
<?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>
</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)) {
} else {
echo "accept</td>";
echo "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>
Try following this excellent example:
http://www.jelecos.com/default/blog/11-04-18/Facebook_PHP_SDK_Fangate_Creation.aspx
Then on the fanpage.php have your code to ask them to log in.
just as the picture I attached, it shows 1 app requests, but there is no apps listed there.
the apprequests sent from the graph api using javascript sdk
Any App Reqeusts you set with you app will need to be deleted by your app. Sample from blog shows how to concatenate the request_id and user_id in order to delete the outstanding requests.
Refer to http://developers.facebook.com/blog/post/569/
<?php
require_once('php-sdk/facebook.php');
$config = array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
);
$facebook = new Facebook($config);
?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:fb="https://www.facebook.com/2008/fbml">
<head>
<title>Deleting Requests Example</title>
</head>
<body>
<?php
$user_id = $facebook->getUser();
if ($user_id) {
//Get the Request Ids
$request_ids = explode(',', $_REQUEST['request_ids']);
//Construct full Request_Id
function build_full_request_id($request_id, $user_id) {
return $request_id . '_' . $user_id;
}
//For each Request construct full Request_id and Delete
foreach ($request_ids as $request_id) {
echo ("reqeust_id=".$request_id."<br>");
$full_request_id =
build_full_request_id($request_id,$user_id);
echo ("full_reqeust_id=".$full_request_id."<br>");
try {
$delete_success =
$facebook->api("/$full_request_id",'DELETE');
if ($delete_success) {
echo "Successfully deleted " . $full_request_id;
}
else {
echo "Delete failed".$full_request_id;
}
}
catch (FacebookApiException $e) {
echo "error";
}
}
}
//User TOS if user has not authenticated your App
else if (!isset($_REQUEST['error'])){
$params = array(
'redirect_uri' => 'http://localhost/~namitag/requests.php'
);
$loginUrl = $facebook->getLoginUrl($params);
echo
'<script>window.top.location.href="'.$loginUrl.'";</script>';
}
else {
echo ("user denied permission"); }
?>
</body>
</html>