bit.ly 短地址转换_使用PHP创建Bit.ly短URL:API版本3

本文介绍了如何使用PHP与Bit.ly的API版本3创建和扩展短URL。通过提供appkey、login(需注册API服务)、长URL和期望的返回格式(如txt、XML或JSON),可以轻松实现URL缩短。获取XML或JSON格式将提供更多关于URL的详细信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

bit.ly 短地址转换

Bit.ly is a great URL shortening service. I love their reliability, shortness of the URL, and the information they provide about a given URL. Recently Bit.ly updated their API to version 3 so I thought I'd update my original Bit.ly post. Here's how you can create short URLs and expand short URLs using Bit.ly.

Bit.ly是一个很棒的URL缩短服务。 我喜欢它们的可靠性,URL的简短性以及它们提供的有关给定URL的信息。 最近Bit.ly将他们的API更新到了版本3,所以我想我应该更新我的原始Bit.ly帖子 。 这是使用Bit.ly创建短URL并扩展短URL的方法。

PHP (The PHP)


/* returns the shortened url */
function get_bitly_short_url($url,$login,$appkey,$format='txt') {
	$connectURL = 'http://api.bit.ly/v3/shorten?login='.$login.'&apiKey='.$appkey.'&uri='.urlencode($url).'&format='.$format;
	return curl_get_result($connectURL);
}

/* returns expanded url */
function get_bitly_long_url($url,$login,$appkey,$format='txt') {
	$connectURL = 'http://api.bit.ly/v3/expand?login='.$login.'&apiKey='.$appkey.'&shortUrl='.urlencode($url).'&format='.$format;
	return curl_get_result($connectURL);
}

/* returns a result form url */
function curl_get_result($url) {
	$ch = curl_init();
	$timeout = 5;
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
	$data = curl_exec($ch);
	curl_close($ch);
	return $data;
}

/* get the short url */
$short_url = get_bitly_short_url('https://davidwalsh.name/','davidwalshblog','xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');

/* get the long url from the short one */
$long_url = get_bitly_long_url($short_url,'davidwalshblog','xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');


All you really need to is pass your appkey and login (you must sign up for their API service), the long or short URL, and the format which you'd like the result to be returned in. If you just want a simple URL with no other information, use the default "txt" format. Retrieving the XML or JSON formats will provide you more information about the URL.

您真正需要做的就是传递您的appkey和登录名(您必须注册其API服务),长或短URL以及希望返回结果的格式。如果您只想使用简单的URL没有其他信息,请使用默认的“ txt”格式。 检索XML或JSON格式将为您提供有关URL的更多信息。

Bit.ly is awesome. I mean, Twitter uses them -- what more of an endorsement would you need.

Bit.ly很棒。 我的意思是,Twitter使用它们-您还需要更多认可。

翻译自: https://davidwalsh.name/bitly-api-php

bit.ly 短地址转换

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值