title: Getting final urls of shortened urls (like bit.ly) using php
body: <p><em>[Updated At Bottom]</em><br />
  Hi everyone. </p>
  
  <p><strong>Start With Short URLs:</strong><br />
  Imagine that you've got a collection of 5 short urls (like <a href="http://bit.ly" rel="nofollow">http://bit.ly</a>) in a php array, like this:</p>
  
  <pre><code>$shortUrlArray = array("http://bit.ly/123",
  "http://bit.ly/123",
  "http://bit.ly/123",
  "http://bit.ly/123",
  "http://bit.ly/123");
  </code></pre>
  
  <p><strong>End with Final, Redirected URLs:</strong><br />
  How can I get the final url of these short urls with php? Like this:</p>
  
  <p><a href="http://www.example.com/some-directory/some-page.html" rel="nofollow">http://www.example.com/some-directory/some-page.html</a><br />
  <a href="http://www.example.com/some-directory/some-page.html" rel="nofollow">http://www.example.com/some-directory/some-page.html</a><br />
  <a href="http://www.example.com/some-directory/some-page.html" rel="nofollow">http://www.example.com/some-directory/some-page.html</a><br />
  <a href="http://www.example.com/some-directory/some-page.html" rel="nofollow">http://www.example.com/some-directory/some-page.html</a><br />
  <a href="http://www.example.com/some-directory/some-page.html" rel="nofollow">http://www.example.com/some-directory/some-page.html</a><br /></p>
  
  <p>I have one method (found online) that works well with a single url, but when looping over multiple urls, it only works with the final url in the array. For your reference, the method is this:</p>
  
  <pre><code>function get_web_page( $url ) 
  { 
      $options = array( 
          CURLOPT_RETURNTRANSFER =&gt; true,     // return web page 
          CURLOPT_HEADER         =&gt; true,    // return headers 
          CURLOPT_FOLLOWLOCATION =&gt; true,     // follow redirects 
          CURLOPT_ENCODING       =&gt; "",       // handle all encodings 
          CURLOPT_USERAGENT      =&gt; "spider", // who am i 
          CURLOPT_AUTOREFERER    =&gt; true,     // set referer on redirect 
          CURLOPT_CONNECTTIMEOUT =&gt; 120,      // timeout on connect 
          CURLOPT_TIMEOUT        =&gt; 120,      // timeout on response 
          CURLOPT_MAXREDIRS      =&gt; 10,       // stop after 10 redirects 
      ); 
  
      $ch      = curl_init( $url ); 
      curl_setopt_array( $ch, $options ); 
      $content = curl_exec( $ch ); 
      $err     = curl_errno( $ch ); 
      $errmsg  = curl_error( $ch ); 
      $header  = curl_getinfo( $ch ); 
      curl_close( $ch ); 
  
      //$header['errno']   = $err; 
      //$header['errmsg']  = $errmsg; 
      //$header['content'] = $content; 
      print($header[0]); 
      return $header; 
  }  
  
  
  //Using the above method in a for loop
  
  $finalURLs = array();
  
  $lineCount = count($shortUrlArray);
  
  for($i = 0; $i &lt;= $lineCount; $i++){
  
      $singleShortURL = $shortUrlArray[$i];
  
      $myUrlInfo = get_web_page( $singleShortURL ); 
  
      $rawURL = $myUrlInfo["url"];
  
      array_push($finalURLs, $rawURL);
  
  }
  </code></pre>
  
  <p><strong>Close, but not enough</strong><br />
  This method works, but only with a single url. I Can't use it in a for loop which is what I want to do. When used in the above example in a for loop, the first four elements come back unchanged, and only the final element is converted into its final url. This happens whether your array is 5 elements or 500 elements long. </p>
  
  <p><strong>Solution Sought:</strong><br />
  Please give me a hint as to how you'd modify this method to work when used inside of a for loop with collection of urls (Rather than just one). </p>
  
  <p>-OR-</p>
  
  <p>If you know of code that is better suited for this task, please include it in your answer. </p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>Update:</strong><br />
  After some further prodding I've found that the problem lies not in the above method (which, after all, seems to work fine in for loops) but possibly encoding. When I hard-code an array of short urls, the loop works fine. But when I pass in a block of newline-seperated urls from an html form using GET or POST, the above mentioned problem ensues. Are the urls somehow being changed into a format not compatible with the method when I submit the form????</p>
  
  <p><strong>New Update:</strong><br />
  You guys, I've found that my problem was due to something unrelated to the above method. My problem was that the URL encoding of my short urls converted what i thought were just newline characters (separating the urls) into this: %0D%0A which is a line feed or return character... And that all short urls save for the final url in the collection had a "ghost" character appended to the tail, thus making it impossible to retrieve the final urls for those only. I identified the ghost character, corrected my php explode, and all works fine now. Sorry and thanks.</p>