0

I am using php and curl to get redirect url, I have got the output, but it shows me an error 302 not found at the last redirect(I have to redirect the url twice), Here is my 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 and then I have use the redirect function again, And I have got an 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 to get only http://www.example.org/skdfjk/new_file.html without any error. or html tags.

Maths RkBala
  • 2,207
  • 3
  • 18
  • 21
  • 1
    Try looking here: http://stackoverflow.com/questions/15025875/what-is-the-best-way-in-php-to-read-last-lines-from-a-file – Yaron Jul 12 '16 at 13:37
  • I can get the last line its easy, but I want it without an error. – Aditya kumar Pandey Jul 12 '16 at 13:39
  • curl either follows the entire redirect chain, or it doesn't follow it at all. if you awnt all the intermediate redirections, you'll have to do it in a loop. `get url, extract redirect, get new url, extract another redirect, etc...` – Marc B Jul 12 '16 at 13:53
  • Use `CURLOPT_FOLLOWLOCATION` ? – drew010 Jul 12 '16 at 17:29

2 Answers2

0

I have used two function to do this thing, its work for 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;
Shawon
  • 302
  • 3
  • 15
0

Use ob_clean(), ob_end_clean(), and ob_get_clean(). function before echo your output

Shahid Latif
  • 135
  • 1
  • 6