0

I'm trying to get a query string in the final URL after redirection.

https://example.com/oauth/auth?user=foo&password=bar

redirects in my browser to:

https://example.com/?secret=8273761838346 

I need the secret={some number} part of the final URL after redirection.

I've tried (and failed) in PHP using file_get_contents, cURL, and even the socket method. cURL returns CURLINFO with the original URL, not the final one. file_get_contents $http_response_header doesn't contain the final URL.

I've tried variations on the options you can set on the cURL call, following the slew of examples available on SO and Google searching, but none of those worked. The URL returned is always the original URL, not the final.

Has anyone ever encountered this and how did you get around it? This is probably a stupid question, but could https be the problem?

@str, here is one of many versions of using cURL that has not yielded the final URL after redirect:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
$header = curl_getinfo($ch);
var_dump($response);
var_dump($header);

I've also tried toggling the HEADER, FOLLOWLOCATION, RETURNTRANSFER, AUTOREFERER, and other options without progress.

Community
  • 1
  • 1
cre8value
  • 295
  • 3
  • 12
  • This is perfectly possible with cURL. Please show your code. – str Mar 01 '13 at 16:26
  • @str added code from one of many attempts. – cre8value Mar 01 '13 at 16:36
  • http://php.net/curl_getinfo - all you need to do is to read. see as well http://www.webmasterworld.com/php/4185134.htm – hakre Mar 01 '13 at 16:55
  • @hakre Thanks but I have actually read those resources several times over and they didn't work, which is why I am posting this question. I keep getting the orig request URL back, not the final one, as I stated in my question. – cre8value Mar 01 '13 at 17:06
  • @ImpudentStrumpet: With file_get_contents this is possible, I know that: [HEAD first with PHP Streams](http://hakre.wordpress.com/2011/09/17/head-first-with-php-streams/) – hakre Mar 01 '13 at 17:08
  • @Gordon Thanks but not sure why you consider it a duplicate. The suggested solutions listed in that link I implemented, but am not able to get the data I am looking for using those methods. – cre8value Mar 01 '13 at 17:15
  • @ImpudentStrumpet can you please clarify please? The suggested solution in the dupe will give you all the redirect targets. All you have to do is run parse_url and parse_str on them afterwards to split the required information from it. If that is not the case, try to explain your redirect scenario better. – Gordon Mar 01 '13 at 17:43
  • @Gordon Yes, I'll try. When I use the solution on that page I do not get the target URLs. Not sure how else to explain, they are not there in the data. I get a response code (200), Date, Server (Apache), Content-type, Connection ('close'), and Set-Cookie which is an array with 2 key-val pairs. There are no URL's. There are no redirect targets. I've tried solutions listed on at least 15 different pages, they all return something similar but lacking the redirect URLs. But when I use a browser to nav to the req URL, I am able to see the final target URL with the query string I am seeking. – cre8value Mar 01 '13 at 18:33

0 Answers0