Reputation: 435
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.
Upvotes: 0
Views: 585
Reputation:
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.
Upvotes: 2
Reputation: 71414
You should be able to use CURLOPT_HEADER
options and inspect the headers that were returned. Unfortunately this would require parsing out the location headers, as I don't know of a more direct way of getting the specific headers values out of returned content.
You might be just as well suited to turn off the option to follow redirects and manually make follow up cURL requests to follow the redirections, logging the redirection information along the way.
Upvotes: 0