PHP call Sina Weibo short link API Interface address example

Source: Internet
Author: User
Tags json numeric value

Sina short URL interface stability and jump speed is still very strong, now give its API description.
This interface supports two types of return formats: XML and JSON

The corresponding URL request address is:

The code is as follows Copy Code

Xml:http://api.t.sina.com.cn/short_url/shorten.xml
Json:http://api.t.sina.com.cn/short_url/shorten.json

Instructions for use

Request Mode: Get
Request Parameters:
Source: Application of Appkey
Url_long: Long links that need to be converted
As an example:

  code is as follows copy code

xml:http://api.t.sina.com.cn/short_url/shorten.xml?source=123456789& Url_long==https://www.111cn.net

Returns the following:

<urls>
     <url>
          <url_short>http://t.cn/123456789</url_short>
          <url_long>=https://www.111cn.net</url_long
          <type>0</type>
      </URL>
</urls>

Json:http://api.t.sina.com.cn/short_url/shorten.json?source= 123456789&url_long=https://www.111cn.net

The return content is:

[{' Url_short ': ' http://t.cn/123456789 ', ' url_long ': https://www.111cn.net ', ' type ': 0}]

The code is as follows Copy Code

<?php
Session_Start ();
$allow _sep = ' 2 ';
if (Isset ($_session[' post_sep ')) {
if (Time ()-$_session[' post_sep '] < $allow _sep) {
Die (' Please do not frequent refresh, rest for 2 seconds before refreshing it ');
} else {
$_session[' post_sep '] = time ();
}
} else {
$_session[' post_sep '] = time ();
}
?>

PHP Original procedure

The code is as follows Copy Code

#短连接生成算法

Class Short_url {
#字符表
public static $charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";

public static function short ($url) {
$key = "Alexis";
$urlhash = MD5 ($key. $url);
$len = strlen ($urlhash);

#将加密后的串分成4段, 4 bytes per paragraph, each paragraph is calculated, a total of four sets of short connections can be generated
for ($i = 0; $i < 4; $i + +) {
$urlhash _piece = substr ($urlhash, $i * $len/4, $len/4);
#将分段的位与0x3fffffff做位与, 0X3FFFFFFF represents a binary number of 30 1, that is, 30-bit after the cipher string is zeroed
$hex = Hexdec ($urlhash _piece) & 0x3fffffff; #此处需要用到hexdec () converts a 16-string into a 10-numeric value, otherwise the operation is not normal

$short _url = "http://t.cn/";
#生成6位短连接
for ($j = 0; $j < 6; $j + +) {
#将得到的值与0x0000003d, 3d is 61, that is, the charset coordinates the maximum value
$short _url. = self:: $charset [$hex & 0x0000003d];
#循环完以后将hex右移5位
$hex = $hex >> 5;
}

$short _url_list[] = $short _url;
}

return $short _url_list;
}
}

$url = http://www.111cn.net;
$short = Short_url::short ($url);
Print_r ($short);

********************************

Call Method:

  code is as follows copy code

     $short = Short_url::short (' www.baidu.com ');
    var_dump ($short);
   //Omit link memcache
    $memcache->set ($cacheKey. $short [0], original address);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.