Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP cURL: Get target of redirect, without following it

Tags:

php

curl

The curl_getinfo function returns a lot of metadata about the result of an HTTP request. However, for some reason it doesn't include the bit of information I want at the moment, which is the target URL if the request returns an HTTP redirection code.

I'm not using CURLOPT_FOLLOWLOCATION because I want to handle specific redirect codes as special cases.

If cURL can follow redirects, why can't it tell me what they redirect to when it isn't following them?

Of course, I could set the CURLOPT_HEADER flag and pick out the Location header. But is there a more efficient way?

like image 578
Stewart Avatar asked Feb 23 '11 12:02

Stewart


People also ask

Does cURL automatically follow redirects?

By default, Curl does not follow redirects and displays the content of the 300x page (if any). To follow redirects with Curl, you need to use the -L or --location command-line option.

Do not follow redirects cURL?

In curl's tradition of only doing the basics unless you tell it differently, it does not follow HTTP redirects by default. Use the -L, --location option to tell it to do that. When following redirects is enabled, curl will follow up to 50 redirects by default.

How can I get redirected URL in PHP?

Answer: Use the PHP header() Function You can simply use the PHP header() function to redirect a user to a different page. The PHP code in the following example will redirect the user from the page in which it is placed to the URL http://www.example.com/another-page.php . You can also specify relative URLs.

What is Curl_exec?

curl_exec(CurlHandle $handle ): string|bool. Execute the given cURL session. This function should be called after initializing a cURL session and all the options for the session are set.


1 Answers

This can be done in 4 steps:

Step 1. Initialise curl

curl_init($ch); //initialise the curl handle
//COOKIESESSION is optional, use if you want to keep cookies in memory
curl_setopt($this->ch, CURLOPT_COOKIESESSION, true);

Step 2. Get the headers for $url

curl_setopt($ch, CURLOPT_URL, $url); //specify your URL
curl_setopt($ch, CURLOPT_HEADER, true); //include headers in http data
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); //don't follow redirects
$http_data = curl_exec($ch); //hit the $url
$curl_info = curl_getinfo($ch);
$headers = substr($http_data, 0, $curl_info['header_size']); //split out header

Step 3. Check if you have the correct response code

if (!($curl_info['http_code']>299 && $curl_info['http_code']<309)) {
  //return, echo, die, whatever you like
  return 'Error - http code'.$curl_info['http_code'].' received.';
}

Step 4. Parse the headers to get the new URL

preg_match("!\r\n(?:Location|URI): *(.*?) *\r\n!", $headers, $matches);
$url = $matches[1];

Once you have the new URL you can then repeat steps 2-4 as often as you like.

like image 66
Nathan Dunn Avatar answered Nov 15 '22 16:11

Nathan Dunn