How to get lastest redirect url in php using curl -
i using php , curl redirect url, have got output, shows me error 302 not found @ last redirect(i have redirect url twice), here php code,
<?php $url = 'http://www.example.com/kdjf/ksdjfk/file'; function getred($url){ $ch = curl_init($url); curl_exec($ch); if (!curl_errno($ch)) { $info = curl_getinfo($ch); return $info['redirect_url']; } curl_close($ch); } $dds=getred($url); echo getred($dds); ?>
$dds gives me next redirection after example.com passed , have use redirect function again, , have got error,
<html> <head> <title>302 found</title> </head> <body bgcolor="white"> <center><h1>302 found</h1></center> <hr><center>nginx</center> </body> </html> http://www.example.org/skdfjk/new_file.html
how http://www.example.org/skdfjk/new_file.html
without error. or html tags.
i have used 2 function thing, work me.
function getredirecturlss($url){ $redirect_url = null; $url_parts = @parse_url($url); if (!$url_parts) return false; if (!isset($url_parts['host'])) return false; //can't process relative urls if (!isset($url_parts['path'])) $url_parts['path'] = '/'; $sock = fsockopen($url_parts['host'], (isset($url_parts['port']) ? (int)$url_parts['port'] : 80), $errno, $errstr, 30); if (!$sock) return false; $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 = ''; while(!feof($sock)) $response .= fread($sock, 8192); fclose($sock); if (preg_match('/^location: (.+?)$/m', $response, $matches)){ if ( substr($matches[1], 0, 1) == "/" ) return $url_parts['scheme'] . "://" . $url_parts['host'] . trim($matches[1]); else return trim($matches[1]); } else { return false; } } function getred($url){ $ch = curl_init($url); curl_exec($ch); if (!curl_errno($ch)) { $info = curl_getinfo($ch); return $info['redirect_url']; } curl_close($ch); } $dds=getred($url); $urls= getredirecturlss($dds); echo $urls;
Comments
Post a Comment