Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>With <strong>curl</strong>, use this: It follows the redirect until it finds the endpoint. In the included code I used goo.gl to shorten the url to a rando image. You will see the output is the original link (and it would output whatever number of redirects and their URLs), and the final redirect to the actual file. I think this is what you are looking for. I did not write this originally, but found it somewhere some time ago and reused it many times again with tweaking when needed. It seems to fit well in many places. Glad to pass it on. I think it might help to achieve what you are looking for. </p> <pre><code>&lt;?php function follow_redirect($url){ $redirect_url = null; if(function_exists("curl_init")){ $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_NOBODY, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); } else{ $url_parts = parse_url($url); $sock = fsockopen($url_parts['host'], (isset($url_parts['port']) ? (int)$url_parts['port'] : 80)); $request = "HEAD " . $url_parts['path'] . (isset($url_parts['query']) ? '?'.$url_parts['query'] : '') . " HTTP/1.1\r\n"; $request .= 'Host: ' . $url_parts['host'] . "\r\n"; $request .= "Connection: Close\r\n\r\n"; fwrite($sock, $request); $response = fread($sock, 2048); fclose($sock); } $header = "Location: "; $pos = strpos($response, $header); if($pos === false){ return false; } else{ $pos += strlen($header); $redirect_url = substr($response, $pos, strpos($response, "\r\n", $pos)-$pos); return $redirect_url; } } $url = 'http://goo.gl/66VJB'; echo '&lt;ol&gt;'; while(($newurl = follow_redirect($url)) !== false){ echo '&lt;li&gt;', $url, '&lt;/li&gt;'; $url = $newurl; } echo '&lt;/ol&gt;'; echo '&lt;a href="', $url, '"&gt;', $url, '&lt;/a&gt;'; ?&gt; </code></pre> <p>Output:</p> <ol> <li><a href="http://goo.gl/66VJB" rel="nofollow">http://goo.gl/66VJB</a></li> </ol> <p><a href="http://1.bp.blogspot.com/_XE0TDW07Noo/TOSVQXZtgAI/AAAAAAAAELo/aG80jZ7u_fo/s1600/aptitude_test.gif" rel="nofollow">http://1.bp.blogspot.com/_XE0TDW07Noo/TOSVQXZtgAI/AAAAAAAAELo/aG80jZ7u_fo/s1600/aptitude_test.gif</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload