Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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