使用 PHP 从 Facebook 个人资料 ID 获取 Facebook 个人资料 URL

时间:2021-01-28 07:53:41

标签: php facebook

我在 xls 表中有一个 Facebook 链接列表,其中 Facebook id 是这样的 enter image description here

当我浏览这些链接时意味着如果我使用 ID www.facebook.com/7500

访问 Facebook

我得到类似这样的网址 https://www.facebook.com/davesachs/

所以我的问题是我想用 PHP 来做这件事,我有一个 PHP 页面,它从 xls 表中读取数据 我的代码在这里:

require_once 'src/SimpleXLSX.php';
if ( $xlsx = SimpleXLSX::parse('fburl.xlsx') ) {
$rows= $xlsx->rows();

foreach($rows as $data){
echo $data[0].'<br>';
}


} else {
echo SimpleXLSX::parseError();
}

它返回所有与我传递的 ID 相同的 Facebook 链接,如 www.facebook.com/7500,但我希望它返回 URL/个人资料链接为 https://www.facebook.com/davesachs/,如果可能,请帮助我这样做。< /p>

1 个答案:

答案 0 :(得分:1)

你可以这样做,参考here

<?php 

require_once 'src/SimpleXLSX.php';
if ( $xlsx = SimpleXLSX::parse('fburl.xlsx') ) {
$rows= $xlsx->rows();

foreach($rows as $data){
  getRedirectedUrl($data[0]);
}


} else {
echo SimpleXLSX::parseError();
}


function getRedirectedUrl($link){
$url=$link;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Must be set to true so that PHP follows any "Location:" header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$a = curl_exec($ch); // $a will contain all headers

$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); // This is what you need, it will return you the last effective URL

// Uncomment to see all headers
/*
echo "<pre>";
print_r($a);echo"<br>";
echo "</pre>";
*/

echo $url; // Voila
}
?>