30

I have this link:

http://libero-news.it.feedsportal.com/c/34068/f/618095/s/2e34796f/l/0L0Sliberoquotidiano0Bit0Cnews0C12735670CI0Esaggi0Eper0Ele0Eriforme0Ecostituzionali0EChiaccherano0Ee0Eascoltano0Bhtml/story01.htm

If you visit it, this become:

http://www.liberoquotidiano.it/news/1273567/I-saggi-per-le-riforme-costituzionali-Chiaccherano-e-ascoltano.html

How can I get the second link from the first?

I have tried this but don't work, returning me the same first link:

<?php
$url="http://libero-news.it.feedsportal.com/c/34068/f/618095/s/2e34796f/l/0L0Sliberoquotidiano0Bit0Cnews0C12735670CI0Esaggi0Eper0Ele0Eriforme0Ecostituzionali0EChiaccherano0Ee0Eascoltano0Bhtml/story01.htm";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$a = curl_exec($ch);
print_r($a);echo"<br>";
if(preg_match('#Location: (.*)#', $a, $r)){
   $l = trim($r[1]);
   echo $l;
}else echo "not working";

Thanks a lot.

Machavity
  • 30,841
  • 27
  • 92
  • 100
michele
  • 26,348
  • 30
  • 111
  • 168
  • It's clear in the code ? `curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);` – HamZa Jul 04 '13 at 14:06
  • Sorry, I checked the source code. The browser is redirected with javascript. Thus you have to read the html/js code, parse it, get the url and then get the content from that url... – HamZa Jul 04 '13 at 14:10

4 Answers4

58

Thanks to @king-isaac the following code was tested and it works.

<?php 

$url="http://libero-news.it.feedsportal.com/c/34068/f/618095/s/2e34796f/l/0L0Sliberoquotidiano0Bit0Cnews0C12735670CI0Esaggi0Eper0Ele0Eriforme0Ecostituzionali0EChiaccherano0Ee0Eascoltano0Bhtml/story01.htm";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true); // true to include the header in the output.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Must be set to true true to follow any "Location: " header that the server sends as part of the HTTP header.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // true to return the transfer as a string of the return value of curl_exec() instead of outputting it directly.

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

$finalUrl = 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 $finalUrl; // Voila
?>
Truefalse
  • 794
  • 5
  • 16
  • Good solution, can you add support to cokies? see [this DOI redirector](http://doi.org/10.1016/S1473-3099(16)30318-8), returning http://secure.jbs.elsevierhealth.com/action/cookieAbsent – Peter Krauss Jun 22 '17 at 15:37
  • 2
    If you do not want to download extra kilobytes, it is better to add curl_setopt($ch, CURLOPT_NOBODY, true); Unfortunately, this does not save from the second request, so it is better to parse the headers manually – Geograph Mar 11 '19 at 18:57
6

You can do it without using CURL. Made it simple and short.

<?php
$url="http://libero-news.it.feedsportal.com/c/34068/f/618095/s/2e34796f/l/0L0Sliberoquotidiano0Bit0Cnews0C12735670CI0Esaggi0Eper0Ele0Eriforme0Ecostituzionali0EChiaccherano0Ee0Eascoltano0Bhtml/story01.htm";

$headers = @get_headers($url);
$final_url = "";
foreach ($headers as $h)
{
    if (substr($h,0,10) == 'Location: ')
    {
    $final_url = trim(substr($h,10));
    break;
    }
}
echo $final_url;
?>
Rachit Mangi
  • 119
  • 2
  • 4
  • 4
    You don't need to use foreach, the get_headers method has a param (format) that, when non-zero, makes the array keys as the headers values so you can do something like this: `$headers = @get_headers($url, 1);` `echo $headers['Location'];` See more here https://www.php.net/manual/en/function.get-headers.php – aliasbody Jul 16 '20 at 10:25
4

You can get the final URL of a request with curl_getinfo.

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
Isaac Hildebrandt
  • 1,018
  • 5
  • 16
  • this can't be used in this example, the first click of the link just contains an advert and a link to the second link. it is not a header redirect. It only becomes a header redirect when the user has clicked it once and has a cookie, i assume... – rorypicko Jul 04 '13 at 14:11
  • ERROR: `PHP Warning: curl_setopt() expects parameter 1 to be resource, string` – Peter Krauss Jun 22 '17 at 15:32
1

If you want to get first redirect:

function getRedirect($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36');
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $responseHeaders = curl_exec($ch);
    curl_close($ch);
    if(preg_match_all("/^location: (.*?)$/im", $responseHeaders, $results))
        return $results[1][0];
}
Mirko
  • 2,231
  • 2
  • 21
  • 17