Skip to content

Instantly share code, notes, and snippets.

@Nickfost
Created March 2, 2014 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nickfost/9302984 to your computer and use it in GitHub Desktop.
Save Nickfost/9302984 to your computer and use it in GitHub Desktop.
Bitly Function
//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