PHP - Facebook API: Alerting the user of a change... simply enough - php

It would seem that this would be a simple thing to do, but I just can't seem to find anything on how to accomplish this on the Facebook Developer forum, the API nor Google.
I'm using the PHP SDK to write a simple app that looks for a certain change regarding the current user (not to seem mysterious, it's just that this part is irrelevant).
As soon as this change is detected, I want to somehow alert the current user of the change, just not by posting it to the user's wall, this information needs to stay confidential - it needs to stay between the user and the app.
According to the Facebook API documentation, notifications are generated automatically - so I have no control over them, and I can't seem to get the Bookmark counter to update automatically either - I have to problem getting it to increment when the user visits the app canvas page and something occurs, but what I want is to alert the user of a certain event without requiring the user to actually visit the app canvas.

I would guide you to the Request dialog page because it's (supposed to be) the right place to look. But as usual something is missing in the documentation.
Anyway, what you are looking for is a application-generated request:
<?php
$app_id = YOUR_APP_ID;
$app_secret = YOUR_APP_SECRET;
$token_url = "https://graph.facebook.com/oauth/access_token?" .
"client_id=" . $app_id .
"&client_secret=" . $app_secret .
"&grant_type=client_credentials";
$app_access_token = file_get_contents($token_url);
$user_id = THE_CURRENT_USER_ID;
$apprequest_url ="https://graph.facebook.com/" .
$user_id .
"/apprequests?message=’INSERT_UT8_STRING_MSG’" .
"&data='INSERT_STRING_DATA'&" .
$app_access_token . "&method=post";
$result = file_get_contents($apprequest_url);
echo("Request id number: ", $result);
?>
Described in a "blog post" instead of the documentation! and can be fount in this document (thanks #Charles).

I'm using the PHP SDK to write a simple app that looks for a certain change regarding the current user (not to seem mysterious, it's just that this part is irrelevant).
Actually, that's not entirely irrelevant, as the Facebook TOS prevents you from doing this in most situations, especially defriending, relationship statuses, etc.
As soon as this change is detected, I want to somehow alert the current user of the change, just not by posting it to the user's wall, this information needs to stay confidential - it needs to stay between the user and the app.
Request their e-mail and send it that way.

Related

PHP upload photo to users facebook wall using php only

I am trying to write a php script that can upload a daily photo to my personal facebook wall. I need this to be done at anytime and not just when I am logged in. I am wondering if it is possible to upload a photo to my wall using php only or do I need to use an app with the right permissions. If so do I have to personally install that app and give correct permissions for my page to make this possible, if it is possible, and can this be done automatically?
What I want to do:
In short I want a users twitter feed to be converted and put on an image. I have the script that automatically does this already. However I would love it if after the twitter user tweets then this calls my script and converts it to an image (again I have done this code already) but...
Now I need a script that can automatically upload this to my personal wall after the image is created. Is this possible?
Well, you can use the extended user access token to get it working for a maximum of 60 days. After that, the token will expire and then you'll have to refresh the token and that is not possible without the user's interaction.
The documentation states the same:
Even the long-lived access token will eventually expire. At any point, you can generate a new long-lived token by sending the person back to the login flow used by your web app - note that the person will not actually need to login again, they have already authorized your app, so they will immediately redirect back to your app from the login flow with a refreshed token - how this appears to the person will vary based on the type of login flow that you are using, for example if you are using the JavaScript SDK, this will take place in the background, if you are using a server-side flow, the browser will quickly redirect to the Login Dialog and then automatically and immediately back to your app again.
After doing the above you will obtain a new short-lived token and then you need to perform the same exchange for a long-lived token as above.
To get the extended token make the following call from your server:
GET /oauth/access_token?
grant_type=fb_exchange_token&
client_id={app-id}&
client_secret={app-secret}&
fb_exchange_token={short-lived-token}
Code
$app_id = APP_ID;
$app_secret = APP_SECRET;
$post_login_url = APP_URL;
$album_name = 'test';
$album_description = 'desc';
$code = $_REQUEST["code"];
//Obtain the access_token with publish_stream permission
if(empty($code))
{
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode($post_login_url)
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url .
"'</script>");
}
else {
// access token
$token_url= "https://graph.facebook.com/oauth/"
. "access_token?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token']; // <---
// extended token
$extended_token_url= "https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=".$app_id."&client_secret=".$app_secret."&fb_exchange_token=".$access_token;
$response = file_get_contents($extended_token_url);
$params = null;
parse_str($response, $params);
$extended_access_token = $params['access_token']; // <---
// Upload to the photos to the album here
}
That's the complete auth process, incl the extended token. You can find many posts regarding the upload of the photo, simply use the $extended_access_token with your calls.
(In case you are not aware, Graph API Explorer is a fantastic tool of facebook to test the APIs).
Good luck!

