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".
Related
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 want to use PHP to send message to my Facebook friends. I found some code but it just lets me log in. Could anyone help me?
<?php
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXX',
'secret' => 'XXXXX',
'cookie' => true,
));
$session=$facebook->getUser();;
if ($session) {
$logoutUrl = $facebook->getLogoutUrl();
$access_token = $facebook->getAccessToken();
try {
$result = $facebook->api(
'me/feed',
'post',
array('access_token' => $access_token , 'message' => 'test')
);
} catch (FacebookApiException $e) {
error_log($e);
}
}
1) Did you provided the correct permissions to the app?
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
2) Check this answer: Update facebook status using php and this: Facebook PHP SDK , Post to another users wall whats wrong?
3) it's /me/feed, not me/feed:
$result = $facebook->api(
'/me/feed',
'post',
array('access_token' => $access_token , 'message' => 'test')
);
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 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.
Further to Post to users wall upon facebook app submission (my old question), I have came up with the following code however it doesn't seem to be working?? I thought best to open a new question as it is a new question.
What am I doing wrong? Also, where should this code go?
<?php
$session = $facebook->getSession();
//Is user logged in and has allowed this app to access its data
if (!$session) {
$loginUrl = $facebook->getLoginUrl(array(
'canvas' => 1,
'fbconnect' => 0,
'next' => 'enter.php',
'cancel_url' => 'index.php',
));
// use the $loginUrl created on the enter button to request permission;
}
$user_id = $facebook->getUser();
//post to wall
$attachment = array('message' => '<message>',
'name' => '<name here>',
'caption' => '<caption here>',
'link' => '<link to app>',
'description' => '<enter description >',
'picture' => '<enter image url>',
'actions' => array(array('name' => '<enter action label>',
'link' => '<enter action url>')
);
$permissions = $facebook->api("/me/permissions");
if( array_key_exists('publish_stream', $permissions['data'][0]) ) {
// Permission is granted!
// Do the related task
try {
$post_id = $facebook->api('/me/feed', 'post', $attachment);
} catch (CurlException $e) {
//timeout so try to resend
$post_id = $facebook->api('/me/feed', 'post', $attachment);
}
catch (FacebookApiException $e) {
error_log($e);
}
} else {
// We don't have the permission
// Alert the user or ask for the permission!
}
// store the post id in-case you need to delete later
?>
I'll just post the code I'm using that works. Hope it helps
fbClass.php
public function __construct() {
// Naredimo instanco
$facebook = new Facebook(array(
'appId' => $this->fbid,
'secret' => $this->fbsecret,
'cookie' => true,
));
$this->facebook = $facebook;
}
function authUser($facebook) {
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if (!($user)) {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_about_me, user_birthday, email, publish_stream',
'redirect_uri' => 'http://apps.facebook.com/myappname/',
));
echo("<script> top.location.href='" . $loginUrl . "'</script>");
} else {
return true;
}
}
process.php
$facebook = $fbClass->facebook;
$fbAuth = $fbClass->authUser($facebook);
if ($fbAuth) {
$res = $facebook->api('/me/feed/', 'post', array(
'message' => MESSAGE,
'name' => NAME,
'caption' => '',
'description' => DESC,
'picture' => PIC,
'link' => 'http://www.facebook.com/myapp/',
'actions' => array('name' => 'Test', 'link' => 'http://apps.facebook.com/myapp/')
));
}
You need a Facebook access token for this code to work. Add your token where My Access token here is in the following code:
$attachment = array(
'access_token' => 'My Access token here',
'message' => '',
'name' => 'My Wall Post Header/Title Here',
'caption' => 'Small caption here',
'link' => 'http://www.mywebsite.org',
'description' => 'Wall Post Details Here',
'picture' => "http://www.mywebsite.org/images/logo.gif",
);
You can get access tokens here.