ก่อนอื่นต้องสมัครสมาชิก bitly.com ก่อนเพื่อนำ API Key มาใช้ สามารถเข้าไปที่
http://bitly.com/a/your_api_key/ เพื่อดู Key ที่เราจะนำไปใช้
สมมุติว่า
Username : realperson
Key : R_1258586555555
จากนั้นมาเขียน JSON กันซึ่ง jQuery นั้น Integrate JSON เข้าไปอยู่แล้ว วิธีเรียก
$.getJSON(
"http://api.bitly.com/v3/shorten?callback=?",
{
"format": "json",
"apiKey": R_1258586555555,
"login": realperson,
"longUrl": long_url
},
function(response)
{
alert(response.data.url);
}
);
"http://api.bitly.com/v3/shorten?callback=?",
{
"format": "json",
"apiKey": R_1258586555555,
"login": realperson,
"longUrl": long_url
},
function(response)
{
alert(response.data.url);
}
);
จากนัันสามารถนำค่า response.data.url ไปใช้ต่อได้เลย
บทความของคุณดีมาก ลองหลายตัวอย่างแล้ว สามารถเอาไปใช้ได้จริง
ตอบลบขอบคุณมากครับ เป็นกำลังใจเขียนบทความดี ๆ ต่อไป