Skip to content

Instantly share code, notes, and snippets.

@adamcrampton
Last active February 5, 2019 23:10
Show Gist options
  • Save adamcrampton/f3425f042e53c729dbb3c263e1c63b70 to your computer and use it in GitHub Desktop.
Save adamcrampton/f3425f042e53c729dbb3c263e1c63b70 to your computer and use it in GitHub Desktop.
Return the forwarded URL using cURL - useful for link shorteners etc
<?php
function returnRedirectedUrl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$a = curl_exec($ch);
if(preg_match('#Location: (.*)#', $a, $r))
$permalink = trim($r[1]);
return $permalink;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment