6 replies
Not sure if this is possible with PHP but would appreciate any help.

I have list of urls which are all redirects ( i.e. BTLY or TinyURL) in text file.

I would like to get actual url (URL after direct) for each redirect and save in text file with first column being redirect URL (BITLY URL) and second column actual URL.

I can do manually but it is taking too much time. I tried iMacros but some site have pop up and iMacros fails.

Is this possible with PHP or any other tool?

  • Profile picture of the author mbaldwin
    this should be possible.
    First you would want to read the .txt file, and make an array with each of the urls you currently have.
    loop through the array and use cURL to open the urls and follow the redirects, then use a $_SERVER['???'], forgot exactly which one, to get the current url you ended up on.
    built another variable to write to the .txt file when the loop is done.
    $newFile .= $oldUrl.$newUrl.'\n';
    Write that to the new .txt file when done, and it should be good to go.
    I'd write it to a different .txt file to make sure it did it right first before writing over the first one and losing your information.
    {{ DiscussionBoard.errors[8880930].message }}
  • Profile picture of the author NRE
    You can do it in c#

       public string GetFinalRedirectedUrl(string url)
            string result = string.Empty;
            Uri Uris = new Uri(url);
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Uris);
            //req3.Proxy = proxy;
            req.Method = "HEAD";
            req.AllowAutoRedirect = false;
            HttpWebResponse myResp = (HttpWebResponse)req.GetResponse();
            if (myResp.StatusCode == HttpStatusCode.Redirect)
                string temp = myResp.GetResponseHeader("Location");
                //Recursive call
                result = GetFinalRedirectedUrl(temp);
                result = url;
            return result;
    {{ DiscussionBoard.errors[8881031].message }}
  • Profile picture of the author topnichewebsites
    Good response NRE if that code works for him
    http://pixelcovers.com/ <- eBook add eCovers

    https://www.unicommercesolutions.com <- WordPress Websites and Maintenance
    {{ DiscussionBoard.errors[8881213].message }}
    • Profile picture of the author blackjack

      Thanks but I have no experience in c# but this gave idea to do something in PHP.


      thanks for your explaination. I will follow this to see if I can get something going.

      {{ DiscussionBoard.errors[8881737].message }}
      • Profile picture of the author danielml
        Not sure if you got it already, but here ya go:
        shortlinks.txt should have the shortlinks separated by new lines.


        //READ FILE
        $file_contents = file_get_contents( dirname( __FILE__ ) . '/shortlinks.txt' );

        //SEND TO ARRAY
        $shortlink_array = explode( "\n" , $file_contents );
        $new_file_contents = "";

        foreach( $shortlink_array as $shortlink )
        $ch = curl_init();
        curl_setopt ( $ch , CURLOPT_URL , $shortlink );
        curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , 1 );
        curl_setopt ( $ch , CURLOPT_TIMEOUT , 30 );
        curl_setopt( $ch , CURLOPT_FOLLOWLOCATION , true );
        $result = curl_exec( $ch );
        $new_file_contents .= $shortlink . "\t" . curl_getinfo( $ch , CURLINFO_EFFECTIVE_URL ) . "\n";

        file_put_contents( dirname( __FILE__ ) . '/result.txt' , $new_file_contents );
        {{ DiscussionBoard.errors[8882335].message }}

Trending Topics