I'm currently using cURL to try and get the URL from a redirect for a website scraper. I only need the url from the website. I've researched on stackoverflow and other sites for the past couple days and have been unsuccessful. The code I'm currently using is from this website:
$url = "http://www.someredirect.com";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_NOBODY, true);
$response = curl_exec($ch);
preg_match_all('/^Location:(.*)$/mi', $response, $matches);
curl_close($ch);
echo !empty($matches[1]) ? trim($matches[1][0]) : 'No redirect found';
Any help would be greatly appreciated!
Try using this code:
}
In your particular case, the server is checking for certain user-agent strings.
When a server checks the user-agent string, it will only respond with a
302
redirect status code when the server sees a "valid" (according to the server) user-agent. Any "invalid" user-agents will not receive the302
redirect status code response orLocation:
header.In your particular case, when the server receives a request from an "invalid" user-agent it responds with a
200
OK status code with no text in the response body.(Note: in the code below, the actual URLs provided have been replaced with examples.)
Let's say that
http://www.example.com
's server checks the User-Agent string and thathttp://www.example.com/product/123/
redirects tohttp://www.example.org/abc
.In PHP your solution would be:
And, the output of this script would be:
http://www.example.org/abc
.