How to Get the Destination Url Using Curl

Get the destination URL only without loading the contents with cURL

For clarity i'll add it as an answer as well.

You can tell curl to only retrieve the headers by setting the CURLOPT_NOBODY to true.

curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_HEADER, true);

From these headers you can parse the location part to get the redirected URL.

Edit for potential future readers: CURLOPT_HEADER will also need to be set to true, i had left this out as you already had it included in your code.

cURL , get redirect url to a variable

You would use

curl_setopt($CURL, CURLOPT_HEADER, TRUE);

And parse the headers for the location header

Get final URL after curl is redirected

curl's -w option and the sub variable url_effective is what you are
looking for.

Something like

curl -Ls -o /dev/null -w %{url_effective} http://google.com

More info


-L Follow redirects
-s Silent mode. Don't output anything
-o FILE Write output to <file> instead of stdout
-w FORMAT What to output after completion

More

You might want to add -I (that is an uppercase i) as well, which will make the command not download any "body", but it then also uses the HEAD method, which is not what the question included and risk changing what the server does. Sometimes servers don't respond well to HEAD even when they respond fine to GET.

python how to get the final destination after redirections

The final url is set on the response object:

In [5]: import requests 
...:
...: r = requests.get("https://ludik.xyz/music")

In [8]: r.url
Out[8]: 'https://ludik.herokuapp.com/#/'

Is it possible to get the origin URL when using a CURL multi execute in PHP with follow location?

You need to ask CURL to return the headers using CURLOPT_RETURNTRANSFER and look for the redirect instruction yourself. This is described here:

http://zzz.rezo.net/HowTo-Expand-Short-URLs.html

PHP - Using cURL to get the final URL status after redirect

You're looking for
CURLOPT_FOLLOWLOCATION


TRUE to follow any "Location: " header that the server sends as part of the HTTP header (note this is recursive, PHP will follow as many "Location: " headers that it is sent, unless CURLOPT_MAXREDIRS is set).

from: http://docs.php.net/manual/da/function.curl-setopt.php

If you don't plan to use the option CURLOPT_FOLLOWLOCATION then you must make sure you're analyzign the headers correctly to get the status.
From http://php.net/manual/en/function.curl-getinfo.php you can see

CURLINFO_HTTP_CODE - The last response code.(...)

that means: there can be more than one status code.
i.e with http://airbrake.io/login there are two sent:

HTTP/1.1 301 Moved Permanently
(...)

HTTP/1.1 200 OK
(...)

That means, only 200 is going to be returned, and if you want to get ANY result, your function needs to look like:

 if($httpStatus >= 300 && $httpStatus < 400) {
return getUrlStatus($redirectURL);
} else {
return $httpStatus;
}


Related Topics



Leave a reply



Submit