2

Is it possible to create a web site and, with JavaScript, read the feed from a Facebook page and display it to my sites visitor without them having to login, or even have Facebook for that matter.

I'm getting lost in the Facebook documentations :(

arpo
  • 1,831
  • 2
  • 27
  • 48
  • possible duplicate of [Can you get a public Facebook page's feed using Graph API without asking a user to allow?](http://stackoverflow.com/questions/9373645/can-you-get-a-public-facebook-pages-feed-using-graph-api-without-asking-a-user) – Donal Fellows Jan 15 '13 at 09:47

2 Answers2

2

You can do it on the server side with PHP. Create a facebook App in the Facebook developer center to get a App Key and Secret Key.

$profile_id = "1234567890";     

//App Info, needed for Auth
$app_id = "0001234567890";
$app_secret = "abc123ebf123f3g5g6j";

/* USE THIS LINE INSTEAD OF THE "veryfypeer" LINE BELOW */
Facebook::$CURL_OPTS[CURLOPT_CAINFO] = '/path/to/crt/fb_ca_chain_bundle.crt';

//retrieve auth token
$authToken = fetchUrl("https://graph.facebook.com/oauth/access_token?type=client_cred&client_id={$app_id}&client_secret={$app_secret}");

$data['feed_data'] = fetchUrl("https://graph.facebook.com/{$profile_id}/feed?{$authToken}");

function fetchUrl($url){
         $ch = curl_init();

/* DO NOT USE THE FOLLOWING LINE: I'VE COMMENTED IT OUT HERE */
//      curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
             curl_setopt($ch, CURLOPT_URL, $url);
             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
             curl_setopt($ch, CURLOPT_TIMEOUT, 20);

         $retData = curl_exec($ch);
         curl_close($ch); 

         return $retData;
    }

... on the Javascript side I think it's not possible, since the FB API Secret must be hidden from the public. Information taken from here

mch
  • 1,259
  • 15
  • 23
  • Thanks for that info. Is this code all I need or do I have to download Facebook SDK or install any other things to get it to work? – arpo May 03 '12 at 09:42
  • Should be all you need, except formatting the output in $data['feed_data'] according to your needs. – mch May 03 '12 at 09:59
  • By the way, you can read the feed of a person, or even the feed of a facebook page. The info you get depends on the users or page's privacy settings. – mch May 03 '12 at 10:03
  • Excelent. I rewrote you code so that it works like a proxy that I can read with JavaScript. – arpo May 03 '12 at 13:06
  • The CURLOPT_SSL_VERIFYPEER parameter was a security risk and there is a better solution. Have corrected the code above just now and hope the other posts will be corrected too. – mch Oct 16 '13 at 07:37
2

Thanks to @mch here commes version that uses php as a proxy to read a Facebook feed with JavaScript.

Place a file called proxy.php on your server and add this code:

<?php
// always add this header to ensure the JSON is output in the correct format
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
header('Content-Type: application/json; charset=utf-8'); 

$graphUrl = $_POST[graphUrl];
if ($graphUrl == "") {
    $graphUrl = "https://graph.facebook.com/facebook/feed/";
}

//App Info, needed for Auth
$app_id = "1234567890";
$app_secret = "0000011122234334445556aaass";

//retrieve auth token
$authToken = fetchUrl("https://graph.facebook.com/oauth/access_token?type=client_cred&client_id={$app_id}&client_secret={$app_secret}");

//Echo back json to read client side.
echo fetchUrl("{$graphUrl}?{$authToken}");

function fetchUrl($url){
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    $retData = curl_exec($ch);
    curl_close($ch); 
    return $retData;
}
?>

Change app_id, app_secret to your apps ids. Create apps here https://developers.facebook.com/apps/

Create a HTML file next to your proxyfile. Add this code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>FB reader</title>
</head>
<body>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {

            var timeout = 5000,
                load_error;

            load_error = function (jqXHR, textStatus, errorThrown) {
                if (errorThrown === "timeout") {
                    alert('Server bussy');
                } else {
                    alert('error: 404', textStatus + ": " + errorThrown);
                }
            };      

            $(document).ready(function() {
                console.log('Loading from Facebook...\n');

                //Change data: {graphUrl: 'https://graph.facebook.com/iambounty/feed'},  to what ever you want.
                $.ajax({
                    type: 'POST',
                    url: 'proxy.php',
                    data: {graphUrl: 'https://graph.facebook.com/iambounty/feed'}, 
                    timeout:  timeout,
                    error: load_error,
                    success: function (rv) {
                        var data = rv.data,
                            len = data.length,
                            i,
                            out = '';
                        for (i = 0; i < len; i += 1) {
                            if (data[i].description) {
                                out += data[i].description + '\n\n';
                            }
                        }
                        console.log(out);
                    }
                });

            });
        });
    </script>
</body>
</html>
arpo
  • 1,831
  • 2
  • 27
  • 48