Oké omdat ik zelf een niet al te kort domein heb en toch graag wil dat mensen dingen van mij tweeten, leek mij het handig om gebruik te maken van bit.ly. Hier is de functie die ik heb geproduceerd om het voor elkaar te krijgen:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?php
define('bitly_user',''); #Jouw bit.ly username
define('bitly_API',''); #Jouw bit.ly api code: http://bit.ly/a/your_api_key
function get_small_url($url){
$host = "http://api.bit.ly/v3/shorten?login=".urlencode(bitly_user)."&apiKey=".urlencode(bitly_API)."&longUrl=".urlencode($url)."&format=json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
$decoded_result = json_decode($result);
if($decoded_result->status_code == '200'){
$return['hash'] = $decoded_result->data->hash;//only slash bit.ly hash
$return['url'] = $decoded_result->data->url;//full bit.ly url
$return['new'] = $decoded_result->data->new_hash; //1 = new || 0 = existing
return $return;
} else {
throw new Exception('HTTP error code ' . $decoded_result->status_code . ': ' . $decoded_result->status_txt, 0);
}
}
?>
|
|
|
Gebruik:
1
2
3
4
5
6
7
8
9
10
11
|
try {
$small_url = get_small_url('http://www.google.nl/');
echo 'Hash:'.$small_url['hash'];
echo '<br />Full url:'.$small_url['url'];
echo '<br />Is new?'.$small_url['new'];
} catch (Exception $e) { // er gaat iets fout
echo 'Error: '.$e -> getMessage();
//
}
|
|
|
Feedback is welkom.
Have fun!