I am developing php application with cURL library.
I would like to get redirect url (in case 301 or 302 http code). In Windows it is simple, I just need to call curl_getinfo($ch). This methods returns associative array with redirect_url, which I am using later.
I have problem when I moved my application to linux server. Such method (curl_getinfo) returns array too, but there is no “redirect_url” index. I though I could try to read headers as a string. I set
curl_setopt(CURLOPT_HEADERFUNCTION, callback)
and save response headers as a string. Then I use simple parser to get fields I am interested in. But now I have another problem. Redirect url (http header LOCATION) returns relative url (whereas in Windows it is absolute).
Why there are differences in windows cURL and linux one? What can I do to make this application resistant to OS changes? And finally, how can I get this field (redirect url) as an absolute url in linux.
Thanks for your help 🙂
Sample from Windows:
Array
(
[url] => http:// wiadomosci.wp.pl/kat,1342,title,Ciag-dalszy-sporu-wokol-poslow-PiS-Nie-moge-tego-zrobic,wid,13927471,wiadomosc.html
[content_type] => text/html
[http_code] => 302
[header_size] => 2407
[request_size] => 384
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.046
[namelookup_time] => 0
[connect_time] => 0.015
[pretransfer_time] => 0.015
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => 0
[upload_content_length] => 0
[starttransfer_time] => 0.046
[redirect_time] => 0
[certinfo] => Array
(
)
[redirect_url] => http:// wiadomosci.wp.pl/kat,1342,title,Ciag-dalszy-sporu-wokol- poslow-PiS-Nie-moge-tego-zrobic,wid,13927471,wiadomosc.html?ticaid=1d436
)
and windows:
Array
(
[url] => http:// wiadomosci.wp.pl/kat,1342,title,Ciag-dalszy-sporu-wokol-poslow-PiS-Nie-moge-tego-zrobic,wid,13927471,wiadomosc.html
[content_type] => text/html
[http_code] => 302
[header_size] => 2425
[request_size] => 384
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.023254
[namelookup_time] => 0.001938
[connect_time] => 0.004836
[pretransfer_time] => 0.004847
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => 0
[upload_content_length] => 0
[starttransfer_time] => 0.023068
[redirect_time] => 0
[certinfo] => Array
(
)
)
There is an alternative solution that will work on every (currently supported) os.
You can ask curl to follow the redirect and limit it to one depth and then use the url key from curl_getinfo($ch);
Example:
If you just want the end url, no matter how many redirects there may be, you can remove the CURLOPT_MAXREDIRS option (or set it high to prevent loops)