Facebook API how to get all wall items - php

I am having trouble getting all the facebook posts, message, photos etc... that show up on my wall via the Facebook API. Basically, many items are missing in the result set. I tried the following with FQL:
$facebook = new Facebook(array(
'appId' => FB_APPID,
'secret' => FB_SECRET,
'cookie' => true
));
$fql = "SELECT post_id, actor_id, target_id, message FROM stream WHERE source_id =me() ORDER BY updated_time DESC";
//Create Query
$params = array(
'method' => 'fql.query',
'query' => $fql,
);
//Run Query
$result = $facebook->api($params);
But this feed seems to be missing some items, and i don't know why.
I also tried with reading the graph feed at http://graph.facebook.com/me/feed?access_token=<Access+token> and again, i also don't get all the feed items.
What is the problem? Why am I missing some items?

use facebook->getLoginUrl with Permissions, see https://developers.facebook.com/docs/reference/login/#permissions
$applicationurl = 'http://{yourwebsite}/facebook.php';
// Get User ID
$user = $facebook->getUser();
if(empty($user))
{
$params = array(
'scope' => 'email',
'redirect_uri' => $applicationurl
);
$loginUrl = $facebook->getLoginUrl($params);
header('Location: ' . $loginUrl ."\r\n");
exit;
}
Use this code before your query.
You can use $user in your query like uid = '.$user.
Set your permissions in the scope part of your request, email in the example above.
EDIT OP says use the scope 'read_stream', and it works

Related

How to post to a facebook group wall in offline mode using graph API? (I am a member of group)

I am trying to post a status update on the wall of a group which I am a member of. Here is the code I am using
<?php
require 'facebook-php-sdk/src/facebook.php';
$appId = 'xxxxxxxxxxxxxxxx';
$appSecret = 'xxxxxxxxxxxxxxxx';
$extended_access_token = 'xxxxxxxxxxxxxxxxxxxxx';
$facebook = new Facebook(array('appId' => $appId, 'secret' => $appSecret));
$msg_body = array(
'message' => 'Good evening',
'type' => 'status',
'access_token' => $extended_access_token,
);
$groups = array(
'Group name' => '1234567',
);
foreach($groups as $group_name => $group_id){
try {
$post_url = "/$id/feed";
$postResult = $facebook->api($post_url, 'post', $msg_body );
print_r($postResult);
} catch (FacebookApiException $e) {
echo $e;
}
}
?>
If I login to fb via browser and hit this page in a new tab, the message is getting posted to the group wall. But If I am not logged into facebook, then if I hit this page, no message is getting posted and I am getting an error message
OAuthException: (#200) The user hasn't authorized the application to perform this action
How can I post to this group via offline mode? Searched a lot for this, could not find any useful info.
you need to have the permissions from the group owner
Change $id to $group_id and you'll be fine.
you ned to get apps this permession : publish_actions,publish_stream,user_groups,

Trying to offline post to facebook users wall, need error checking, cannot find it in documentation

I am trying to post to some users walls, i have the required permisions, but, some tokens are invalid, and some people only autherized the first step of oauth, and not the publish stream, so i am getting a bunch of errors, this is the code:
$config = array(
'appId' => APP_ID,
'secret' => APP_SECRET,
'cookie' => false,
);
$facebook = new Facebook($config);
$query = "SELECT * FROM `facebook-users` WHERE NOT token = '';";
$result = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($result)) {
// success
if (!empty($row['token'])) {
$attachment = array(
'access_token' => $row['token'],
'message' => $message,
'name' => $title,
'link' => $link,
'description' => $desc,
'picture'=>$pic,
'actions' => json_encode(array('name' => $action_name,'link' => $action_link))
);
$post_url = '/' . $row['uid'].'/feed';
$status = $facebook->api($post_url, "post", $attachment);
$count++;
}
echo $count;
die();
?>
So how do i error check on those basic errors, and continue even tho they occured?
You may need a long-live token. You may check permissions at /id/permissions, search for publish_stream. For testing, you can use the Graph API Explorer console
If the errors just show up when you do not have the required permissions to post to the user wall, why don't you just check it in the while loop?
Something like:
if(!($facebook->api($post_url, "post", $attachment))) continue;
You already got it:
Uncaught OAuthException: (#200)
Thus: put everything in try/catch blocks and catch those exceptions from the FB API properly. Its some hard work, since there is no doc on the possible errors like in the Google API, so you need to generate errors and look at the logs.

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

look at this fql code it returning null

<?
$fb_signed_request = $facebook->getSignedRequest();
$page_id = $fb_signed_request["page"]["id"];
$fb_like_status = $fb_signed_request["page"]["liked"];
$fb_is_admin = ($fb_signed_request["page"]["admin"]==1)?TRUE:FALSE;
$fql = "SELECT uid FROM page_admin WHERE page_id='" . $page_id . "'";
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$fqlResult = $facebook->api($param);
print_r ($fqlResult);
?>
I'm get the PAGE_ID ok, but FQLRESULT is null. I'm trying to get the id of the owner of the page..
Did you ask for manage_pages permission from the user? Check what access was granted via calling /me/permissions to see the complete list of items granted.

FQL returns "Requires valid signature"

I am having a wierd problem, things that worked before stopped working today, maybe it was bad before but now after the oAUTH 2 change, I am having troubles with a near production app
this is what I try
$params = array('method'=>'fql.query','query' => 'SELECT uid2 FROM friend WHERE uid1 = me()');
$result = $facebook->api($params);
I get:
Exception: 104: Requires valid signature
or more elaborated :
$config = array(
'appId' => 'XXXXXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX',
);
$facebook = new Facebook($config);
$uid = $facebook->getUser();
if ($uid){
try {
$access_t = $facebook->getAccessToken();
$fql = 'SELECT uid2 FROM friend WHERE uid1 = '.$uid;
$params = array('method' => 'fql.query', 'query' => 'SELECT uid2 FROM friend WHERE uid1 = '.$uid);
$result = $facebook->api($params);
echo $result;
$friends = $facebook->api(array('method' => 'fql.query', 'query' => $fql, 'access_token' => $access_t));
var_dump($friends);
} catch (FacebookApiException $e) {
echo $e;
}
this is the code I am using to validate the user and get the login info and permissions allowed:
$canvas_base_url = "https://apps.facebook.com/myapp/index.php?from=allow";
$params = array('scope' => 'publish_stream,email,offline_access,user_status,friends_status,friends_photos,user_photos,xmpp_login,user_online_presence,friends_online_presence',
'redirect_uri' => $canvas_base_url
);
$loginUrl = $facebook->getLoginUrl($params);
what am I doing wrong ?
This means the access token you are using is invalid. It has probably expired.
Here's a quote from the docs at http://developers.facebook.com/docs/authentication/:
In addition to the access token (the access_token parameter), the
response contains the number of seconds until the token expires (the
expires parameter). Once the token expires, you will need to re-run
the steps above to generate a new code and access_token, although if
the user has already authorized your app, they will not be prompted to
do so again. If your app needs an access token with an infinite expiry
time (perhaps to take actions on the user's behalf after they are not
using your app), you can request the offline_access permission.
So you should re-run the steps to generate an access token, or require the offline_access permission.

Categories