0

I have a list of Facebook links in xls sheet with Facebook id something like this enter image description here

and when i go through these links means if i access Facebook with id www.facebook.com/7500

i get urls something like this https://www.facebook.com/davesachs/

so my question is i want to do this with PHP, i have a PHP page which read data from xls sheet my code here:

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();
}

its returning all Facebook link with id same that i am passing like www.facebook.com/7500 but i want it return URL / link of profile as https://www.facebook.com/davesachs/ ,if it is possible please help me to do that.

Tausif
  • 420
  • 2
  • 15

1 Answers1

1

You can do something like that, Taken reference from 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
}
?>

Dinesh Suthar
  • 853
  • 1
  • 17
  • 41
  • its returning unsupported browser and when i use ua $ua = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.16 (KHTML, like Gecko) \ Chrome/24.0.1304.0 Safari/537.16'; curl_setopt($ch, CURLOPT_USERAGENT, $ua); its returning the same url that i am passing not the result url i wanted – Tausif Jan 28 '21 at 09:42