I have a PHP script that upload an image+description+other details on my facebook fanpage wall.
But what i need is to upload INSIDE an Album.
I'm looking all over the internet for this answer, this tutorial is the closest i got until now.
Here's my code:
$params = array(
"access_token" => $page_token,
"message" => "#php #facebook",
"link" => "www.domain.com.br",
"description" => "Buy this now!",
"source" => "#" . $path,
);
try {
$ret = $fb->api('/'.$album_id . '/photos', 'POST', $params);
echo 'Successfully posted to Facebook Fan Page';
} catch(Exception $e) {
echo $e->getMessage();
}
I have already tried to change this $ret = $fb->api('/'.$fanpage_id . '/photos', 'POST', $params); path to this one: $ret = $fb->api('/'.$fanpage_id.'/'.$album_id . '/photos', 'POST', $params); and something similar.
But it returns (#240) The target_id references an inactive user because fb API thinks that $album_id is the $fanpage_id.
if somebody knows how to post a picture inside the album, please help me ;)
Here it is, this code checks if the album with the name you want exists, if not creates a new one:
//Check if the album exists just change PAGE_ID to the ID of your page and NAME_OF_YOUR_ALBUM to the name of the album where you want to upload the photo
$fql = "SELECT object_id FROM album WHERE owner = PAGE_ID AND name='NAME_OF_YOUR_ALBUM'";
$album_exists = $this->facebook->api(array(
'method' => 'fql.query',
'query' =>$fql,
'access_token' => $accessToken
));
if(array_key_exists(0, $album_exists)){
//the album with that name exists, let's save his ID
$album_uid = $album_exists[0]['object_id'];
}else{
//We don't have an album with that name, let's create an album
$album_details = array('name'=> 'NAME_OF_YOUR_ALBUM');
$create_album = $this->facebook->api('/'.$page_id.'/albums', 'post', $album_details);
//Get album ID of the album you've just created
$album_uid = $create_album['id'];
}
//details of the photo you want to upload
$photo_details = array('message'=> 'This is a fantastic photo');
$file='PATH_TO_YOUR_FILE/NAME_FILE.jpg'; //Example image file
$photo_details['image'] = '#' . realpath($file); //get the real path
//Upload the photo to the album you've just created or the one you wanted
$upload_photo = $this->facebook->api('/'.$album_uid.'/photos', 'post', $photo_details);
Related
On my page I want post feeds with an image in the text of message and not like a link/url, is possible add to text message something like htmltags or bbcode?
$msg = "<img src=\"urlimg or facebook\" >\nMy text here";
$args = array(
'message' => $mgs,
);
$myfeed = $facebook->api($pageid . '/feed', 'post', $args);
update:
i've found a solution but if i post 2 times in a row they will be groupped in the same box in timeline
$args = array(
'message' => $msg,
'image' => '#'.$path,
'aid' => $album_id,
'access_token' => $token
);
$photo = $facebook->api($album_id . '/photos', 'post', $args);
exist a setting to stop auto-group that? or there is another way to post it like feed with image?
You can't publish an image in the middle of the text message, Facebook do not allow it.
But you can attach an image to the message, it will appear on the left of the message in this way:
$msg = "My text here";
imgUrl = "http://urltotheimage.com/path/image.jpg";
$args = array(
'message' => $mgs,
'picture' => $imgUrl
);
$myfeed = $facebook->api($pageid . '/feed', 'post', $args);
So I spent 30 seconds searching around the PHP Facebook API, which really is what you should be doing, and found the following example:
<?
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require_once('php-sdk/facebook.php');
$config = array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
'fileUpload' => true,
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$photo = './mypic.png'; // Path to the photo on the local filesystem
$message = 'Photo upload via the PHP SDK!';
?>
<html>
<head></head>
<body>
<?
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
// Upload to a user's profile. The photo will be in the
// first album in the profile. You can also upload to
// a specific album by using /ALBUM_ID as the path
$ret_obj = $facebook->api('/me/photos', 'POST', array(
'source' => '#' . $photo,
'message' => $message,
)
);
echo '<pre>Photo ID: ' . $ret_obj['id'] . '</pre>';
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl( array(
'scope' => 'photo_upload'
));
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
echo '<br />logout';
} else {
// No user, print a link for the user to login
// To upload a photo to a user's wall, we need photo_upload permission
// We'll use the current URL as the redirect_uri, so we don't
// need to specify it here.
$login_url = $facebook->getLoginUrl( array( 'scope' => 'photo_upload') );
echo 'Please login.';
}
?>
</body>
</html>
Take note of the $config variable values and the $facebook->api() call.
How do I give a Facebook App permission to post images to a Facebook Page album?
This is the code I use:
$args = array(
'image' => '#' . $img,
'aid' => $album_id,
'no_story' => 1,
'access_token' => $app_access_token
);
$photo = $facebook->api($album_id . '/photos', 'post', $args);
This is the error I get:
Uncaught OAuthException: A user access token is required to request this resource.
Here is the code I use to publish a photo to an album on Facebook. I hope it'll help.
<?php
include '../src/facebook.php';
$config['appId']='app_id';
$config['secret']='API_KEY';
$config['fileUpload'] = true;
$param['redirect_uri']='your_redirect_url';
$param['scope']=',publish_stream,user_photos';
$facebook=new Facebook($config);
$userid=$facebook->getUser();
if($userid){
try{
$args = array(
'image' => '#' . $img,
'message'=>$message,
);
$photo = $facebook->api('/'.$album_id . '/photos', 'post', $args);
}
catch(FacebookApiException $e){
echo $e->getMessage();//Get OAuth Error
}
else{
$loginUrl=$facebook->getLoginUrl($param);
echo "Please <a href='$loginUrl'>Click Here</a> To Login";
}
?>
Learn more Here
Just in case any user stumbles upon this post , to resolve this error
1> $facebook->setaccesstoken() to facebook page's access token . The facebook page access token is available by issuing $facebook->api('/user_id/accounts'), here user id is the id of page's admin .
2> After setting access token you can use the facebook->api to post the photo to album using album id
$facebook->api('album_id/photos', 'post', array( 'source' => '#'.$photo ));
What I want to do is create album on facebook and upload photo to the album, now it work to create an album on facebook,but it's not work on uploading photo to album, I dont know why I cannot get the album ID, any solution or idea?
here is my code which is create album and upload photo on facebook
<?//At the time of writing it is necessary to enable upload support in the Facebook SDK, you do this with the line:
$facebook->setFileUploadSupport(true);
//Create an album
$album_details = array(
'message'=> 'name',
'name'=> 'name'
);
$create_album = $facebook->api('/me/albums', 'post', $album_details);
//Get album ID of the album you've just created
$albums = $facebook->api('/me/albums');
foreach ($albums["data"] as $album) {
//Test if the current album name is the one that has just been created
if($album["name"] == 'name'){
$album_uid = $album["id"];
}
}
//Upload a photo to album of ID...
$photo_details = array(
'message'=> 'test'
);
$file='http://scm-l3.technorati.com/11/12/30/59287/google-logo-682-571408a.jpg'; //Example image file
$photo_details['image'] = '#' . realpath($file);
$upload_photo = $facebook->api('/'.$album_uid.'/photos', 'post', $photo_details);
//print_r($upload_photo);
?>
thanks
I dont think you could upload file from url, try:
$pathToFile = "/some/path/someimage.jpg";
$facebook->setFileUploadSupport(true);
............
$create_album = $facebook->api('/me/albums', 'post', $album_details);
//get the album id
$album_uid = $create_album["id"]; // no need for foreach loop
.....
$args = array('message' => 'Some message');
$args['image'] = '#' . realpath($pathToFile); //see realpath use here
$data = $facebook->api('/'. $album_uid . '/photos', 'post', $args);
Hope it helps
you can not call realpath against http file. it's for local files only.
download the file first using curl, wget, or one of php internal functions like file_get_contents/file_put_contents.
for example:
$file='http://scm-l3.technorati.com/11/12/30/59287/google-logo-682-571408a.jpg'; //Example image file
$output = tempnam(sys_get_temp_dir(), 'blex_');
$cmd = "wget -q \"$file\" -O $output";
exec($cmd);
$photo_details['image'] = '#' . realpath($output);
Using the facebook php-sdk, users can upload photos to facebook from our site. I can create the album and upload the photos. But can't get the URL to the newly created Album :(
Here's what I do ...
// create album
$albumDetails = array(
'name' => 'Fun images'
);
$album = $facebook->api('/me/albums', 'post', $albumDetails);
$albumID = $album['id'];
// upload photos
foreach ($images as $image) {
$file = FACEBOOK_IMAGES_DIR . $image->image_id . '.jpg';
$photoDetails = array(
'message'=> 'by choreboy'
);
$photoDetails['image'] = '#' . realpath($file);
$photoData = $facebook->api('/'.$albumID.'/photos', 'post', $photoDetails);
}
// Graph API says I can get link to the album:
// http://developers.facebook.com/docs/reference/api/album
// I thought I could get to the link data this way. But returns an empty array
$data=$facebook->api("/{$albumID}/photos?access_token={$session['access_token']}");
var_dump($data);
Maybe you are missing the first line in this code.
This URL redirects to your album:
$album_url = 'http://www.facebook.com/' . $albumID;
I've set up a Facebook application, I've requested the extended permissions and now I'm trying to upload a photo but it doesn't work!
I've tried everything, from
$facebook->api_client->photos_upload('photo/789165784.jpg');
To
$facebook->api_client->photos_upload('photo/789165784.jpg', NULL, 'My photo', 100000287894654);
I'm beginning to suspect that I need to set up some extensions for php. I'm using WAMP and since the server is currently offline, I can't test it on production until tomorrow (I think..).
Thanks!
Here are some various ways to upload photos using the Graph API. The examples assume you've instantiated the $facebook object and have a valid session for the current user.
1 - Default Application Album of Current User
This example will upload the photo to your default application album of the current user. If the album does not yet exist it will be created.
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/me/photos', 'post', $args);
print_r($data);
2 - Target Album
This example will upload the photo to a specific album.
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/'. $ALBUM_ID . '/photos', 'post', $args);
print_r($data);
3 - Target Album with Access Token
This example will upload a photo to a specific album which requires an access token.
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/'. $ALBUM_ID . '/photos?access_token='. $ACCESS_TOKEN, 'post', $args);
print_r($data);
I don't know too much about this, I'm afraid, but the following link suggests that other people have had the same problem. Maybe this forum will be helpful:
http://forum.developers.facebook.com/viewtopic.php?pid=93450
It also links to this page, which seems like it might help:
http://wiki.auzigog.com/Facebook_Photo_Uploads
Ben