-
-
Save tommcfarlin/1e9882520588a5d8ee8948050ce48d69 to your computer and use it in GitHub Desktop.
[PHP] Find the Destination of a Redirect with PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Determines the destination URL of a provided URL that gives a redirect. | |
* | |
* @param string $url the specified URL provided by the API. | |
* @return string $url the destination of the specified URL. | |
*/ | |
private function getDestinationUrl($url) | |
{ | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_HEADER, true); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
// Read the headers as provided by cURL. | |
$headers = curl_exec($ch); | |
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); | |
// We only need the domain so just read the first part. | |
$url = explode('/', $url); | |
$url = $url[0]; | |
return $url; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment