curl 通过PHP获取Facebook个人资料的Meta标题

n3ipq98p  于 12个月前  发布在  PHP
关注(0)|答案(2)|浏览(96)

我试图通过下面的PHP函数获取网站的Meta标题。它适用于所有网站,但Facebook,我得到这个这个错误:
“更新浏览器|Facebook”

function fetchMeta($url)
{
$ch = curl_init();

$header = array();
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,"; 
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; 
$header[] = "Cache-Control: max-age=0"; 
$header[] = "Connection: keep-alive"; 
$header[] = "Keep-Alive: 300"; 
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; 
$header[] = "Accept-Language: en-us,en;q=0.5";

curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $urlX);

$data = curl_exec($ch);
curl_close($ch);

return $data;
}

$sites_html = fetchMeta("https://www.facebook.com/stackexchange");
echo $sites_html;

字符串
我需要改变什么才能让它工作?

3hvapo4f

3hvapo4f1#

尝试发送您的用户代理与它,现在,因为它的空白(或cURL的默认)

curl_setopt($ch, CURLOPT_USERAGENT, "Proper user agent string, maybe your browsers?");

字符串
Facebook检测到您正在使用不受支持的/旧的浏览器,因为它无法检测到现代用户代理。

gab6jxml

gab6jxml2#

<?php
// Fetching the meta title of a Facebook profile through PHP
ini_set("user_agent","facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)"); // EXTRA
function fetchMeta($url)
{
$ch = curl_init();
$timeout = 5; // EXTRA
$header = array();
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,"; 
$header[0] = "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; 
$header[] = "Cache-Control: max-age=0"; 
$header[] = "Connection: keep-alive"; 
$header[] = "Keep-Alive: 300"; 
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; 
$header[] = "Accept-Language: en-us,en;q=0.5";

curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
//curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
// EXTRA
curl_setopt($ch, CURLOPT_USERAGENT, "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)");
curl_setopt($ch, CURLOPT_REFERER, "http://facebook.com");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // SSL TRUE or FALSE
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POST, FALSE);
// EXTRA
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

$sites_html = fetchMeta("https://www.facebook.com/albdroid.official"); //Albdroid Test <Working OK>
//$sites_html = fetchMeta("https://www.facebook.com/stackexchange");  // stackexchange Test <Working OK>
echo $sites_html;
?>

字符串

相关问题