-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax_bitly.php
26 lines (24 loc) · 1004 Bytes
/
ajax_bitly.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
function make_bitly_url($url,$login,$appkey,$format = 'xml',$history=1, $version = '2.0.1')
{
// Modified function originally from http://davidwalsh.name/bitly-php
//create the URL
$bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format='.$format.'&history='.$history;
//get the url
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $bitly . "?" . $param);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
//parse depending on desired format
if(strtolower($format) == 'json') {
$json = @json_decode($response,true);
return $json['results'][$url]['shortUrl'];
} else {
$xml = simplexml_load_string($response);
return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
}
} // end function make_bitly_url
header('Content-Type: application/json; charset=utf8');
print json_encode(make_bitly_url($_POST['url'],'YOUR_BITLY_USERNAME','YOUR_BITLY_API_KEY','json'));
?>