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
JSON_Encode Not Working with a HTML String as Value
How to Check If a MySQL Query Using the Legacy API Was Successful
How to Redefine a Type Hint to a Descendant Class When Extending an Abstract Class
How to Call a Static Method from a Class If All I Have Is a String of the Class Name
PHP - Detect Whitespace Between Strings
PHP Spl_Autoload_Register VS _Autoload
How to Redirect to Different Url Based on Roles in Symfony 2
What's the Best Way to Localise a Date on Laravel
How to Enable Ssl in Wamp Server
How to Use PHP In_Array with Associative Array
Unexpected 'Use' (T_Use) When Trying to Use Composer
Using Curl in PHP with Client Certificate and Private Key in Separate Files
Mp4 Plays When Accessed Directly, But Not When Read Through PHP, on iOS
Efficient Way to Pass Variables from PHP to JavaScript
Native PHP Support in Visual Studio 2010
How to Redirect a 404 Error in a Custom 404 Page Using Codeigniter