Post doesn't show on page feed - php

I try to post facebook status to a page using facebook api. i get array return (id) of those post. but its doesn't show up on page timeline. i try to access facebook feed graph of those page. and i get the result.
fb page : https://www.facebook.com/testscrap?ref=hl (no timeline showing). but i actually have a post accessed from facebook graph ( https://www.facebook.com/348759318542072/posts/350492115035459 )
Where am i going wrong?
here is the code to post :
$feed_dir = "/{$page['page_id']}/feed/";
$msg_body = array (
'access_token' => $page['access_token'],
'message' => 'Jingle bell jingle bell jingle all the way',
'picture' => 'http://assets.kompas.com/data/photo/2012/08/26/0958534620X310.jpg',
);
try {
$result = $this->data['fbinstance']->api($feed_dir, 'post', $msg_body);
jlog($result);
} catch (Exception $e) {
$err_str = $e->getMessage();
jlog($err_str);
}

Copy from the comment to the authors post:
As I said in the other comment, I have experienced caching problems from Facebook on pages and groups, no matter how much I emptied my cache and changes browsers. But it should show up sooner or later. Try with a different user if you have access to it.
Facebook might sometimes be slow to update the cache, and therefore be slow to display new posts. So a successful request should work, just test with other users.

Related

Facebook app posting to page successfully, but posts are not visible to non-developers. Do I really have to review for an app with 1 user?

I have setup a facebook app to post images / videos to one particular fan page. It requests 'publish_actions' and 'manage_pages permissions. It is in developer mode and I have no intention to expand the functionality beyond posting to this single facebook page.
The code works correctly, it posts to the correct page and everything looks good whilst logged into the developer account / page admin (same account). But other facebook users cannot see the posts that have been made via the app. I was under the impression that apps didn't need to go through the review process for internal apps such as these?
Heres a quick stripped down version of the code for posting to the page:
FacebookSession::setDefaultApplication($settings['app_id'], $settings['secret']);
//This is user access_token not the pages.
$session = new FacebookSession( $settings['access_token'] );
$pageTokenResponse = (new FacebookRequest(
$session
, 'GET', '/' . $settings['page_id']
, array( 'fields' => 'access_token' )
))->execute()->getGraphObject(GraphPage::className());
//Retrieved Page Token
$pageToken = $pageTokenResponse->getProperty("access_token");
uploadUrlToFacebook($imgurl, $item->get_title(), $session, $pageToken, $settings['page_id']);
function uploadUrlToFacebook($url, $title, $session, $pageToken, $pageId){
$request = new FacebookRequest(
$session,
'POST',
"/$pageId/photos",
array (
'access_token' => $pageToken
,'url' => $url
,'message' => $title
)
);
$response = $request->execute();
}
I'm sure there is a stupid error either somewhere in the code or my apps setup but why can I see the posts but nobody else can ?
Turns out I had already selected some items for review before I properly understood the process so I ended up in a state of not being able to make the app live as I had pending review items. I deleted the permissions from the review queue, made the app live and all is good.
I think while authenticating Fb App you given "only me" visibility for Publish Actions.
go to https://www.facebook.com/settings?tab=applications
Select your App .
Then Check this . "App visibility and post audience"

Create a Facebook App that can post to a page I am an admin of

