How can I get a list of URLs that CURL follows when loading a page with a CURLOPT_FOLLOWLOCATION set to True?
I know there is a curl_getinfo($ch, CURLINFO_EFFECTIVE_URL) function, but that returns just the last URL.
How can I get a list of URLs that CURL follows when loading a page with a CURLOPT_FOLLOWLOCATION set to True?
I know there is a curl_getinfo($ch, CURLINFO_EFFECTIVE_URL) function, but that returns just the last URL.
The following code should work for you, which uses the CURLOPT_HEADERFUNCTION
cURL option:
$locations = array();
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_HEADERFUNCTION, function($curl, &$header) use(&$locations) {
$key = 'Location:';
if (strpos($header, $key) === 0) {
$locations[] = trim(substr($header, strlen($key)));
}
return strlen($header);
});
// ...
curl_exec($curl);
$locations
will contain the URLs that cURL was redirected to.