Send facebook apprequest with PHP - php

I'm making a facebook app which will show a user's friends, then the user will choose a friend and the app will send that friend an apprequest with PHP.
With that apprequest I also will send the user's id, so that when the receiver plays the app, I will know that it will be thanks to that user.
To get a user's friends I'm using the following code
$friends_array = $facebook->api('/me/friends');
(I know this will only return a user's friends who have already accepted the app's permissions, so I'm using 2 test accounts which are friends)
To send the apprequest
If I use the following
$param = array(
'message' => 'Check out the latest update',
'data' => 'some_data_string',
"to" => $_POST["friend_id"],
"method" => "POST",
'access_token' => $facebook->getAccessToken(),
);
$tmp = $facebook->api("/me/apprequests", "POST", $param);
It is executed, but nothing is received.
If I use the following
$param = array(
'message' => 'Check out the latest update',
'data' => 'some_data_string',
'access_token' => $facebook->getAccessToken(),
);
$tmp = $facebook->api("/".$_POST["friend_id"]."/apprequests", "POST", $param);
I get the following error "Fatal error: Uncaught OAuthException: (#2) Failed to create any app request thrown ..."
The app is public and available to all users.
Do I need maybe to submit the app for any special permissions in order to post an apprequest?
Also if you have any other suggestion to have the desired results I'm referring to at the beginning, please reply.

Related

facebook api return nothing but with me I have the data

I make a Facebook app, now we change it from server and I run again the codes to test it, and I found that it works perfectly except for one FQL. I get the user info with ->(/me)
The FLQ works (I tested with Facebook tools). My connection and scope its correct. any idea?
I leave you my fql
$scope = 'email,read_stream';
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
// Get the current user
$user = $facebook->getUser();
$fql = "SELECT type, source_id, share_count, likes, permalink, description, post_id, message, target_id, created_time, attachment
FROM stream
WHERE source_id = me() and type in (46, 80)
order by created_time desc
LIMIT 500";
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$fqlResult = $facebook->api($param);
print_r($fqlResult);
I wouldn't use FQL personally as there's a reason they depreciate the old API's, but to access feed data of the user you need to have granted access from the API for your application, it should handle access_tokens automatically but when your user logs in with your website you want to be using
$params = array(
'scope' => 'read_stream, friends_likes',
// Access to feed data (read_stream) and whatever else you need
'redirect_uri' => 'https://www.myapp.com/post_login_page'
);
$loginUrl = $facebook->getLoginUrl($params);
Then you can access all the information you want with the api query:
$facebook->api('/me?fields=feed.fields(source,id,shares,likes,link,description,message,created_time)', 'GET');
I highly recommend using the Facebook Graph API Explorer to test API queries and retrieve all the information you need
Hope that helps mate!
In the parameter array, add one more parameter : access_token and give it the valid access token value
I found my problem,
the code works, but I was asking for two specific types.
and type in (46, 80)
When I remove it it appear all the stream :)
Thanks to everyone

send message to selected friends in facebook

I have the following code to send messages to selected friends by custom made
$friends = $facebook->api(array(
"method" => "fql.query",
"query" => "SELECT uid,name FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())"
));
where $facebook is the one having the details of the application & key values.,
& I get my friends list into the $friends
I'll be having a textarea too to enter the message that has to be sent,
now I'll be passing the selected list of friends & message to a function as follows
function facebook_send_message(to,message) {
FB.ui({
app_id:'MY APP ID',
method: 'send',
name: 'Abcdef',
link: 'http://apps.facebook.com/',
to:to,
message:message
},function(response){alert(response);});
}
when this function was called, a facebook popup opens, but my form in which all these content is placed gets submitted,
I'm not able to send the message to the selected friends, do anyone can help me in this issue..
For sending message with custom text, you have added 'message' parameter to FB.ui, but this feature is Deprecated. You can't pre-fill the message anymore.
This was deprecated might be because of bad advertisements.
So don't use 'message' parameter for message.

