/* Example code */
$link = "http://www.stackoverflow.com";
print getSmallLink($link);
function getSmallLink($longurl){
// Bit.ly
$url = "http://api.bit.ly/shorten?version=2.0.1&longUrl=$longurl&login=YOURLOGIN&apiKey=YOURAPIKEY&format=json&history=1";
$s = curl_init();
curl_setopt($s,CURLOPT_URL, $url);
curl_setopt($s,CURLOPT_HEADER,false);
curl_setopt($s,CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($s);
curl_close( $s );
$obj = json_decode($result, true);
return $obj["results"]["$longurl"]["shortUrl"];
}
Run Code Online (Sandbox Code Playgroud)
从Ceejayoz的例子开始,你可以把它变成一个班轮!
$short_url = json_decode(file_get_contents("http://api.bit.ly/v3/shorten?login=bitlyusername&apiKey=bitlyapikey&longUrl=".urlencode("http://example.com")."&format=json"))->data->url;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7527 次 |
| 最近记录: |