การย่อ URL ให้สั้น ด้วย php กับ to.ly
หน้าแรก PHP MySQL เกร็ดความรู้ การย่อ URL ให้สั้น ด้วย php กับ to.ly
API Info
Simply have your application access a URL similar to the following, via an HTTP GET request: http://to.ly/api.php?longurl=http://www.example.com
cURL Command Line Example
curl -d "longurl={YOUR LINK HERE}" http://to.ly/api.phpReturns the shortened link on a single line. Replace {YOUR LINK HERE} with your link... please omit curly brackets!
JSON/jQuery Example
function CompressURL(url,success)
{
var link = 'http://to.ly/api.php?json=1&longurl=' + encodeURIComponent(url) + '&callback=?';
$.getJSON(link, function(data){
success && success(data.shorturl);
});
}
CompressURL("http://www.twitter.com/jonasl", function(shorturl) {
alert(shorturl);
});
PHP Source Code Example
This example requires cURL (ubuntu/linux package name is php5-curl)function CompressURL($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://to.ly/api.php?longurl=".urlencode($url));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$shorturl = curl_exec ($ch);
curl_close ($ch);
return $shorturl;
}
echo CompressURL("http://twitter.com"); // Test
Python Source Code Example
import urllib
def CompressURL(url):
"""Compress the URL using to.ly"""
apiurl = "http://to.ly/api.php?longurl="
quoted = urllib.quote_plus(url)
shorturl = urllib.urlopen(apiurl + quoted).read()
return shorturl
print CompressURL("http://www.twitter.com")
© Triop AB
refer: http://to.ly/api_info.php
ขึ้นไปด้านบน

