How can I get true URL ASAP ? I need to check big number of URLs in a single script (max execution time – 30 sec).
CURL solution takes too much time. I found something like this:
function get_URL($url)
{
$headers = @get_headers($url);
$pattern = '/Location\s*:\s*(https?:[^;\s\n\r]+)/i';
if ($locations = preg_grep($pattern, $headers))
{
preg_match($pattern, end($locations), $redirect);
return $redirect[1];
}
return $url;
}
which seems to works faster. Are there any other, faster ways?
You could use the CURL “multi” feature, which allows you to make multiple requests simultaneously without having to manage multiple processes yourself.
The PHP documentation for curl_multi_init() has some basic examples.
Another tutorial is available here.
Also, if PHP’s max execution time is the only thing stopping you and you don’t actually care if it takes longer than 30 seconds you can always call set_time_limit() on each iteration of your loop.