【问题标题】:Facebook page feed doesn't work with php file_get_contents()Facebook 页面提要不适用于 php file_get_contents()
【发布时间】:2012-10-24 03:55:58
【问题描述】:

当我通过浏览器访问此网址时,它会显示我期望的 json 提要:

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

当我在 PHP 中做一个

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

我收到一个 html 页面,说我的浏览器不受 facebook 支持,我应该升级。如何让 file_get_contents 返回我期望的 json 提要?

附加说明我也尝试过 bash wget https://www.facebook.com/feeds/page.php?format=json&amp;id=237173582992285 并且我下载的文件也有 html 内容说浏览器不支持。

【问题讨论】:

  • 使用 curl 并为浏览器设置标题。 Facebook 已检查该页面应仅在浏览器中打开
  • 这是一个httpS 链接,请注意SSL 的S,您需要将curl 与cacert.pem 或file_get_contents 中的SSL 一起使用。

标签: php json facebook-page


【解决方案1】:

试试这个,它对我有用

 $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 标头。

【讨论】:

  • 请注意,这违反了 Facebook 的服务条款。
  • @Eugen,复制粘贴?我刚刚看到您的代码不起作用,所以我设置了useragent。通常所有curl 代码都使用相同的变量,这可能是您看到这是您的代码的原因。无论如何,它不是我的代码,请相信:)
  • 我刚刚看到你的useragent 和我的useragent 不一样,这清楚地表明我无罪:)
  • 真的是你成就了我的一天!
【解决方案2】:

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

提要供 RSS 阅读器使用,而不是供脚本使用。理论上,您可以通过更改 User-Agent 标头来解决此问题,但这违反了 Facebook 的 terms of service

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

【讨论】:

    【解决方案3】:

    要获取页面的公开帖子,您应该使用其对应的连接,即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"
        }
      }
    }
    

    详细了解页面端点here

    【讨论】:

      【解决方案4】:
      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;
       }
      }
      

      【讨论】:

      • 如果您尝试过此操作,您会发现它不适用于相关 URL。
      • 如果您使用更新版本,您将违反 Facebook 的服务条款。但它应该工作:)
      • 我试过了,但没有用。正如我所提到的,您的编辑使其正常工作,但违反了 Facebook 的服务条款。
      【解决方案5】:
       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;
      }
      

      【讨论】:

      • 您好,Sem。请描述您为解决问题所做的工作。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-17
      • 2012-05-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      相关资源
      最近更新 更多