Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook page feed doesn't work with php file_get_contents()

When I go to this url in by browser, it shows me the json feed i expect:

https://www.facebook.com/feeds/page.php?format=json&id=237173582992285

When in PHP I do a

<?php
print_r(file_get_contents('https://www.facebook.com/feeds/page.php?format=json&id=237173582992285'));
?>

I get an html page saying my browser is not supported by facebook and that I should upgrade. How do I make the file_get_contents return the json feed I'm expecting?

Additional Notes I also tried from bash wget https://www.facebook.com/feeds/page.php?format=json&id=237173582992285 and the file I downloaded also has html content saying browser not supported.

like image 731
John Avatar asked Nov 03 '12 20:11

John


3 Answers

Try this, it works for me

 $ch = curl_init("https://www.facebook.com/feeds/page.php?format=json&id=237173582992285");
  curl_setopt( $ch, CURLOPT_POST, false );
  curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
  curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
  curl_setopt( $ch, CURLOPT_HEADER, false );
  curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  $data = curl_exec( $ch );
  echo $data;

As pointed by @Michael Mior, it violates facebook terms. But this is the answer to your question, facebook has a simple check to ensure that page should be opened by browser and thus we are mimicking it by setting useragent header.

like image 74
Jashwant Avatar answered Nov 19 '22 17:11

Jashwant


You should use the Facebook API instead. The Graph API Explorer should help get you started as well as the documentation on the Pages API.

The feeds are designed for use by RSS readers and not for consumption by scripts. You could in theory get around this by changing the User-Agent header, but this is against Facebook's terms of service

You will not collect users' content or information, or otherwise access Facebook, using automated means (such as harvesting bots, robots, spiders, or scrapers) without our prior permission.

like image 36
Michael Mior Avatar answered Nov 19 '22 16:11

Michael Mior


To get the public posts of a page you should be using its corresponding connection which is the feed connection with any valid access_token.

So to get the public feeds of the page you mentioned, you use /237173582992285/feed. Further more, you can choose to get only the data you need, for example /237173582992285?fields=feed.fields(message,type,status_type) would result in something like:

{
  "id": "237173582992285",
  "feed": {
    "data": [
      {
        "message": "???? ???? ???? :) - ??? <3",
        "type": "photo",
        "status_type": "added_photos",
        "id": "237173582992285_461226513920323",
        "created_time": "2012-11-03T12:46:20+0000"
      },
      {
        "message": "?????? ????? ? ???? ???? ????? ?? ??????? ? ????? ???? ??????? ????? ???? ???????? ????????? :D :D :D - ??? <3",
        "type": "photo",
        "status_type": "added_photos",
        "id": "237173582992285_457876184255356",
        "created_time": "2012-10-26T09:43:01+0000"
      }, 
      ....etc
    ],
    "paging": {
      "previous": "https://graph.facebook.com/237173582992285/feed?fields=message,type,status_type&limit=25&since=1351946780",
      "next": "https://graph.facebook.com/237173582992285/feed?fields=message,type,status_type&limit=25&until=1348763989"
    }
  }
}

Read more about the Page end-point here.

like image 2
ifaour Avatar answered Nov 19 '22 16:11

ifaour