Facebook upload photo from computer - php

i am trying to make a facebook app that uploads photos from the computer to a specific album on facebook. i have the code below but i get an error: failed creating formpost data.
require_once 'include.php';
$config = array(
'appId' => $app_id,
'secret' => $app_secret,
);
$facebook = new Facebook($config);
$user = $facebook->getUser();
if($user && isset($_POST['submit'])){
try {
$facebook->setFileUploadSupport(true);
//Create an album
$album_details = array(
'message'=> 'Album desc',
'name'=> 'Album name'
);
$create_album = $facebook->api('/me/albums', 'POST', $album_details);
//Get album ID of the album you've just created
$album_uid = $create_album['id'];
//Upload a photo to album of ID...
$photo = realpath($_FILES['miss_photo']['tmp_name']);
$photo2 = $photo . '.jpg';
//echo $photo2; exit();
$photo_details['image'] = '#' . $photo2;
$upload_photo = $facebook->api('/'.$album_uid.'/photos', 'POST', $photo_details);
} catch (FacebookApiException $e) {
echo ($e->getMessage());
}
}
else
echo 'error';
any idea why i get the error message?

You cant use files in that way, you need to provide path to your file, like:
photo_details = array(
'message'=> 'some test'
);
$photo_details['image'] = '#' . realpath('/path/to/your/image_file.jpg');
$upload_photo = $facebook->api('/'.$album_uid.'/photos', 'post', $photo_details);
Hope that helps

Related

How to get Facebook user ID, post images on user's wall after changes?

Years ago I've developed apps for Facebook, but now seems not working old method to get user's information (id, name, etc) and post data on user's wall.
I've used like this, but for now It returns me blank screen:
require_once('FacebookAPI/Facebook.php');
$facebook = new Facebook(array(
'appId' => 'xxxxx',
'secret' => 'xxxxx',
));
$user = $facebook->getUser();
if ($user)
{
try
{
#Code below to post data on user's wall
# Photo Caption
$photoCaption = 'Just used app';
$imageUrl = ('http://example.com'); // Example URL
$link = ('http://www.facebook.com/page/app_xxxx?ref=ts');
# Post Data for Photos API
$post_data = array(
'message' => $photoCaption,
'link' => $link,
'caption' => 'Caption text'
);
$apiResponse = $facebook->api('/me/feed', 'POST', $post_data);
?>
#Code below to show app
<html>
<body bgcolor="#ffffff">
<center>
// link to my app
</center>
</body>
</html>
// Code below to get user's information (name, id)
<?php
$user_profile = $facebook->api('/me');
$coded = $_REQUEST['code'];
$name = $user_profile['name'];
$id = $user_profile['id'];
session_start();
$_SESSION['name'] = $name;
$_SESSION['id'] = $id;
}
catch (FacebookApiException $e)
{
$user = null;
error_log($e);
}
}
else
{ # Code below to get access from user
$redirectUri = 'http://www.facebook.com/mypage/app_xxxx?ref=ts';
$loginUrl = $facebook->getLoginUrl( array(
'scope' => 'publish_stream,photo_upload',
'redirect_uri' => $redirectUri
));
echo("<script>top.location.href = '" . $loginUrl . "';</script>");
}
Could someone help me out to get successfully user's name, id?
You can try this code. Hope you have downloaded latest php-sdk, you have appid,app des
include_once "php-sdk/src/facebook.php";
$app_id = 'your app id';
$facebook = new Facebook(array(
'appId' => 'your app id',
'secret' => 'your app secret'
));
$user = $facebook->getUser();
if ($user) {
$me = $facebook->api('/me');
$name= $me['name'];
$fid=$me['id'];
}
else
{
$loginUrl = "http://www.facebook.com/dialog/oauth?client_id=" .
$app_id . "&redirect_uri=" . urlencode($app_url) . "&scope=email";
echo("<script> top.location.href='" . $loginUrl . "'</script>");
}

Facebook post image on fan page with app

I need to post on facebook fan page with app.
My post type is text+image.
When i post only text, i have no problem, but when i try to add a jpg it doesn't works.
Here's my code:
<pre>
<?php
require_once 'facebook.php';
// configuration
$appid = 'XXX';
$appsecret = 'YYY';
$pageId = 'ZZZ';
$msg = 'MSG MSG MSG';
$title = 'TITOLO TITOLO TITOLO';
$uri = 'http://www.google.it/';
$desc = 'Description';
$pic = 'http://sharefavoritebibleverses.com/images/bible_verses.png';
$action_name = 'AZIONE NOME';
$action_link = 'http://www.yahoo.it';
$facebook = new Facebook(array(
'appId' => $appid,
'secret' => $appsecret,
'cookie' => false,
));
$user = $facebook->getUser();
// Contact Facebook and get token
if ($user)
{
// you're logged in, and we'll get user acces token for posting on the wall
try
{
$facebook->setFileUploadSupport(true);
$page_info = $facebook->api("/$pageId?fields=access_token");
if (!empty($page_info['access_token'])) {
/*$attachment = array(
'access_token' => $page_info['access_token'],
'message' => $msg,
'name' => $title,
'picture'=>$pic
);*/
$photo_details = array(
'message' => 'message',
'access_token' => 'XYZ'
);
$photo_details['picture'] = '#'.realpath('aaa.jpg');
echo realpath('aaa.jpg');
$status = $facebook->api("/$pageId/feed", "post", $photo_details);
}
else
{
$status = 'No access token recieved';
}
}
catch (FacebookApiException $e)
{
error_log($e);
$user = null;
}
}
else
{
// you're not logged in, the application will try to log in to get a access token
header("Location:{$facebook->getLoginUrl(array('scope' => 'photo_upload,user_status,publish_stream,user_photos,manage_pages'))}");
}
echo $status;
echo "<br>CONT=".count($status);
?>
Also i need a format like second one, and not like first:
What's wrong in my code?
Thanks!
Try this:
//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'=> 'Album desc',
'name'=> 'Album name'
);
$create_album = $facebook->api('/me/albums', 'post', $album_details);
//Get album ID of the album you've just created
$album_uid = $create_album['id'];
//Upload a photo to album of ID...
$photo_details = array(
'message'=> 'Photo message'
);
$file='app.jpg'; //Example image file
$photo_details['image'] = '#' . realpath($file);
$upload_photo = $facebook->api('/'.$album_uid.'/photos', 'post', $photo_details);
Please try to make sure that $pic is the relative image location on your server and not a generic web url of the image.

Facebook Php image in text message

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.

Post a Link from PHP

I have this script:
This code should post a Text and a Link to a WebSite
<?
// 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('facebook.php');
$c = array(
'appId' => '4102137023*****',
'secret' => '*****c4a60cb08*****7c0333*****',
);
$facebook = new Facebook($c);
$uid = $facebook->getUser();
echo "Userid: " . $uid;
echo "<BR>";
?>
<html>
<head></head>
<body>
<?
if($uid){
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$ret_obj = $facebook->api('/me/feed', 'POST',
array(
'link' => 'angel-craft.de',
'message' => 'Wenn ihr das hier seht freut euch auf ein Game'
));
echo '<pre>Post 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' => 'publish_stream'
));
echo 'Please login.';
echo $e->getType();
echo $e->getMessage();
}
// Give the user a logout link
echo '<br />logout';
} else {
// No user, so print a link for the user to login
// To post to a user's wall, we need publish_stream 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' => 'publish_stream' ) );
echo 'Please login.';
}
?>
</body>
</html>
This should post a link with a text
but the Userid($uid) stays empty.
And YES this is the Demo script from FB dev.
I've used this successfully.
<?php
require 'src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => '-----',
'secret' => '-----',
'cookie' => true,
));
$session = $facebook->getSession();
$me = null;
// Session based API call.
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
$Name = $me['first_name'];
$properties = array(
array(
'text'=>'Property Link',
'href'=>'http://www.yahoo.com'
),
array(
'text'=>'Property Link',
'href'=>'http://www.yahoo.com'
)
);
// Link that is adjacent to "Like" and "Comment" at the very bottom of the post.
$action_links = array(
'name'=>'Test',
'link'=>'http://www.yahoo.com'
);
// Dictates who can see the post.
$privacy = array(
'value'=>'ALL_FRIENDS'
);
// api('/me/feed', 'post',... = Wall Post.
$wallPost = $facebook->api('me/feed', 'post', array(
'message'=> 'Testing',
'link'=> 'http://www.yahoo.com',
'properties'=>$properties,
'actions'=>$action_links
)
);
} catch (FacebookApiException $e) {
error_log($e);
}
}
$par = array();
$par['req_perms'] = "email, publish_stream";
if ($me) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl($par);
}
?>
I found the bug, The cert was old. Thanks to all :)

Why I cannot get the facebook album ID ?

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);

Categories