facebook graph api - post large image - php

I finally got facebooks graph api to post messages on my fan PAGE as page
How do i get it to post large images as a post, not as a link?
'source' => $photo seems to create a thumbnail
this is what i have so far
<?php
$page_id = 'YOUR-PAGE-ID';
$message = "I'm a Page!";
$photo = "http://www.urlToMyImage.com/pic.jpg";
require '../src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'YOUR-APP-ID',
'secret' => 'YOUR-SECRET-ID',
));
$user = $facebook->getUser();
if ($user) {
try {
$page_info = $facebook->api("/$page_id/?fields=access_token");
if( !empty($page_info['access_token']) ) {
$facebook->setFileUploadSupport(true); // very important
$args = array(
'access_token' => $page_info['access_token'],
'message' => $message,
'source' => $photo
);
$post_id = $facebook->api("/$page_id/feed","post",$args);
}
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl(array( 'next' => 'http://mydomain.com/logout_page.php' ));
} else {
$loginUrl = $facebook->getLoginUrl(array('scope'=>'manage_pages,publish_stream'));
}
?>

The problem here is that you are in actual fact not posting a photo. What you are doing is posting a link to that photo so what you see is indeed a thumbnail preview image that Facebook retrieved from that URL.
What you'll want to do is provide a full path to a file on your server prefixed with the # symbol. The topic has been discussed on the site quite a bit so I'll just point you in the direction of a canonical post dealing with uploading of images to Facebook with the PHP SDK
Upload Photo To Album with Facebook's Graph API
The code looks like this -
$facebook->setFileUploadSupport(true);
$params = array('message' => 'Photo Message');
$params['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/me/photos', 'post', $params);

Related

Facebook: PHP SDK issues

I have some facebook php code that worked flawlessly before the 4/30/15 upgrade. I upgraded my code, it seems to work on certain computers but not on others. On computers that it doesn't work on I've tried multiple browsers with the same result. I am able to log into Facebook using the SDK, but it won't post anything to my page's wall. Same code, different computer, and everything works fine. Here's the code:
<?php
$facebook = new Facebook(array(
'appId' => '##########',
'secret' => '##########',
'fileUpload' => true
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
}
catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl(array(
'next' => ($user['baseurl'] . 'logout.php')
));
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'publish_pages , manage_pages'
));
}
$access_token = $facebook->getAccessToken();
$params = array(
'access_token' => $access_token
);
#The id of the fanpage
$fanpage = '############';
#The id of the album
$album_id = '############';
if ($user) {
$accounts = $facebook->api('/me/accounts', 'GET', $params);
foreach ($accounts['data'] as $account) {
if ($account['id'] == $fanpage || $account['name'] == $fanpage) {
$fanpage_token = $account['access_token'];
}
}
$message = 'Post this to the wall.';
$img = 'path to image.jpg';
$args = array(
'message' => $message,
'image' => '#' . $img,
'aid' => $album_id,
'no_story' => 0,
'access_token' => $fanpage_token
);
$photo = $facebook->api($album_id . '/photos', 'post', $args);
}
?>
Most permissions need to get approved by Facebook before they can be used for any user, else they only work for users with a role in the App. Check out the docs about Login Review.
You can only use publish_pages and manage_pages as App Admin/Developer/Tester without review.
If you only test with those users and it still does not work, debug the Access Token in the Debugger and see if those permissions are really authorized: https://developers.facebook.com/tools/debug/
Edit: After reading your comment, i believe you are trying to use a User Token. Use /me/accounts to get a Page Token and try again with that one. If you debug the Token, make sure the Page shows up too.

Posting a Photo to my Facebook Wall (working) But posts to the User Feed

