function make_bitly_url($url,$login,$appkey,$format = 'xml',$version = '2.0.1') {
$sql = "SELECT * FROM `url_cache` WHERE `before`='".mysql_real_escape_string($url)."' LIMIT 1";
$check = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($check)==1) {//we found it in cache
$url = mysql_fetch_assoc($check);
return $url['after'];
}
//create the URL
$bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format='.$format;
//get the url
//could also use cURL here
$response = file_get_contents($bitly);
//parse depending on desired format
if(strtolower($format) == 'json') {
$json = @json_decode($response,true);
$sql = "INSERT INTO `url_cache` SET `before`='".mysql_real_escape_string($url)."', `after`='".mysql_real_escape_string($json['results'][$url]['shortUrl'])."'";
mysql_query($sql) or die(mysql_error());
return $json['results'][$url]['shortUrl'];
}
else //xml
{
$xml = simplexml_load_string($response);
$sql = "INSERT INTO `url_cache` SET `before`='".mysql_real_escape_string($url)."', `after`='".mysql_real_escape_string('http://bit.ly/'.$xml->results->nodeKeyVal->hash)."'";
mysql_query($sql) or die(mysql_error());
return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
}
}
$url = $mosConfig_live_site."/plugins/content/sige/plugin_sige/socialshare.php?img=".$mosConfig_live_site.$rootfolder.$_images_dir_."/Midsize/".$images[$a]['filename']."&name=".$imagename;
$u = make_bitly_url($url,'badwolfff','R_f650e553b78bb0569fc67dbc16a5de7c','json');
$html .= '<a href="http://www.facebook.com/sharer.php?u='.$u.'&t=Zuenelli - '.$imagename.'" title="aggiungi a facebook" target="_blank"><img src="'.$mosConfig_live_site.'/plugins/content/sige/plugin_sige/facebook.png" /></a><a href="http://twitter.com/?status=Zuenelli propone - '.$imagename.' (cliccare sul seguente link per scoprire): '.$u.'" title="pubblica su twitter" target="_blank"><img src="'.mosConfig_live_site.'/plugins/content/sige/plugin_sige/twitter.png" /></a>
<?php
$baseurl = 'http://localhost/'; //address to use as the first part of the short URLs (note trailing slash)
$sql_host = 'localhost'; //host of your mysql database (usually localhost)
$sql_user = 'root'; //mysql user with access to your database
$sql_pass = 'root'; //password for this user
$sql_db = 'turl'; //the name of your database
$db = mysql_connect($sql_host, $sql_user, $sql_pass);
mysql_select_db($sql_db, $db);
if(isset($_GET['k'])) {
$k = mysql_real_escape_string($_GET['k'], $db);
if($result = mysql_query("SELECT `url` FROM `turl` WHERE `key` = '" . $k . "'", $db)) {
if(mysql_num_rows($result) > 0) {
$row = mysql_fetch_row($result);
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $row[0]);
exit;
}
}
}
if(isset($_POST['submit'], $_POST['url'])) {
if(filter_var($_POST['url'], FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED) !== false) {
$url = mysql_real_escape_string($_POST['url'], $db);
if($result = mysql_query("SELECT `key` FROM `turl` WHERE `url` = '" . $url . "'", $db)) {
if(mysql_num_rows($result) > 0) {
$row = mysql_fetch_row($result);
$key = $row[0];
}
}
if(!isset($key) && mysql_query("INSERT INTO `turl` (`url`) VALUES ('" . $url . "')", $db)) {
$id = mysql_insert_id($db);
$key = base_convert($id, 10, 36);
mysql_query("UPDATE `turl` SET `key` = '" . $key . "' WHERE `id` = '" . $id . "'");
}
echo '<a href="' . $baseurl . $key . '" target="_blank">' . $baseurl . $key . '</a>';
}
}
else {
?>
<form method="post" action="short.php">
<label>URL: <input type="text" name="url" id="url" /></label>
<br />
<input type="submit" name="submit" id="submit" value="Shorten" />
</form>
<?php
}
?>
RewriteEngine On
RewriteCond %{REQUEST_URI} \/([0-9a-z]+)$ [NC]
RewriteRule ^(.*) short.php?k=%1 [L]
CREATE TABLE `turl` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`key` VARCHAR(32) DEFAULT NULL,
`url` text NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `key` (`key`)
);
Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.
When asked, what has been your best career decision?
Deciding to stick with EE.
Being involved with EE helped me to grow personally and professionally.
Connect with Certified Experts to gain insight and support on specific technology challenges including:
We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE