PHP Help

by 6 replies
8
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?

Thanks
#programming #php
  • 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.
    • [ 1 ] Thanks
  • You can do it in c#

    Code:
       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);
            }
            else
            {
                result = url;
            }
    
            return result;
        }
    • [ 2 ] Thanks
  • Good response NRE if that code works for him
    • [ 1 ] Thanks
    • [1] reply
    • NRE


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

      Mbaldwin

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

      Thanks
      • [1] reply

Next Topics on Trending Feed

  • 8

    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.