Ok, this all works fine... EXCEPT, the photo posts to the User Feed, instead of posting as the page admin... (it posts from my personal Facebook account, instead of as the page)
Searched all over for an answer, but coming up empty...
(config.php)
<?PHP
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret,
'fileUpload' => true,
'cookie' => true
));
$fbuser = $facebook->getUser();
?>
<?php
include_once("config.php");
$facebook->setFileUploadSupport(true);
$userPageId = 619143434781602;
if ($fbuser) {
try {
$img = $imgSrc;
$post_url = '/'.$userPageId.'/photos';
$photo = $facebook->api($post_url, 'POST',
array(
'source' => '#' . 'test.jpg',
'message' => 'Photo uploaded via the PHP SDK!'
)
);
} catch (FacebookApiException $e) {
echo $e->getMessage();
}
}
?>
there is a way you can do it by using js if you take a look at How to post to Facebook fanpage as fanpage, not user
this may also give you an insight http://www.sergiy.ca/post-on-facebook-app-wall-and-fan-page-wall-as-admin/
happy reading and good luck

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 app that posts to all its users in a specific time (php)

It's my first question on stackoverflow. I made this application and it works fine for posting a photo to a user's wall once he visit the app page, But what I really want is to post many photos in a scheduled way (at a specific time like 12 Feb 2013 10:00 pm) to his wall and all users wall
I made a MySQL database that contains a table for users' ID, and a table for posts' info and time.
What can I do next?
include ("../facebook/facebook.php") ;
$facebook = new Facebook(array(
'appId' => APP_ID ,
'secret' => APP_SECRET ,
'fileUpload' => true,
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$facebook->setFileUploadSupport(true);
$photo ="images/image1.jpg" ;
$message= "My message ";
$ret_obj = $facebook->api('/me/photos', 'post', array(
'source' => '#' . $photo,
'message' => $message,
)
);
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'read_stream, publish_stream, photo_upload')
);
}
#kareem : In simple words you can have a loop statement for the same.
for($i=0;$i<$upperLimit;$i++)
{
$photo ="images/image".$i."jpg" ;
$message= "My message ";
$ret_obj = $facebook->api('/me/photos', 'post', array(
'source' => '#' . $photo,
'message' => $message,
)
);
}
$upperLimit is the limit , till which you are planning to post the image on users wall.
Note: you can have a set of Images in a location as image1...imagen,so that you can loop around the images while posting on user wall
if required you can add a timer inside the for loop for some delay

How to post an image to a facebook page from a PHP app?

I'm trying to post an image to a facebook page from a PHP app. I check many resources, the documentation, some demo codes and several questions in this site, but cannot finish a working app.
Here is my code:
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret
));
$fbuser = $facebook->getUser();
$facebook->setFileUploadSupport(true);
$post_url = '/443513955707619/photos';
$msg_body = array(
'source' => '#/www/www.gbart.hu/public_html/facebook/megosztos_app/img/winner/winner_'.(int)$round[0].'.jpg',
//'image' => 'http://www.gbart.hu/facebook/megosztos_app/img/winner/winner_'.(int)$round[0].'.jpg',
'message' => 'http://www.facebook.com/WangMesterKinaiKonyhaja/app_322145727882829',
'access_token' => $access_token
);
try {
$postResult = $facebook->api($post_url, 'post', $msg_body);
}
catch (FacebookApiException $e) {
echo $e->getMessage();
}
Here are my permissions for the app:
$fbPermissions = 'email, publish_actions, publish_stream, photo_upload, manage_pages';
I have the appID, app_secret, access_token parameters as required (the other parts of the app is working).
I got some different error messages, like invalid album id or invalid access tokens. I solved these, and now there isn't any error messages, but the photo does not appear anywhere.
In a previous version of this code I tried to post the image to the page walls not to the album, but that made a weird result: posted the image to my user profile's wall.
Try to post something like below
$url = "https://graph.facebook.com/443513955707619/photos?access_token=".$token."&message=hello&url=http://imagetopost.com/hi.jpg&method=post";
$response = file_get_contents($url);

Categories