php调用新浪微博短链接api接口地址例子
新浪短网址接口的稳定性和跳转速度还是很给力的,现给出其API说明。
该接口支持两种返回格式:xml和json
对应的URL请求地址为:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4025')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4025>xml:http://api.t.sina.com.cn/short_url/shorten.xml
json:http://api.t.sina.com.cn/short_url/shorten.json
使用说明
请求方式:GET
请求参数:
source:应用的appkey
url_long:需要转换的长链接
举个例子:
xml:http://api.t.sina.com.cn/short_url/shorten.xml?source=123456789&url_long==https://www.jquerycn.cn
返回内容为:
<urls>
<url>
<url_short>http://t.cn/123456789</url_short>
<url_long>=https://www.jquerycn.cn</url_long>
<type>0</type>
</url>
</urls>
json:http://api.t.sina.com.cn/short_url/shorten.json?source=123456789&url_long=https://www.jquerycn.cn
</td></tr></table>返回内容为:
[{"url_short":"http://t.cn/123456789","url_long":https://www.jquerycn.cn","type":0}]
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5963')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5963><?php
session_start();
$allow_sep = '2';
if (isset($_SESSION['post_sep'])) {
if (time() - $_SESSION['post_sep'] < $allow_sep) {
die('请不要频繁刷新,休息2秒再刷新吧');
} else {
$_SESSION['post_sep'] = time();
}
} else {
$_SESSION['post_sep'] = time();
}
?>
php原始做法
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9846')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9846>#短连接生成算法
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 < 4; $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.cn/";
#生成6位短连接
for ($j = 0; $j < 6; $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 = http://www.jquerycn.cn;
$short = Short_Url::short($url);
print_r($short);
********************************
</td></tr></table>调用方法:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4213')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4213> $short = Short_Url::short('www.baidu.com');
var_dump($short);
//省略链接memcache
$memcache->set($cacheKey.$short[0],“原始地址”);
您可能感兴趣的文章:
PHP实现百度、网易、新浪短网址服务的API接口调用
php使用新浪微博API开发用户授权功能
php调用新浪微博短链接api接口地址例子
php获取新浪微博数据API的实例代码
新浪微博PHP版SDK的导致20007错误
JQuery与Ajax调用新浪API获取短网址的代码
下载新浪微博视频和秒拍视频的方法
html5调用app分享功能的介绍
PHP如何实现短网址
php生成短网址的思路与实现