新浪t.cn短网址接口(PHP)

使用前要先去申请API-KEY,或使用oAuth进行认证。
将长网址变为短网址的方法:

<?php
function shortenSinaUrl($long_url){
$apiKey=’API-KEY’;
$apiUrl=’http://api.t.sina.com.cn/short_url/shorten.json?source=’.$apiKey.’&url_long=’.$long_url;
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, $apiUrl);
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, array(‘Content-type:application/json’));
$response = curl_exec($curlObj);
curl_close($curlObj);
$json = json_decode($response);
return $json[0]->url_short;
}
?>

将短网址还原成长网址的方法:

<?php
function expandSinaUrl($short_url){
$apiKey=’API-KEY’;
$apiUrl=’http://api.t.sina.com.cn/short_url/expand.json?source=’.$apiKey.’&url_short=’.$short_url;
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, $apiUrl);
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, array(‘Content-type:application/json’));
$response = curl_exec($curlObj);
curl_close($curlObj);
$json = json_decode($response);
return $json[0]->url_long;
}
?>

更多具体使用方法,请参见:http://open.t.sina.com.cn/wiki/index.php/Short_url/expand

原文地址:http://www.biaodianfu.com/sina-shortener-url-api-php.html

Published by

@XiaoKyun

双鱼男,过分热心的好人。