search

Home  >  Q&A  >  body text

PHP cURL method to get redirect target without following redirect

The curl_getinfo function returns a large amount of metadata about the result of the HTTP request. However, for some reason it doesn't contain the information I currently want, which is the target URL if the request returns an HTTP redirect code.

I did not use CURLOPT_FOLLOWLOCATION because I wanted to handle specific redirect codes as special cases.

If cURL can track redirects, why can't it tell me what they redirect to when it doesn't?

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

P粉575055974P粉575055974396 days ago791

reply all(1)I'll reply

  • P粉684720851

    P粉6847208512023-10-26 00:44:26

    You can simply use this: (CURLINFO_REDIRECT_URL)

    $info = curl_getinfo($ch, CURLINFO_REDIRECT_URL);
    echo $info; // the redirect URL without following it

    As you mentioned, disable CURLOPT_FOLLOWLOCATION option (before execution) and place my code after execution.

    refer to

    reply
    0
  • Cancelreply