Using Sina Short URL API with PHP to Generate and Expand Short Links
This article explains how to use Sina's short URL API to convert long links into short t.cn links, detailing request formats, required parameters, example PHP code for generating and expanding URLs, and shows sample JSON and XML responses.
Short URLs are compact web addresses that redirect to longer original links; they are commonly created using server‑side scripts such as PHP.
Sina provides an API that can transform a long URL into a short one of the form t.cn/xxx . Two endpoint formats are available:
http://api.t.sina.com.cn/short_url/shorten.json (JSON response)
http://api.t.sina.com.cn/short_url/shorten.xml (XML response)The required request parameters are:
source : the AppKey assigned when you create a Sina application.
url_long : the URL to be shortened (URL‑encoded). Up to 20 URLs can be submitted, each as a separate url_long parameter.
Below is a simple PHP example that builds the request URL, fetches the response, and outputs the result:
<?php
$api = 'http://api.t.sina.com.cn/short_url/shorten.json'; // JSON endpoint
$source = 'YOUR_APPKEY';
$url_long = 'https://detail.tmall.com/item.htm?spm=a21wu.241046-us.9629632455.7.193eb6cbbC9gFg&id=585958323801';
$request_url = sprintf($api.'?source=%s&url_long=%s', $source, $url_long);
$data = file_get_contents($request_url);
echo $data;
?>A typical JSON response looks like this:
[
{
"url_short": "http:\/\/t.cn\/Rki0twp",
"url_long": "http:\/\/detail.tmall.com\/item.htm?spm=a21wu.241046-us.9629632455.7.193eb6cbbC9gFg&id=585958323801",
"type": 0
}
]The same information can be retrieved in XML format:
<?xml version="1.0" encoding="UTF-8"?>
<urls>
<url>
<url_short>http://t.cn/Rki0twp</url_short>
<url_long>https://detail.tmall.com/item.htm?spm=a21wu.241046-us.9629632455.7.193eb6cbbC9gFg&id=585958323801</url_long>
<type>0</type>
</url>
</urls>The generated short link in the example is http://t.cn/Rki0twp , which redirects to the original Taobao product page.
A complete reusable PHP class for short‑URL operations is provided below:
class ShortUrl {
// Sina AppKey
const APPKEY = 'xxxxxxxx'; // replace with your AppKey
private static function CURLQueryString($url) {
$addHead = array("Content-type: application/json");
$curl_obj = curl_init();
curl_setopt($curl_obj, CURLOPT_URL, $url);
curl_setopt($curl_obj, CURLOPT_HTTPHEADER, $addHead);
curl_setopt($curl_obj, CURLOPT_HEADER, 0);
curl_setopt($curl_obj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_obj, CURLOPT_TIMEOUT, 8);
$result = curl_exec($curl_obj);
curl_close($curl_obj);
return $result;
}
private static function doWithResult($result, $field) {
$result = json_decode($result, true);
return isset($result[0][$field]) ? $result[0][$field] : '';
}
public static function getShort($url) {
$url = 'http://api.t.sina.com.cn/short_url/shorten.json?source=' . self::APPKEY . '&url_long=' . $url;
$result = self::CURLQueryString($url);
return self::doWithResult($result, 'url_short');
}
public static function getLong($url) {
$url = 'http://api.t.sina.com.cn/short_url/expand.json?source=' . self::APPKEY . '&url_short=' . $url;
$result = self::CURLQueryString($url);
return self::doWithResult($result, 'url_long');
}
}Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.