Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created March 26, 2019 19:39
Show Gist options
  • Save tommcfarlin/1e9882520588a5d8ee8948050ce48d69 to your computer and use it in GitHub Desktop.
Save tommcfarlin/1e9882520588a5d8ee8948050ce48d69 to your computer and use it in GitHub Desktop.
[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