Can I use open graph simply to obtain public facebook information?

I am trying to use open graph to read story information off a public facebook page (http://www.facebook.com/onlinehorsesupplies). I want to obtain the message content of the latest post for use elsewhere on the related ecommerce website. I could scrape it but using open graph seems like it should be a better way to go.
Using the http://developers.facebook.com/tools/explorer I found the URL I need to use quite easily but I had to click on the 'get access token' to make it work. I use this URL:
https://graph.facebook.com/188288097875046?fields=posts&access_token=
If I use the access token copied off the explorer page it works fine. If I use an access token off my own app it fails with "Bad request...". How can I get an appropriate access token in my php code?
I think your app needs to request a token first. At least thats how it worked for me.
Something like:
$app_token_url = "https://graph.facebook.com/oauth/access_token?" . "client_id=" . $app_id . "&client_secret=" . $app_secret .
"&grant_type=client_credentials";
$response = file_get_contents( $app_token_url );
parse_str( $response, $fbinfo );
then you can use that token for the request...

Can't publish a high score to player's timeline on Facebook

I'm developing a PHP game and would like to post players highscores to their own facebook wall /timeline.
I've set up a Facebook App and the PHP code I'm using to POST the score is (as provided by Facebook itself):
<?php
require 'facebook-sdk/facebook.php';
$app_id = MY_APP_ID;
$app_secret = MY_APP_SECRET;
$score = 1500; // this is gonna be passed someway...
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
$user = MY_USER_ID; // to be replaced with a call to $facebook->getUser()
$app_access_token = get_app_access_token($app_id, $app_secret);
$facebook->setAccessToken($app_access_token);
$response = $facebook->api('/' . $user . '/scores', 'post', array(
'score' => $score,
));
print($response);
// Helper function to get an APP ACCESS TOKEN
function get_app_access_token($app_id, $app_secret) {
$token_url = 'https://graph.facebook.com/oauth/access_token?'
. 'client_id=' . $app_id
. '&client_secret=' . $app_secret
. '&grant_type=client_credentials';
$token_response =file_get_contents($token_url);
$params = null;
parse_str($token_response, $params);
return $params['access_token'];
}
?>
Of course there is a login and install section which I have omitted, asking the user to login and grant 'publish_stream' and 'publish_actions' privileges to the app.
This is working with success, the response variable outputs 1.
I can see the posted score using the Facebook Graph API Explorer so I assume everything is really working fine and smooth.
The problem is that I am not able to see the supposedly posted user-story anywhere on Facebook.
Reading the documentation it seems to me that a user story has to be automatically published when one saves a score. As an example, have a look here or here.
Does anyone solved this problem already? Do you see something that I might have missing? Can you please point me at the right direction to solve this issue?
Any help will be highly appreciated.
You write
Reading the documentation it seems to me that a user story has to be automatically published when one saves a score.
Scores are not automatically published. They are only published under certain conditions, namely when a user:
gets a new high score ("High score story").
pass another friend's score("Passing story").
In your code you post the score 1,500 everytime. After the first time you post it, when you post it again repeatedly for testing, your post request will be successful but the score will not be published again since it is not a new high.
Sources:
Facebook Developers: Games Tutorial.Facebook Developers Developer Blog: Games Update: Expanding distribution for Scores and Achievements
Try look at this
https://github.com/fbsamples/CriticalMass/tree/master/web/criticalmass
Hope will be a useful
You can create a post on an application's profile page by issuing an HTTP POST request to APP_ID/feed (not PROFILE_ID/posts) with the publish_stream permissions.
More Details : TechNew.In
If your problem is 'When I successfully post a score to the API, it doesn't necessarily create a story in News Feed or on Timeline' this isn't a problem - this is how the scores API works.
Scores are a lightweight sharing option, and aren't always shown individually - i rarely see 'User got score X' stories on Facebook, but see 'X beat Y's Score in Z' and 'X got a new high score' fairly often - There's also a Timeline unit on a user's profile showing a summary of gaming activity and the scores data is shown there.
Just keep posting to /[user]/scores when the user gets a new high score and let Facebook take care of the distribution
I read something recently that FB has stopped allowing Api posts to timelines. It will still show up in the news feed but just not on their wall anymore.
I think i've found the issue for you.
https://developers.facebook.com/docs/opengraphprotocol/#types
See the statement:
Pages of type article or video do not have publishing rights, and will not show up on user's profiles because they are not real world objects.

How do I change this example Facebook code to post feed data automatically?

I took this code from Facebook's documentation to get me started in learning how to get my app to post to a feed.
It works as described, in that when I go to my app's canvas URL, I am presented with a dialog where I can enter in text, click "share", and it posts into my timeline. So far, so good.
But I want to alter it so that instead of explicitly typing something in and clicking a button, a post is automatically sent to the feed, triggered be events in my PHP code.
However, and I realize this is a newbie kind of question, I can't figure out how to adjust the code to make that happen. My experiments either just break the code or end up with the same dialog.
How do I get the PHP to post a message straight into the feed, and then display the app's canvas URL immediately after (so as not to get caught in a loop of constantly reloading and posting over and over...)?
For convenience, here is the same code from the Facebook documentation:
<?php
$app_id = "YOUR_APP_ID";
$canvas_page = "YOUR_CANVAS_PAGE_URL";
$message = "Apps on Facebook.com are cool!";
$feed_url = "https://www.facebook.com/dialog/feed?app_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page)
. "&message=" . $message;
if (empty($_REQUEST["post_id"])) {
echo("<script> top.location.href='" . $feed_url . "'</script>");
} else {
echo ("Feed Post Id: " . $_REQUEST["post_id"]);
}
?>
You may achieve this by requiring publish_stream permission (see Publishing Permissions).
Once your app have that permission granted by user you may publish feed stories without showing Feed Dialog to user.
This also allow you to publish content to user's feed without need of user's active access_token and using application access_token for that purpose.
It's really easy to implement this using PHP-SDK:
$facebook = new Facebook(/*...*/);
$facebook->api('/USER_ID/feed', 'post', array(
'message'=>'Text entered by user'
));
Probably publish_actions permission can be sufficient for that task, but documentation isn't yet updated across developers site so it's safer to use publish_stream (see statuses for user object).
The publish_stream permission is a superset of publish_actions allowing everything that publish_actions allows plus more. Some of the additional publishing capabilities are:
posting to a friend's feed
posting questions
creating notes
posting content to events or groups

how to send invites to uninvited friends in facebook app?

i'm using this code to invite friends in my facebook application
$app_id = "000000000000000000000";
$canvas_page = "http://apps.facebook.com/applicationname/";
$message = "join me in this app.";
$requests_url = "https://www.facebook.com/dialog/apprequests?app_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page)
. "&message=" . $message;
if (empty($_REQUEST["request_ids"])) {
echo("<script> top.location.href='" . $requests_url . "'</script>");
every time the user call this page he can see all his friends
i need to view only the UNinvited friends.
how?
A better method would be to use the javascript SDK. You can use that to render a facebook popup with the share dialog inside...
With regard to your question, you can use filters for the apprequest method as stated here. You are looking for the app_non_users filter.
[EDIT]
If you want to filter even more - ie. don't list friends that have already been invited (but not yet accepted), you'll have to read each request_id generated and store the invited users facebook UID and place them in the exclude_ids filter. This method could be further optimized by using the javascript sdk and FB.ui() method, posting the generated request_ids using AJAX, reading the request_id server-side and returning the users UID to be added to a javascript array in order to be used in the exclude_ids filter for subsequent FB.ui() invite calls...

Categories