As part of the website I am working on, I need to be able to post directly to the wall of my clients Facebook Page.
I have created an App and am successfully posting to my own dummy profile by simply using:
$request = new FacebookRequest(
$session, 'POST', '/me/feed', array(
'link' => 'my_url',
'message' => 'my_message'
);
Now I need to modify this to post to my clients page, im assuming I cant just change 'me' to by their page ID because that would be a massive security flaw so I need to get permission somehow to post to their page. I am an admin for the page, how can I do this? Or do I actually need to be logged in with the user account that created the page?
I have looked all around in the developer section at creating an app but there doesn't seem to be any way of creating an app for a different account.
Any advice would be greatly appreciated on this.
Many thanks
In order to post to a Facebook page, you need to go through a two steps process -
login as the user using the extended user token,
get the user's token for the page
post to the page using the page's token.
The following code should be able to post a message to your page. I HAVEN'T DEBUGGED IT since it is simplified from my own web site :-) Don't forget to include the Facebook SDK files:
function extract_access_token_of_facebook_page_by_id($array_of_all_user_pages,$page_id){
$num_of_pages=count($array_of_all_user_pages['data']);
$the_page_token="";
for ($i=0; $i<$num_of_pages; $i++){
if ($array_of_all_user_pages['data'][$i]['id']==$page_id){
$the_page_token=$array_of_all_user_pages['data'][$i]['access_token'];
}
}
return $the_page_token;
}
$facebook = new Facebook(array(
'appId' => $YOUR-APP-ID,
'secret' => $YOUR-APP-SECRET,
));
try
{
$facebook->setAccessToken($THE-USER-TOKEN);
$page_id = YOUR-PAGE-ID;
$fanpage_access_token=extract_access_token_of_facebook_page_by_id($facebook->api("/me/accounts"),$page_id);
$args = array();
$args['access_token'] = $fanpage_access_token;
$args['message'] = "THE TEXT YOU WANT TO POST";
$api_url="/".$page_id."/feed";
$api_response = $facebook->api($api_url, 'post', $args);
}
catch (FacebookApiException $e)
{
echo 'Error facebookservice'.$e;
}
Please look into Application developer section. It will help you. Create on developer account and you will be able to access page.
you code is for old sdk version i think, it not work for me : Fatal error: Class 'Facebook' not found

Facebook Php Api basics

Im getting started with the php sdk, and struggling to understand a few things (I have a basic example below - but everytime the pic goes to MY wall and not the fan page)
Code:
require_once("facebook.php"); // set the right path
$config = array();
$config['appId'] = 'app id';
$config['secret'] = 'app secret';
$config['fileUpload'] = true; // optional
$fb = new Facebook($config);
$params = array(
// this is the access token for Fan Page
"access_token" => "I create this via the graph api (select App and click get token), I select publish stream and photo_upload permissions.",
"message" => "test message",
"source" => "#" ."photo.png", // "#" .
);
try {
// **** is Facebook id of Fan page
$ret = $fb->api('/****/photos', 'POST', $params);
echo 'Photo successfully uploaded to Facebook Album';
} catch(Exception $e) {
echo $e->getMessage();
}
the picture keeps going to MY wall (logged in user) and not the fan page, is that because im getting a token for the currently logged in user (me) instead of the fan page? and if so how do i generate a token for the fan page whilst logged in as the developer? (is it by putting the fan page id into the Get bar?)
Apologies for the noob questions but the facebook documentation sucks (until u understand the core functionality of course). Most tutorials work once u know how to use the graph API - none ive looked actually EXPLAIN how to use the graph to generate correct tokens, etc.
\POST /<ID>/photos — this will post on the <ID>'s wall only, so please double-check the <ID> that you are using.
The access token will depict that on behalf of whom the photo would be posted on the <ID>'s wall. If you use the user access token, the photo will be published on behalf of the user and if page access token is used, it will be published on the page's behalf itself.
You can get the page access token of your pages by-
\GET /me/accounts?fields=access_token
(permission required: manage_pages)
Demo

PHP Facebook API - post message to the wall with a link to an user?

I know I cannot post through Facebook API messages to the user's wall with the links which go out of Facebook.
But can I post a message, where would be a link to a Facebook user? So far I didn't find something like that, so trying my luck here.
Using the /me/feed endpoint you can post to the user's wall, nothing prevents it.
You will need publish_stream permission.
if ($user) {
$attachment = array(
'message' => 'this is my message',
'link' => 'http://www.facebook.com/profile.php?id=xxxx',
);
try {
// Proceed knowing you have a user who is logged in and authenticated
$result = $facebook->api('/me/feed/','post',$attachment);
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
}
I know I cannot post through Facebook API messages to the user's wall
with the links which go out of Facebook.
You surely can, using the feed api
If the user have given you the permission to publish , you can post any link on his wall.

Managing Facebook Pages using PHP

I'm looking to integrate my current website more deeply with Facebook using a Fan Page (notice: I did noticed the Open Graph protocol that Facebook support but i'm looking to create a website-wide fan page).
I know that you can create and manage Fan Pages using Facebook, but I'm looking for a way to do that using a PHP script - for example, post to the Fan Page wall, create events using the Fan Page and ideally - create secondary Fan Pages on the fly.
After looking around in Facebook's developer section I didn't find a method to do those tasks from outside using the Facebook API.
So my question is: how would you accomplish that?
Thanks!
Publishing posts to your Page, and creating Events, are both relatively trivial tasks. You can use the Graph API to do that.
Check out the area about Publishing specifically. It gives you the general overview of how publishing works, and this can be applied all over the Graph.
Also, the documentation about the Events portion of the Graph API has an example cURL post for how to create a new event via the Graph API.
Posting anything to your Facebook page is going to require you have the manage_pages extended permission, and it's probably a good idea to get the offline_access permission also.
An example of both posting to your Page wall and creating an event (in php) would look a lot like this:
<?php
require 'facebook.php';
$fb = new Facebook(array(
'appId' => FB_APP_ID,
'secret' => FB_APP_SECRET,
'cookie' => true
));
$your_page_id = '123123123';
//get the access token to post to your page via the graph api
$accounts = $fb->api("/me/accounts");
foreach ($accounts['data'] as $account)
{
if ($account['id'] == $your_page_id)
{
//found the access token, now we can break out of the loop
$page_access_token = $account['access_token'];
break;
}
}
try
{
//publish a story to the page's wall (as the page)
$post_id = $fb->api("/{$your_page_id}/feed", "POST", array(
'message' => "Hello to all my fans, I love you!"
'access_token' => $page_access_token;
));
echo "Post published. ID: {$post_id}<br>";
//create a new event.
$event_id = $fb->api("/{$your_page_id}/events", "POST", array(
"name" => "My Totally Awesome Event, You Better Show UP!",
"start_time" => time(), //it starts now...duh!
"location" => "Anywhere, USA"
));
echo echo "Event created. ID: {$event_id}<br>";
}
catch (Exception $e)
{
var_dump($e);
}
As for creating Pages on the fly, the only way you can do that is by using the Open Graph Protocol. The only restriction here is that pages have to have unique URLS. So you can assign each of your Open Graph objects a unique ID, and give them a URL like http://www.mysite.com/pages?id=123456. This will let you output the Open Graph tags required to generate the page on FB. You can then use the Graph API to get the ID of the Open Graph object after someone likes it like so: http://graph.facebook.com/?ids=http://www.mysite.com/pages?id=123456.
You can publish to these Open Graph objects the same exact way you'd publish to a standard Facebook Page.
Hope that helps!

Categories