bit.lyを利用して短縮URLを取得する
短縮URLをbit.lyを利用して生成
試してみたらあっさり出来たのでソースおいておきます。
用意するブツ
bit.lyに登録後に利用できる Legacy API Keyをここから取得 https://bitly.com/a/settings/advanced
そしてドキュメント
bit.ly shorten url document
APIに対してURLとapiKeyをつけてやると短縮されたURLが取得できる仕組み
https://api-ssl.bitly.com/v3/shorten?login=hoge&apiKey=xxxxxxxx&longUrl=http://xxxxx.xxx/
URLを取得するコード
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// To short URL use bit.ly API | |
// http://dev.bitly.com/links.html#v3_shorten | |
$response = file_get_contents( | |
'https://api-ssl.bitly.com/v3/shorten?login=hoge&apiKey=xxxxxxxx&longUrl=' | |
.urlencode('http://www.laddy.info/') | |
.'&format=txt' | |
); | |
var_dump($response); | |
// result | |
// $ php testrss.php | |
// http://bit.ly/1Mu0u9N | |
// remove &format=txt return JSON format | |
$res_json = file_get_contents( | |
'https://api-ssl.bitly.com/v3/shorten?login=hoge&apiKey=xxxxxxx&longUrl=' | |
.urlencode('http://www.laddy.info/') | |
); | |
var_dump(json_decode($res_json)); | |
/* result | |
object(stdClass)#1 (3) { | |
["status_code"]=> | |
int(200) | |
["status_txt"]=> | |
string(2) "OK" | |
["data"]=> | |
object(stdClass)#2 (5) { | |
["long_url"]=> | |
string(22) "http://www.laddy.info/" | |
["url"]=> | |
string(21) "http://bit.ly/1Mu0u9N" | |
["hash"]=> | |
string(7) "1Mu0u9N" | |
["global_hash"]=> | |
string(7) "1Mu0u9O" | |
["new_hash"]=> | |
int(0) | |
} | |
} | |
*/ |
-
前の記事
Ubuntu14.04でOwnCloudを動かす 2015.01.21
-
次の記事
ubuntuでセキュリティパッケージを自動インストール 2015.03.03