Hi, I've just started to use the Facebook SDK php and login, retrieving a user's profile and posting links is working fine, however posting photos is not working no matter what I try.
Facebook (post) request error: (#324) Requires upload file is the error I get.
I've tried 'source' => new CURLFile('path/to/file.name', 'image/png') with various different locations for the image (even in the root), 'source' => '@/path/to/file.name' even though I'm using the correct php library, and even from a site link but still no luck?
I'd like user to be able to use $_FILES['img']['tmp_name'] so users can upload multiple images/photos themselves from a form but I can't seem to get a fixed image/photo to work?
Any help/guidance would be great.
<?php
session_start();
require_once 'facebook/autoload.php';
use Facebook\FacebookRequest;
use Facebook\GraphObject;
use Facebook\FacebookRequestException;
use Facebook\FacebookSession;
use Facebook\GraphUser;
use Facebook\FacebookRedirectLoginHelper;
$api_key = 'XXXXXXXXXXX';
$api_secret = 'XXXXXXXXXXX';
$redirect_login_url = 'XXXXXXXXXXXX';
$message = $_POST['mess'];
FacebookSession::setDefaultApplication($api_key, $api_secret);
$helper = new FacebookRedirectLoginHelper( $redirect_login_url);
// First check if this is an existing PHP session
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// create new session from the existing PHP sesson
$session = new FacebookSession( $_SESSION['fb_token'] );
try {
// validate the access_token to make sure it's still valid
if ( !$session->validate() ) $session = null;
} catch ( Exception $e ) {
// catch any exceptions and set the sesson null
$session = null;
echo 'No session: '.$e->getMessage();
}
} elseif ( empty( $session ) ) {
// the session is empty, we create a new one
try {
// the visitor is redirected from the login, let's pickup the session
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $e ) {
// Facebook has returned an error
echo 'Facebook (session) request error: '.$e->getMessage();
} catch( Exception $e ) {
// Any other error
echo 'Other (session) request error: '.$e->getMessage();
}
}
if ( isset( $session ) ) {
// store the session token into a PHP session
$_SESSION['fb_token'] = $session->getToken();
// and create a new Facebook session using the cururent token
// or from the new token we got after login
$session = new FacebookSession( $session->getToken() );
try {
// with this session I will post a message to my own timeline
$request = new FacebookRequest(
$session,
'POST',
'/me/photos',
array(
'source' => '?????',
'message' => $message
)
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
// the POST response object
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
$msgid = $graphObject->getProperty('id');
} catch ( FacebookRequestException $e ) {
// show any error for this facebook request
echo 'Facebook (post) request error: '.$e->getMessage();
}
} else {
// we need to create a new session, provide a login link
echo 'No session, please <a href="'. $helper->getLoginUrl( array( 'publish_actions' )
).'">login</a>.';
}
Finally found this here, https://developers.facebook.com/docs/php/FacebookFile/5.0.0
'source' => $fb->fileToUpload('/path/to/file');
Closing this and recommending using 5.0 as noted above. Thanks.
+1 Thanks @here :)
It also works for me ! :+1:
Most helpful comment
Finally found this here, https://developers.facebook.com/docs/php/FacebookFile/5.0.0
'source' => $fb->fileToUpload('/path/to/file');