How to find where the URL will redirected using cURL?
Last Updated :
12 Jul, 2019
In General, cURL stands for ‘Client for URLs’, here URL written in uppercase to indicates that the cURL deals with URLs.
PHP Approach:
Basic Function used in cURL:
Parameter with the other important commands:
- curl_setopt($ch, CURLOPT_URL, $url) It pass URL as a parameter, this return target url that you want to get from the internet.
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE) It is set to TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it directly.
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE) It is set to TRUE to follow any “Location: ” header that the server sends as part of the HTTP header.
- curl_getinfo($ch, CURLINFO_EFFECTIVE_URL) It passed to get cURL info of effective URL.
- curl_exec($ch) It grab URL and pass it to the variable for showing output.
- curl_close($ch) It close curl resource, and free up system resources.
Program:
<?php
$ch = curl_init();
curl_setopt( $ch , CURLOPT_URL, $url );
curl_setopt( $ch , CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt( $ch , CURLOPT_FOLLOWLOCATION, TRUE);
$html = curl_exec( $ch );
$redirectedUrl = curl_getinfo( $ch , CURLINFO_EFFECTIVE_URL);
curl_close( $ch );
echo "Original URL: " . $url . "<br/>" ;
echo "Redirected URL: " . $redirectedUrl . "<br/>" ;
?>
|
Output:
Original URL: https://geeksforgeeks.org/
Redirected URL: https://www.geeksforgeeks.org/
Command-Line Approach:
- Get Redirect URL with cURL:
Syntax:
curl -Ls -w %{url_effective} -o /dev/null [URL]
Description:
-
curl — command name
-
-s — Silent mode
-
-L — Location which Follow redirects
-
-D – — Dump headers here
-
[URL] — URL that performs redirection
-
-o /dev/null — remove extra stdout info
-
-w ‘%{url_effective}’ — final destination
Example 1:
curl -Ls -w %{url_effective} -o /dev/null
https://www.geeksforgeeks.org/php-cucrl/
Output:
- Follow the redirects with cURL:
syntax:
curl -s -L -D - [URL] -o /dev/null -w '%{url_effective}'
Example 2: Following redirect to 404 Error page.
curl -s -L -D -
https://www.geeksforgeeks.org/php-cucrl/ -o /dev/null -w
'%{url_effective}'
Output:
D:\mycurl\bin>curl -s -L -D - https://www.geeksforgeeks.org/php-cucrl/
-o /dev/null -w '%{url_effective}'
HTTP/2 404
server: Apache
strict-transport-security: max-age=3600; includeSubDomains
link: ; rel="https://api.w.org/"
access-control-allow-credentials: true
x-frame-options: DENY
x-content-type-options: nosniff
content-type: text/html; charset=UTF-8
cache-control: must-revalidate, max-age=3, s-maxage=21600
date: Mon, 08 Jul 2019 01:34:28 GMT
'https://www.geeksforgeeks.org/php-cucrl/'
- Followed redirect to correct page:
Example 3:
curl -s -L -D -
https://www.geeksforgeeks.org/php-curl/ -o /dev/null -w
'%{url_effective}'
Output:
D:\mycurl\bin>curl -s -L -D - https://www.geeksforgeeks.org/php-curl/
-o /dev/null -w '%{url_effective}'
HTTP/2 200
server: Apache
strict-transport-security: max-age=3600; includeSubDomains
access-control-allow-credentials: true
x-frame-options: DENY
x-content-type-options: nosniff
content-type: text/html; charset=UTF-8
cache-control: must-revalidate, max-age=3, s-maxage=21600
date: Mon, 08 Jul 2019 01:34:55 GMT
'https://www.geeksforgeeks.org/php-curl/'
Share your thoughts in the comments
Please Login to comment...