Created
March 2, 2014 07:00
-
-
Save Nickfost/9302984 to your computer and use it in GitHub Desktop.
Bitly Function
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
//explode $data into a $line and explode line into a $word and check if it's a url then pass it bitly and return that url | |
function bitly($data){ | |
//token | |
$token = ""; // you must add your api token here | |
//break into line by line | |
$linearray = explode("\n",$data); | |
$linei = 0; | |
foreach($linearray as $line){ | |
$wordarray = explode(" ",$line); | |
$wordi = 0; | |
foreach($wordarray as $word){ | |
if(filter_var($word, FILTER_VALIDATE_URL)){ | |
$api = "https://api-ssl.bitly.com/v3/shorten?access_token=".$token."&URI=".$word; | |
$apidata = json_decode(file_get_contents($api), true); | |
if($apidata['status_code']==200){ | |
$newwordarray[$wordi] = "http://bit.ly/".$apidata['data']['hash']; | |
} | |
else{ | |
$newwordarray[$wordi] = $word; | |
//echo "Looks like ".$word." Returned an error ".$apidata['status_txt']."<br /> \n"; | |
} | |
} | |
else{ | |
$newwordarray[$wordi] = $word; | |
} | |
$wordi++; | |
//word | |
} | |
$newlinearray[$linei] = implode(" ", $newwordarray); | |
$linei++; | |
unset($newwordarray); | |
//line | |
} | |
$finaldata = implode("\n",$newlinearray); | |
return $finaldata; | |
//function | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment