Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php get url of redirect from source url

Tags:

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.

like image 944
michele Avatar asked Jul 04 '13 14:07

michele


People also ask

How can I get redirected URL in PHP?

Answer: Use the PHP header() Function You can simply use the PHP header() function to redirect a user to a different page. The PHP code in the following example will redirect the user from the page in which it is placed to the URL http://www.example.com/another-page.php . You can also specify relative URLs.

How to redirect to a different page using PHP?

In PHP, when you want to redirect a user from one page to another page, you need to use the header() function. The header function allows you to send a raw HTTP location header, which performs the actual redirection as we discussed in the previous section.

How to redirect to another page after successful login in PHP?

if ($count == 1){ $_SESSION['username'] = $username; header("location:redirectafterlogin. php"); //header to redirect, but doesnt work }else{ //3.1. 3 If the login credentials doesn't match, he will be shown with an error message.


2 Answers

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); 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 ?> 
like image 147
Truefalse Avatar answered Oct 07 '22 02:10

Truefalse


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; ?> 
like image 32
Rachit Mangi Avatar answered Oct 07 '22 01:10

Rachit Mangi