- Make sure the SDK is properly loaded.
- Your app requires
photo_upload
& publish_stream
permissions.
$file
should be a relative path to your image. i.e ex: /path/to/image/file
- The POST param you're passing should be
image
and not source
So now the final version should look something like this (After you fix the above things):
<?php
require_once('images/Facebook.php');
$facebook = new Facebook(array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
));
# Get User ID
$user = $facebook->getUser();
if ($user) {
try {
# Photo Caption
$photoCaption = 'My Photo Caption!';
# Relative Path to your image.
$file = 'plaukai/images/PlaukaiNeuzvedus.jpg';
# Post Data for Photos API
$post_data = array(
'message' => $photoCaption,
'source' => '@' . realpath( $file )
);
$apiResponse = $facebook->api('/me/photos', 'POST', $post_data);
} catch (FacebookApiException $e) {
$user = null;
error_log($e);
}
} else {
$loginUrl = $facebook->getLoginUrl( array(
'scope' => 'publish_stream,photo_upload'
));
echo("<script>top.location.href = '" . $loginUrl . "';</script>");
}
?>
Note: I've removed setAccessToken/getAccessToken as those are not needed for this unless those were being used with the other codes, but otherwise its not needed as the SDK automatically passes access_token
of the current user when your app makes any API calls.
Hope that helps, Let me know if you have any questions.
PS I've not tested the above code, but it has to work unless there is any other problem.
EDIT 1:
Try one of these solutions as well:
Solution 1:
# Photo Caption
$photoCaption = 'My Photo Caption!';
# Relative Path to your image.
$file = 'plaukai/images/PlaukaiNeuzvedus.jpg';
# Post Data for Photos API
$post_data = array(
'message' => $photoCaption,
'image' => '@' . realpath( $file )
);
Solution 2:
# Photo Caption
$photoCaption = 'My Photo Caption!';
# Absolute Path to your image.
$imageUrl = 'http://placehold.it/500x500/'; // Example URL
# Post Data for Photos API
$post_data = array(
'message' => $photoCaption,
'url' => $imageUrl
);
EDIT 2:
Here's the code you requested:
$redirectUri = 'http://domain.com/myapp/';
$loginUrl = $facebook->getLoginUrl( array(
'scope' => 'publish_stream,photo_upload',
'redirect_uri' => $redirectUri
));
echo("<script>top.location.href = '" . $loginUrl . "';</script>");