Main function used in cURL:
- curl_init() function: it initiates a new curl session and returns a cURL handle.
Syntax:curl_init();
- curl_setopt() Function:sets an option for the cURL session identified by with the ch parameter. The parameter indicates which parameter to set, and the value indicates the value for that parameter.
Syntax:curl_setopt ($ch, option, value);
- curl_setopt ($ch, CURLOPT_URL, $url )It passes the url as a parameter, this is the return target url you want to get from the internet.
- curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE) Set to TRUE to return the transmission as a string return value of curl_exec(), rather than output it directly.
- curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, TRUE)Set to TRUE, to follow any "Location:" header that the server sends as part of the HTTP header.
- curl_getinfo ($ch, CURLINFO_EFFECTIVE_URL)This is passed to get the cURL information of the effective URL.
- curl_exec ($ch) Capturesthe URL and passes it to a variable to display the output.
- curl_close ($ch) Closesthe curl resource and frees up system resources sy.
// From the URL to get the redirected URL
$url
=
’ https://engineerforengineer.org/ ’
;
// Initiates a CURL session.
$ch
= curl_init();
// Grab the URL and pass it to a variable.
curl_setopt (
$ch
, CURLOPT_URL,
$url
);
// Catch the output (DO NOT print!)
curl_setopt (
$ch
, CURLOPT_RETURNTRANSFER, TRUE);
// Return the following location true
curl_setopt (
$ch
, CURLOPT_FOLLOWLOCATION, TRUE);
$html
= curl_exec (
$ch
);
// Getinfo or redirected URL from a valid URL
$redirectedUrl
= curl_getinfo (
$ch
, CURLINFO_EFFECTIVE_URL);
// Close descriptor
curl_close (
$ch
);
echo
"Original URL: "
.
$url
.
"
"
;
echo
"Redirected URL:"
.
$redirectedUrl
.
"
"
;
?>
Output:Original URL: https://engineerforengineer.org/ Redirected URL: https://www.engineerforengineer.org/
Command Line Approach: Get redirect url using cURL:
Syntax:curl -Ls -w% {url_effective} -o / dev / null [URL]
Description : - curl- team name
- -s- Quiet Mode
- -L- The location that follows the redirects
- -D -- Dump headers here
- [URL]- URL that redirects
- -o / dev / null- remove additional information about stdout
- -w ’% {url_effective} ’- final destination
Example 1: curl -Ls -w% {url_effective} -o / dev / null https://www.engineerforengineer.org/php-cucrl/
Exit : Follow the cURL redirects:syntax: curl -s -L -D - [URL] -o / dev / null -w ’% {url_effective}’
Example 2: After redirecting to 404 page Error.curl -s -L -D - https://www.engineerforengineer.org/php-cucrl/ -o / dev / null -w ’% {url_effective } ’
Exit :D: mycurlin > curl -s -L -D - https://www.engineerforengineer.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.engineerforengineer.org/php- cucrl / ’
After redirecting to the correct page:
Example 3:curl -s -L - D - https://www.engineerforengineer.org/php-curl/amp/ -o / dev / null -w ’% {url_effective}’
Output:D: mycurlin > curl -s -L -D - https://www.engineerforengineer.org/php-curl/amp/ -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.engineerforengineer.org/php- curl / amp / ’