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

时间:2024-11-18 09:00:23

短地址转换

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

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

PHP (The PHP)

  1. /* returns the shortened url */
  2. function get_bitly_short_url($url,$login,$appkey,$format='txt') {
  3. $connectURL = '/v3/shorten?login='.$login.'&apiKey='.$appkey.'&uri='.urlencode($url).'&format='.$format;
  4. return curl_get_result($connectURL);
  5. }
  6. /* returns expanded url */
  7. function get_bitly_long_url($url,$login,$appkey,$format='txt') {
  8. $connectURL = '/v3/expand?login='.$login.'&apiKey='.$appkey.'&shortUrl='.urlencode($url).'&format='.$format;
  9. return curl_get_result($connectURL);
  10. }
  11. /* returns a result form url */
  12. function curl_get_result($url) {
  13. $ch = curl_init();
  14. $timeout = 5;
  15. curl_setopt($ch,CURLOPT_URL,$url);
  16. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  17. curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
  18. $data = curl_exec($ch);
  19. curl_close($ch);
  20. return $data;
  21. }
  22. /* get the short url */
  23. $short_url = get_bitly_short_url('/','davidwalshblog','xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
  24. /* get the long url from the short one */
  25. $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的更多信息。

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

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

翻译自: /bitly-api-php

短地址转换