[Updated below]
Hello.
Start with short urls:
Imagine you have a collection of 5 short URLs (e.g. http://bit.ly ) in a php array, for example:
$shortUrlArray = array("http://bit.ly/123", "http://bit.ly/123", "http://bit.ly/123", "http://bit.ly/123", "http://bit.ly/123");
Finish with final, redirected URLs:
How can I get the final url of these short urls using php? Like this:
http://www.example.com/some-directory/some-page.html
http://www.example.com/some-directory/some-page.html
http://www.example.com/some-directory/some-page.html
http://www.example.com/some-directory/some-page.html
http://www.example.com/some-directory/some-page.html
I have one method (found online) that works well with a single URL, but when navigating to multiple URLs, it only works with the final URL in the array. For your reference, the method is as follows:
function get_web_page( $url ) { $options = array( CURLOPT_RETURNTRANSFER => true, // return web page CURLOPT_HEADER => true, // return headers CURLOPT_FOLLOWLOCATION => true, // follow redirects CURLOPT_ENCODING => "", // handle all encodings CURLOPT_USERAGENT => "spider", // who am i CURLOPT_AUTOREFERER => true, // set referer on redirect CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect CURLOPT_TIMEOUT => 120, // timeout on response CURLOPT_MAXREDIRS => 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 <= $lineCount; $i++){ $singleShortURL = $shortUrlArray[$i]; $myUrlInfo = get_web_page( $singleShortURL ); $rawURL = $myUrlInfo["url"]; array_push($finalURLs, $rawURL); }
Close but not enough
This method works, but with only one URL. I cannot use it in a for loop that I want to do. When used in the for example in the above example, the first four elements are returned unchanged, and only the final element is converted to its final url. This happens if your array consists of 5 elements or 500 elements.
Decision:
Please give me a hint on how to change this method to work inside a for loop with a set of URLs (not just one).
-OR -
If you know the code that is best suited for this task, include it in your answer.
Thanks in advance.
Update:
After some further pushing, I found that the problem is not with the method above (which, after all, seems to work fine for loops), but maybe it encodes. When I hard code an array of short URLs, the loop works fine. But when I go to the block of new URLs from the html form using GET or POST, the above problem arises. Are the URLs somehow changed to a method incompatible format when I submit the form?
New update:
You guys, I found that my problem is related to something not related to the above method. My problem was that the URL encoding of my short URLs converts what I thought was only newline characters (separating the URLs):% 0D% 0A, which is a string of string or return character ... And what all short URLs are saved for the final url in the collection had a ghostly character attached to the tail, making it impossible to get the final URLs only for them. I identified the ghost, fixed my php explosion, and now everything works fine. Sorry and thank you.