Facebook page feed not working with php file_get_contents ()

When I go to this URL in the 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

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

I get an html page that says my browser is not supported by Facebook and that I need to update. How to make file_get_contents return the expected json feed?

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

+5
source share
5 answers

Try 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 out by @Michael Mior, this violates facebook terms. But this is the answer to your question, facebook has a simple check to ensure that this page should be opened by the browser, and therefore we simulate its useragent header setting.

+7
source

Instead, you should use the Facebook API. The Graph API Explorer should help you get started, as well as the documentation for the API pages .

Feeds are intended for use by RSS readers, and not for use by scripts. Theoretically, you can get around this by changing the User-Agent header, but this is against the Terms of Use

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

+2
source

In order to receive public messages on the page, you must use the appropriate connection, which is the feed connection with any valid access_token .

So, to get the public channels of the page you specify, you use /237173582992285/feed . In addition, you can select only the data you need, for example /237173582992285?fields=feed.fields(message,type,status_type) will lead to 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 endpoint of the page here .

+2
source
 function load_url($url) { $ch = curl_init($url); curl_setopt( $ch, CURLOPT_POST, false ); curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true ); curl_setopt( $ch, CURLOPT_HEADER, false ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)'); $received_data = curl_exec( $ch ); if($received_data){ return $received_data; } else { return false; } } 
0
source
  function get_facebook_id($facebookUrl) { $ch = curl_init($facebookUrl); 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 ); $fbResponse = curl_exec( $ch ); if($fbResponse) { $matches = array(); if (preg_match('/"entity_id":"([0-9])+"/', $fbResponse, $matches)) { $jsonObj = json_decode("{" . $matches[0] . "}"); if($jsonObj) { $facebookId = $jsonObj->entity_id; } } } return $facebookId; } 
0
source

Source: https://habr.com/ru/post/1443868/


All Articles