一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

php调用新浪微博短链接api接口地址例子

时间:2022-06-24 22:46:04 编辑:袖梨 来源:一聚教程网

新浪短网址接口的稳定性和跳转速度还是很给力的,现给出其API说明。
该接口支持两种返回格式:xml和json

对应的URL请求地址为:

代码如下 复制代码

xml:http://api.t.si*n*a.*com.cn/short_url/shorten.xml
json:http://api.*t.*s*ina.com.cn/short_url/shorten.json

使用说明

请求方式:GET
请求参数:
source:应用的appkey
url_long:需要转换的长链接
举个例子:

代码如下 复制代码

xml:http://api.t.si*n*a.*com.cn/short_url/shorten.xml?source=123456789&url_long==https://www.111com.net

返回内容为:



http://t*.cn*/*123456789
=https://www.111com.net
0

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

返回内容为:

[{"url_short":"http://t*.cn*/*123456789","url_long":https://www.111com.net","type":0}]

代码如下 复制代码

session_start();
$allow_sep = '2';
if (isset($_SESSION['post_sep'])) {
if (time() - $_SESSION['post_sep'] die('请不要频繁刷新,休息2秒再刷新吧');
} else {
$_SESSION['post_sep'] = time();
}
} else {
$_SESSION['post_sep'] = time();
}
?>

php原始做法

代码如下 复制代码

#短连接生成算法

class Short_Url {
#字符表
public static $charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

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

#将加密后的串分成4段,每段4字节,对每段进行计算,一共可以生成四组短连接
for ($i = 0; $i $urlhash_piece = substr($urlhash, $i * $len / 4, $len / 4);
#将分段的位与0x3fffffff做位与,0x3fffffff表示二进制数的30个1,即30位以后的加密串都归零
$hex = hexdec($urlhash_piece) & 0x3fffffff; #此处需要用到hexdec()将16进制字符串转为10进制数值型,否则运算会不正常

$short_url = "http://t.*c*n*/";
#生成6位短连接
for ($j = 0; $j #将得到的值与0x0000003d,3d为61,即charset的坐标最大值
$short_url .= self::$charset[$hex & 0x0000003d];
#循环完以后将hex右移5位
$hex = $hex >> 5;
}

$short_url_list[] = $short_url;
}

return $short_url_list;
}
}

$url = https://www.111cn.net;
$short = Short_Url::short($url);
print_r($short);

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

调用方法:

代码如下 复制代码

$short = Short_Url::short('www.baidu.com');
var_dump($short);
//省略链接memcache
$memcache->set($cacheKey.$short[0],“原始地址”);

热门栏目