5

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.

4

5 回答 5

8

试试这个,它对我有用

 $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;

正如@Michael Mior 所指出的,它违反了Facebook 条款。但这是您问题的答案,facebook 有一个简单的检查来确保页面应该由浏览器打开,因此我们通过设置useragent标题来模仿它。

于 2012-11-03T20:23:39.670 回答
2

您应该改用 Facebook API。Graph API Explorer以及Pages API上的文档应该可以帮助您入门。

提要供 RSS 阅读器使用,而不是供脚本使用。理论上你可以通过更改User-Agent标题来解决这个问题,但这违反了 Facebook 的服务条款

未经我们事先许可,您不得使用自动化方式(例如收获机器人、机器人、蜘蛛或刮刀)收集用户的内容或信息,或以其他方式访问 Facebook。

于 2012-11-03T20:17:27.440 回答
2

要获取页面的公开帖子,您应该使用其相应的连接,即feed与任何有效的连接access_token

因此,要获取您提到的页面的公共提要,请使用/237173582992285/feed. 此外,您可以选择仅获取您需要的数据,例如/237173582992285?fields=feed.fields(message,type,status_type) 会产生如下结果:

{
  "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"
    }
  }
}

在此处阅读有关页面端点的更多信息。

于 2012-11-03T20:25:36.813 回答
0
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;
 }
}
于 2012-11-03T20:14:16.960 回答
0
 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;
}
于 2019-06-05T17:50:46.287 回答