php - 如何下载 Facebook 页面?

标签 php curl

我想使用 PHP 下载 Facebook 网页。我尝试过 file_get_contents 和stream_context_create。我也尝试过 curl 。但 Facebook 仅返回此消息:

Update Your Browser You’re using a web browser that isn’t supported by Facebook. To get a better experience, go to one of these sites and get the latest version of your preferred browser:

我错过了什么吗?

这是 Curl 代码:

       $url="https://www.facebook.com/media/set/?set=a.189662541197403.1073741845.188398434657147&type=1&l=a8755a774e";
        $custom_headers = array();
        $custom_headers[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        $custom_headers[] = "Pragma: no-cache";
        $custom_headers[] = "Cache-Control: no-cache";
        $custom_headers[] = "Accept-Language: en-us;q=0.7,en;q=0.3";
        $custom_headers[] = "Accept-Charset: utf-8,windows-1251;q=0.7,*;q=0.7";

        $ch = curl_init();
        $useragent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1";
        curl_setopt($ch, CURLOPT_USERAGENT, $useragent); // set user agent
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $custom_headers);

        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,20);
        curl_setopt($ch, CURLOPT_TIMEOUT, 40); //timeout in seconds

        $txResult = curl_exec($ch);

        $statuscode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        print "$txResult";

最佳答案

改用 API 吗?

您提到的 URL 的数据也可以从此 URL 中检索:

http://graph.facebook.com/?id=189662541197403

此外,这会将您的整个代码减少到这一行:

json_decode(file_get_contents('http://graph.facebook.com/?id=189662541197403'));

关于php - 如何下载 Facebook 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17591273/

相关文章:

bash - 在 Azure 管道中,linux 命令在脚本文件末尾被截断

php - 查看 Paypal API - 什么是 cURL?

php - 在 PHP (RegEx) 中提取已加载页面的部分内容

curl - 如何隐藏curl elasticsearch命令的输出消息?

javascript - 对特定的 PHP 函数进行 jQuery AJAX 调用

php - MySQL从单独的表中按索引ID排序

php - 从网站下载pdf文件并在下载后更改文件名

php - php 变量的算术运算

php - 尝试从数据库中删除某些内容时,RouteCollection.php 中的 Laravel 5.1 NotFoundHttpException

c++ - https 后失败 C++