Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中刮取facebook粉丝页面_Php_Curl_Web Scraping_Facebook Page - Fatal编程技术网

在PHP中刮取facebook粉丝页面

在PHP中刮取facebook粉丝页面,php,curl,web-scraping,facebook-page,Php,Curl,Web Scraping,Facebook Page,我正试图用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, $use

我正试图用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来获取内容。

问题似乎是,由于无效的访问令牌,服务器返回了一个400错误请求。这又导致
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;

问题似乎在于,由于访问令牌无效,服务器返回了一个400错误请求。这又导致
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;

页面是公共的,即使使用应用程序访问令牌,也可以读取提要。 尝试按如下方式更改访问令牌:

$token = "APP-ID|APP-SECRET";
(应用程序ID和应用程序机密,中间有一个管道)

这是唯一一个永远不会过期的令牌,只有当你更改了你的应用程序的ID或秘密时

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

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

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

页面是公共的,即使使用应用访问令牌,也可以读取提要。 尝试按如下方式更改访问令牌:

$token = "APP-ID|APP-SECRET";
(应用程序ID和应用程序机密,中间有一个管道)

这是唯一一个永远不会过期的令牌,只有当你更改了你的应用程序的ID或秘密时

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

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

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

尝试添加curl\u setopt($ch,CURLOPT\u SSL\u VERIFYPEER,false);为什么要刮取网页?尝试添加curl\u setopt($ch,CURLOPT\u SSL\u VERIFYPEER,false);你为什么要刮网页?