http://blog.csdn.net/fdipzone/article/details/70832761
新浪提供了长链接转为短链接的API,可以把长链接转为 t.cn/xxx 这种格式的短链接。
API:
http://api.t.sina.com.cn/short_url/shorten.json (返回结果是JSON格式)
http://api.t.sina.com.cn/short_url/shorten.xml (返回结果是XML格式)
请求参数:
source 申请应用时分配的AppKey,调用接口时代表应用的唯一身份。
url_long 需要转换的长链接,需要URLencoded,最多不超过20个。
多个url参数需要使用如下方式请求:url_long=aaa&url_long=bbb
创建source方法
1.进入http://open.weibo.com/ ,选择菜单 微连接->网站接入。
2.点击立即接入,创建新应用,随便填写应用名称,点击创建。
3.创建成功后,AppKey就是source参数的值,可以用于请求创建短链接。
测试代码:
<?php
$api = 'http://api.t.sina.com.cn/short_url/shorten.json';
$source = '您申请的AppKey';
$url_long = 'http://blog.csdn.net/fdipzone';
$request_url = sprintf($api.'?source=%s&url_long=%s', $source, $url_long);
$data = file_get_contents($request_url);
echo $data;
?>
返回JSON格式
[
{
"url_short": "http:\/\/t.cn\/RyVmU5i",
"url_long": "http:\/\/blog.csdn.net\/fdipzone",
"type": 0
}
]
返回XML格式
<?xml version="1.0" encoding="UTF-8"?>
<urls>
<url>
<url_short>http://t.cn/RyVmU5i</url_short>
<url_long>http://blog.csdn.net/fdipzone</url_long>
<type>0</type>
</url>
</urls>
生成的短链接为 http://t.cn/RyVmU5i ,访问会跳转到 http://blog.csdn.net/fdipzone
完整调用方法如下:
function getSinaShortUrl($source, $url_long){
if(empty($source) || !$url_long){
return false;
}
if(!is_array($url_long)){
$url_long = array($url_long);
}
$url_param = array_map(function($value){
return '&url_long='.urlencode($value);
}, $url_long);
$url_param = implode('', $url_param);
$api = 'http://api.t.sina.com.cn/short_url/shorten.json';
$request_url = sprintf($api.'?source=%s%s', $source, $url_param);
$result = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $request_url);
$data = curl_exec($ch);
if($error=curl_errno($ch)){
return false;
}
curl_close($ch);
$result = json_decode($data, true);
return $result;
}
$source = '您申请的AppKey';
$url_long = 'http://blog.csdn.net/fdipzone';
$data = getSinaShortUrl($source, $url_long);
print_r($data);
$url_long = array(
'http://blog.csdn.net/fdipzone/article/details/46390573',
'http://blog.csdn.net/fdipzone/article/details/12180523',
'http://blog.csdn.net/fdipzone/article/details/9316385'
);
$data = getSinaShortUrl($source, $url_long);
print_r($data);
?>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
输出:
Array
(
[0] => Array
(
[url_short] => http:
[url_long] => http:
[type] => 0
)
)
Array
(
[0] => Array
(
[url_short] => http:
[url_long] => http:
[type] => 0
)
[1] => Array
(
[url_short] => http:
[url_long] => http:
[type] => 0
)
[2] => Array
(
[url_short] => http:
[url_long] => http:
[type] => 0
)
)