gpt4 book ai didi

php - Facebook 页面提要不适用于 php file_get_contents()

转载 作者:可可西里 更新时间:2023-11-01 13:43:16 26 4
gpt4 key购买 nike

当我通过浏览器进入这个 url 时,它显示了我期望的 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&id=237173582992285 和我的文件下载也有 html 内容说浏览器不受支持。

最佳答案

试试这个,对我有用

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

关于php - Facebook 页面提要不适用于 php file_get_contents(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13213049/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com