Facebook页面供稿不适用于php file_get_contents() [英] Facebook page feed doesn't work with php file_get_contents()

查看:101
本文介绍了Facebook页面供稿不适用于php file_get_contents()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过浏览器进入该URL时,它显示了我期望的json提要:

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

在PHP中,我执行

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

我得到一个html页面,它说我的浏览器不受Facebook支持,因此我应该升级.如何使file_get_contents返回我期望的json提要?

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?

附加说明,我还尝试了bash wget https://www.facebook.com/feeds/page.php?format=json&id=237173582992285,并且我下载的文件也包含html内容,表示不支持浏览器.

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.

推荐答案

尝试一下,它对我有用

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

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.

这篇关于Facebook页面供稿不适用于php file_get_contents()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