2

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

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 来获取内容。

4

2 回答 2

4

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

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

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

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

PHP SDK 的另一个解决方案:

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

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

于 2013-05-02T11:23:51.583 回答
0

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

CURLOPT_FAILONERROR

选项。在此处查看此选项和其他curl选项的说明。

以下代码返回与对同一 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;
于 2013-05-02T11:06:45.603 回答