Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[PHP] Find the Destination of a Redirect with PHP
<?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
You can’t perform that action at this time.