php

PHP 短縮URLのAPIから短縮後のURLを取得

twitterに関連したwebサービスを作っているとURL短縮の必要が出てくることが多々あります。
そんな時には、有名なurl短縮サービスを利用することになると思うのですが、代表的なbit.ly、TinyURLを利用する方法です。

bit.ly

bit.lyのURL短縮APIを使うには、アカウント登録して、APIキーを取得剃る必要があります。
アカウント登録はここ

[php]$long_url = "http://your-long-url";
$req = "http://api.bit.ly/shorten?login=yourAcountName&apiKey=yourApiKey&version=2.0.1&longUrl=".$posted_url;
$contents = file_get_contents($req);

if(isset($contents)) {
$url = json_decode($contents, true);
}
$shorten_url = $url[‘results’][$posted_url][‘shortUrl’];[/php]

TinyURL

TinyURLの場合は、アカウント登録なしに利用できる?みたいです。

[php]function getTinyUrl($url) {
return file_get_contents("http://tinyurl.com/api-create.php?url=".$url);
}
$long_url = "http://your-long-url";
$shorten_url = getTinyUrl($long_url);

[/php]

limo service new york
пластиковые окна в минске