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

对着背影说爱祢 提交于 2019-12-04 08:56:32

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.

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.

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.

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;
 }
}
 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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!