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:
- curl_init() Function: It will initiate the curl a new session and return a cURL handle.
Syntax:curl_init();
- curl_setopt() Function: It sets an option for a cURL session identified by the ch parameter. Option specifies which option is to set, and value specifies the value for the given option.
Syntax:curl_setopt($ch, option, value);
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 // From URL to get redirected URL // Initialize a CURL session. $ch = curl_init(); // Grab URL and pass it to the variable. curl_setopt( $ch , CURLOPT_URL, $url ); // Catch output (do NOT print!) curl_setopt( $ch , CURLOPT_RETURNTRANSFER, TRUE); // Return follow location true curl_setopt( $ch , CURLOPT_FOLLOWLOCATION, TRUE); $html = curl_exec( $ch ); // Getinfo or redirected URL from effective URL $redirectedUrl = curl_getinfo( $ch , CURLINFO_EFFECTIVE_URL); // Close handle 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/'