$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!
When the server checks the user agent string, only when the server sees the "effective" (according to the server) the user agent, it will redirect response 302 status code. Any "inactive" user agent 302 does not receive the redirect status response codes or Location: header.
In your particular case, when the server receives from the "invalid" requesting user agent, it responds 200 OK status code, and no response in the body text.
(Note: In the following code, the actual URL has been provided alternative example.)
Suppose http://www.example.com server checks the user agent string, and to redirect http://www.example.com/product/123/ http://www.example.org/abc.
In PHP, your solution is:
<?php $url = 'http://www.example.com/product/123/'; $ch = curl_init($url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux x86_64; rv:21.0) Gecko/20100101 Firefox/21.0"); // Necessary. The server checks for a valid User-Agent. curl_exec($ch); $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';
The output of this script will be: http: //www.example.org/abc.