Short link, in layman's terms, is to convert a long URL into a short URL string through program calculation and other methods. Regarding Sina short links, we can use the Sina Weibo short link generator, which is a small tool that can generate t.cn short links. But what if we implement Sina short links ourselves? In this article, we will teach you how to use php to generate Sina short links and restore Sina short link classes.
<?php /* * 生成新浪的短链接或还原新浪短链接 * date 2017年11月22日 * author www.phpernote.com */ class SinaUrl{ //新浪APPKEY const APPKEY='31641035'; //CURL private static function CURLQueryString($url){ //设置附加HTTP头 $addHead=array("Content-type: application/json"); //初始化curl,当然,你也可以用fsockopen代替 $curl_obj=curl_init(); //设置网址 curl_setopt($curl_obj,CURLOPT_URL,$url); //附加Head内容 curl_setopt($curl_obj,CURLOPT_HTTPHEADER,$addHead); //是否输出返回头信息 curl_setopt($curl_obj,CURLOPT_HEADER,0); //将curl_exec的结果返回 curl_setopt($curl_obj,CURLOPT_RETURNTRANSFER,1); //设置超时时间 curl_setopt($curl_obj,CURLOPT_TIMEOUT,8); //执行 $result=curl_exec($curl_obj); //关闭curl回话 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'); } } //使用示例,如下: $result=SinaUrl::getShort('http://www.phpernote.com/'); echo $result; //http://t.cn/zYzBqAU $result=SinaUrl::getLong('http://t.cn/zYzBqAU'); echo $result; //http://www.phpernote.com/
The above is a summary of the classes (methods) for generating Sina short links and restoring Sina short links. I hope it can help everyone.
Related recommendations:
PHP implements URL long connection generation short link operation
Link php short link algorithm collection and analysis
Weibo short link algorithm PHP version_PHP tutorial
The above is the detailed content of Sina short link generation and restoration short link tutorial. For more information, please follow other related articles on the PHP Chinese website!