I am trying to create a Facebook application that uploads images to a page. I can manage to upload photos to the user's photo album but not to the page's.
Here's my code:
require_once('php-sdk/facebook.php');
$config = array(
'appId' => 'XXXXXXXX',
'secret' => 'YYYYYYYYYYYYYYYY',
'fileUpload' => true,
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$photo = $_POST['image'];
$message = "Lorem ipsum";
echo $user_id;
if ($user_id) {
$ret_obj = $facebook->api('/XXXXXXXXXX/photos', 'POST', array(
'access_token' => $access_token,
'source' => '#' . $photo,
'message' => $message
)
);
} catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl( array(
'scope' => 'user_photos,publish_stream,photo_upload'
));
echo 'Please login.';
print_r($e);
error_log($e->getType());
error_log($e->getMessage());
}
echo '<br />logout';
} else {
$login_url = $facebook->getLoginUrl( array( 'scope' =>user_photos,publish_stream,photo_upload,manage_pages' ) );
echo 'Please login.';
}
Any help would be much appreciated.
When you do:
$facebook->api('/XXXXXXXXXX/photos', 'POST', array(
... is XXXXXXXXXX your appId or the albumId? You must do the call to your album ID.
Also make sure you are using the correct $access_token. To make sure look at:
https://graph.facebook.com/yourPageID?fields=access_token&access_token=YourPersonalAccessToken
where yourPersonalAccessToken is:
A Page admin access_token for this page; The current user must be an
administrator of this page; only returned if specifically requested
via the fields URL parameter.
Your must grant to graph api manage_pages permissions.
Related
I have a php form for facebook for a certain media house.To fill the form, the media house facebook profile should be liked first. When the profile is liked then only the form is accessible. I have so far succeeded in that. Now when the form is successfully submitted I want to share some info as XXX applied for "YYYY" campaign automatically in the fb profile of XXX. Is it possible? I have done facebook sharing with asking permission only. Is it possible to share some info after successfully form submission without asking for permission.
Any help / suggestions are welcome. Thanks in advance.
In success page I added the following code:
Before
<?php
require_once('php-sdk/facebook.php');
$config = array(
'appId' => 'XXX',
'secret' => 'YYY',
'allowSignedRequest' => false,
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 2;
$message = 'Message Here;
?>
Then in body part:
<?php
if($user_id){
try(){
$ret_obj = $facebook->api('/me/feed', 'POST', array(
'link' => 'https://www.facebook.com/myrepublica',
'message' => $message,
)
);
}catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream',
));
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
}else{
$login_url = $facebook->getLoginUrl( array( 'scope' => 'photo_upload') );
echo 'Please login.';
}
?>
Hope this helps someone.
I dont seem to be able to get this to work. The event loads fine but the image just doesnt upload, please let me know what I am doing wrong, I thiink there is a problem with the picture url format, does in need the # in front?, I have tried it without and it doesn't work:
////////////////////////////////////////////////////////////////////////////////////////
require 'facebook.php';
// appId and secret all work fine
$facebook = new Facebook(array(
'appId' => '************',
'secret' => '**************',
'fileUpload' => true
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
$response = $facebook->api(
"/me/events",
"POST",
array (
'name' => $title,
'start_time' => $start_time,
'location' => $location,
'end_time' => $end_time,
'description' => $description,
'picture' => '#http://andyotto.files.wordpress.com/2013/01/donald- duck.gif?w=863',
'venue' => $venue
)
);
if (count($response)>0){
$response_message = "Event successfully added uploaded to Facebook.";
}else{
$response_message = "There was a problem uploading your event to Facebook.";
}
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$statusUrl = $facebook->getLoginStatusUrl();
$loginUrl = $facebook->getLoginUrl(array(
'scope'=>'create_event'
));
header( 'Location:'.$loginUrl ) ;
}
It seems from the docs that you can't upload an image directly once you publish an Event (https://developers.facebook.com/docs/graph-api/reference/user/events/#publish), at least the picture filed is not described.
According to this doc (https://developers.facebook.com/docs/graph-api/reference/event#picture) you can set the Event's picture once it's created via
POST /{EVENT_ID}/picture
with the picture encoded as "multipart/form-data" in the request's body parameter "source".
I recently created a Facebook App to send visualized data (image) to pages which are already belongs to same user account as The App.
Also, I set extended permissions in Permissions Pane of App. Setting Page.
Here are the codes that can send post in text format. But when I tried to send images attached it fails.
What are the possible mistakes that I can't get at the moment?
$facebook = new Facebook(array(
'appId' => MY_APP_ID,
'secret' => MY_APP_SECRET,
'fileUpload' => true,
'allowSignedRequest' => false
));
$img = "../../../tw-data/postImages/twpostimage-usd.png";
$message = 'Activity Test with Image # ' . date('d.M.Y H:i:s');
try
{
$access_token = $facebook->getAccessToken();
$params = array('access_token' => $access_token,
'source' => '#'.$img,
'message' => $message
);
$result = $facebook->api('/'. MY_PAGE_ID .'/feed','POST', $params);
print_r($result);
}
catch (FacebookApiException $e)
{
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
What Errors are you getting when you try this?
I believe the main issue you're having is that you're posting to
/my_page_id/feed
where you should be posting to
/my_page_id/photos
From the facebook docs
$response = $facebook->api(
"/me/photos",
"POST",
array (
'url' => '{image-url}',
)
);
EDIT:
I have successfully posted an image with a message using the following format.
$img = 'FILE_NAME.jpg';
$message = 'Activity Test with Image # ' . date('d.M.Y H:i:s');
$params = array('access_token' => $access_token,
'message' => 'test',
'source' => '#'.$img
);
$result = $facebook->api('/USER_ID/photos','POST', $params);
print_r($result);
Also, I set extended permissions in Permissions Pane of App. Setting
Page.
That's a common mistake. Permissions have to be requested in your scope, not in the app settings.
$params = array(
'scope' => 'publish_stream',
'redirect_uri' => 'https://www.myapp.com/post_login_page'
);
$loginUrl = $facebook->getLoginUrl($params);
Accept the permissions dialog and try again. If you want to post to the page as page, obtain a page access token.
Happy Coding!
I am using this code to post to the user's wall :
require 'fb/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
));
$params = array(
'canvas' => 1,
'scope' => 'publish_stream,email,user_about_me,user_birthday,user_website',
'redirect_uri' => 'urlhere',
);
$fb_session = $facebook->getUser();
// Session based API call.
if ($fb_session)
{
try
{
$args = array(
'message' => $_GET['by'],
'link' => 'linkhere/',
'caption' => $_GET['test']
);
$post_id = $facebook->api("/me/feed", "post", $args);
}
catch (Exception $e)
{
$fb_login_url = $facebook->getLoginUrl($params);
echo $e->getMessage();
}
}
else
{
$fb_login_url = $facebook->getLoginUrl($params);
}
The code is working till the point uuer clicks on login part to post. After that the url contains a code = xxxzz and a state =yyy but the $fb_session is 0.
This works sometimes without any changes. Please help!
You should include the access token when you try to post to some User Facebook wall.
First get access token:
$fbToken = NULL;
$fbToken = $facebook->getAccessToken();
right after $fb_session = $facebook->getUser();
and then include it in the $args array:
'access_token' => $fbToken
if session is NOT created or access token is not valid, you should redirect the user back to re-authorize your Facebook App.
So in ELSE, you should add
header("Location: $fb_login_url ");
after $fb_login_url = $facebook->getLoginUrl($params);
assuming that everything else is correct, but the access_token is not valid, you should also include header("Location: $fb_login_url "); before echo $e->getMessage();
in both cases you are using getloginurl
shouldn't it be
if ($fb_session) {
try {
$args = array(
'message' => $_GET['by'],
'link' => 'linkhere/',
'caption' => $_GET['test']
);
$post_id = $facebook->api("/me/feed", "post", $args);
} catch (Exception $e) {
$fb_logout_url = $facebook->getLogoutUrl;
.
.
.
I am trying to post a link on the facebook wall. but i got these error. please help me anyone to solve this error.
$config = array(
'appId' => 'xxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
if($user_id) {
try {
$facebook->api('/$user_id/feed','POST',
array( 'access_token' => $facebook->getAccessToken(),
'message' => 'Hello World!',
'link' => 'www.example.com'
)
);
} catch(FacebookApiException $e) {
$result = $e->getResult();
error_log(json_encode($result));
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
echo '<br />logout';
} else {
as the redirect_uri, so we don't
// need to specify it here.
$login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
echo 'Please login.';
}
{"error":{"message":"(#1) An error occured while creating the share","type":"OAuthException","code":1}}
OAuthException
(#1) An error occured while creating the share
{"error":{"message":"(#1) An error occured while creating the share","type":"OAuthException","code":1}}
OAuthException
(#1) An error occured while creating the share
Could you try to add 'type' => 'link' and see if it works? As far as I've read on Facebook forums this is actually a bug.
you are not doing anything wrong, there is a small php mistake in your code due to which your $user_id variable is not being read.
solution,replace single quote with double quote around /$user_id/feed in your code. try this it will work:
$facebook->api("/$user_id/feed",'POST',
array( 'access_token' => $facebook->getAccessToken(),
'message' => 'Hello World!',
'link' => 'www.example.com'
)
);