Posting to wall users use token

I have php code that posting in the wall of the user that use in my app. But in the post write
"post by _" (My app name).
Code:
$fql_info = $facebook->api($fql_query);
if($fql_info[0][offline_access] == 1)
{
$attachment = array(
'message' => "",
'name' => u("ghfhgfhfgh"),
'description' => u("fghgfhgfh"),
'link' => LINK,
'picture'=> "http://gfhgfhgfhfh"
);
$facebook->api('/'.$f[uid].'/feed', 'POST', $attachment);
$abc.=$f[uid]."\n";
echo $f[uid]."-".$fql_info[0][offline_access]."\n";
}
I want to know how I can to post but without my app name. User as poster itself, not the application has released it.
is it possible?
I guess it's not possible. The app name will appear in the post.
Sorry to say but i don't think this is possible . Facebook would not allow us to do that , it will always say Posted via : [Your APP name]

Need help posting to my own wall in Facebook with PHP

The code below is not working for some reason:
require './facebook.php';
$facebook = new Facebook(array(
'appId' => 'working_app_id',
'secret' => 'working_secret'
));
// Get User ID
$user = $facebook->getUser();
$token = $facebook->getAccessToken();
print $token; // token shows.
$attachment = array
(
'access_token'=>$token,
'message' => 'This is where the message will go.',
'name' => 'Name Text',
'link' => 'http://www.website.com',
'description' => 'Description text.'
);
$result = $facebook->api('/me/feed/','post',$attachment);
exit;
I am getting the following error:
Uncaught OAuthException: An active access token must be used to query information about the current user.
When I print the token, it appears to be a real token. I just have no idea what I could be doing wrong. All I want to do is post a very simple status update to my own page.
I don't know why I can't seem to figure this out. I'm banging my head against the wall on this one because it should just be so simple to post a quick little status update to my wall in Facebook from PHP.
Note: The App Type I created to get my appID and secret was a "Website".
Check out this answer, it appears that you are using the wrong method to get your access token.
Howto use FB Graph to post a message on a feed (wall)

Delete facebook post with Graph API - trouble getting this to work

I'm using the following to post a message on my Facebook page:
$attachment = array(
'access_token' => $access_token,
'message' => 'This is a test Message 4:',
'name' => "This is a test Name 4",
'link' => "http://slashdot.org/",
'description' => "This is a test Description 4"
);
$ret_code=$facebook->api('/me/feed', 'POST', $attachment);
This works great.
How do I delete the same post using the facebook GRAPH api? I read the docs and it says to issue a POST like:
https://graph.facebook.com/COMMENT_ID?method=delete
To test I set this up in a simple form with submit button, POSTing the data to https://graph.facebook.com/COMMENT_ID?method=delete (substituting COMMENT_ID fro the 11111111111_111111111111 id returned from the original publish call. This returns "This API call requires a valid app_id".
What is the correct way to issue a DELETE command?
Since you are using the php-sdk you just issue this call:
$facebook->api("/COMMENT_ID","DELETE");
You can use the following code:
Http::post('https://graph.facebook.com/'.$fb_action_id, array('method'=>'delete', 'access_token'=>$your_app_access_token));
This post will return a boolean value, true if successed and false if failed.
Its been discussed here Facebook SDK and Graph API Comment Deleting Error
you need to pass the access token too. You can delete all the milestones of a page like follows:
$milestones = $facebook->api('/PAGE_ID/milestones');
foreach($milestones[data] as $milestone)
{
echo $milestone['id'];
$args = array(
'access_token' => $pages_access_token
);
$deleted = $facebook->api($milestone['id'],"delete",$args);
if($deleted)
{
echo " <font color=\"green\">OK</font><br>";
}
else
{
echo " <font color=\"red\">ERR</font><br>";
}
}

Categories