PHP cURL method to get redirect target without following redirect
P粉575055974
P粉575055974 2023-10-25 08:56:01
0
1
784

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粉575055974
P粉575055974

reply all(1)
P粉684720851

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template