【问题标题】:Scrape facebook fan page in PHP用 PHP 抓取 facebook 粉丝页面
【发布时间】:2013-04-26 11:59:55
【问题描述】:

我正在尝试在 php 中使用 curl 来抓取 Facebook 粉丝专页,但它只是给了我一个空白页面。 这是我的代码。

function curlFunction($source_url){
  $ch = curl_init();

  $userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1';
  curl_setopt($ch, CURLOPT_USERAGENT,       $userAgent);
  curl_setopt($ch, CURLOPT_URL,             $source_url);
  curl_setopt($ch, CURLOPT_HEADER,      false);
  curl_setopt($ch, CURLOPT_FAILONERROR,     true);
  curl_setopt($ch, CURLOPT_ENCODING,        "UTF-8" );
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION,  true);
  curl_setopt($ch, CURLOPT_AUTOREFERER,         true);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER,  true);
  curl_setopt($ch, CURLOPT_TIMEOUT,             60);

  $html= curl_exec($ch);
  curl_close($ch);
  return $html;
}   

$token = "CAACEdEose0cBADMEK5uLLfSTj1nZCG8eogAZBi6Dfkr4gJN9o6fFuyfEHkPtO94br9i9YP9gmiYPunHxRxr1PqU3YNy34PziACwEaMXl4NT9zZBMgdWD6WFh6mAL2dlqsjnYs9sKQ5sz7ZCVBn7ZA8lVrZCJRq8O0ZD";

$url = "https://graph.facebook.com/StarHub/feed?accesstoken=" . $token;

$html = curlFunction($url, $info);
echo $html;

我已经在其他网站上使用了这个功能来抓取页面,它工作正常。然后我遇到了这个问题,当我使用 https 它给了我空白页但是当我只使用 http 它工作正常,但是 facebook graph api 要求我使用 https 来获取内容。

【问题讨论】:

  • 尝试添加 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  • 你为什么要抓取网页?

标签: php curl web-scraping facebook-page


【解决方案1】:

页面是公开的,即使使用应用访问令牌也可以读取提要。 尝试像这样更改访问令牌:

$token = "APP-ID|APP-SECRET";

(App ID 和 App Secret,中间有一个管道)

这是唯一永不过期的令牌,只有在您更改应用程序的 ID 或 Secret 时。

使用 PHP SDK 的另一种解决方案:

$result = $facebook->api('/PAGE-ID/feed', array('access_token' => 'APP-ID|APP-SECRET'));
var_dump($result['data']);

您甚至可以在没有访问令牌的情况下执行此操作,如果没有用户被授权,则无论如何都应该使用应用程序访问令牌。

【讨论】:

    【解决方案2】:

    问题似乎是由于访问令牌无效,服务器返回 400 Bad Request 错误。这反过来导致 curl 返回一个空字符串,因为

    CURLOPT_FAILONERROR
    

    选项。请参阅此选项和其他 curl 选项 here 的说明。

    以下代码返回与对同一 URL 的常规浏览器请求相同的结果:

    function curlFunction($source_url){
      $ch = curl_init();
    
      $userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1';
      curl_setopt($ch, CURLOPT_USERAGENT,       $userAgent);
      curl_setopt($ch, CURLOPT_URL,             $source_url);
      curl_setopt($ch, CURLOPT_HEADER,      false);
      curl_setopt($ch, CURLOPT_FAILONERROR,     true);
      curl_setopt($ch, CURLOPT_ENCODING,        "UTF-8" );
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION,  true);
      curl_setopt($ch, CURLOPT_AUTOREFERER,         true);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER,  true);
      curl_setopt($ch, CURLOPT_TIMEOUT,             60);
    
      $html= curl_exec($ch);
      curl_close($ch);
      return $html;
    }   
    
    $token = "CAACEdEose0cBADMEK5uLLfSTj1nZCG8eogAZBi6Dfkr4gJN9o6fFuyfEHkPtO94br9i9YP9gmiYPunHxRxr1PqU3YNy34PziACwEaMXl4NT9zZBMgdWD6WFh6mAL2dlqsjnYs9sKQ5sz7ZCVBn7ZA8lVrZCJRq8O0ZD";
    
    $url = "https://graph.facebook.com/StarHub/feed?accesstoken=" . $token;
    
    $html = curlFunction($url, $info);
    echo $html;
    

    【讨论】:

      猜你喜欢
      • 2011-04-03
      • 2023-03-07
      • 1970-01-01
      • 2011-04-05
      • 2011-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多