Short link, in layman's terms, is to convert long URL urls into short URL strings through program calculations. About Sina Short link we can use the Sina Weibo short link generator, which is a gadget that can generate t.cn short links. But how do we do it if we can achieve Sina short link ourselves? In this article we will teach you how to use PHP to generate short links to Sina and restore the Sina Short link class.
<?php/** generate Sina Short link or restore Sina short link * Date November 22, 2017 * Author Www.phpernote.com*/class sinaurl{//Sina appkeyconst appkey= ' 31641035 ';//curlprivate static function curlquerystring ($url) {//Set additional HTTP header $addhead=array ("Content-type:application /json ");//Initialize curl, of course, you can also use Fsockopen instead of $curl_obj=curl_init ();//Set URL curl_setopt ($curl _obj,curlopt_url, $url);// Additional head content curl_setopt ($curl _obj,curlopt_httpheader, $addHead);//Output return header information curl_setopt ($curl _obj,curlopt_header,0) ;//Return the result of Curl_exec to curl_setopt ($curl _obj,curlopt_returntransfer,1);//Set time-out curl_setopt ($curl _obj,curlopt_ timeout,8);//execute $result=curl_exec ($curl _obj);//Turn Curl back curl_close ($curl _obj); return $result;} Handles the returned result private static function Dowithresult ($result, $field) {$result =json_decode ($result, True); return Isset ($ result[0][$field])? $result [0][$field]: ';} Gets the short link 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::d owithresult ($resulT, ' Url_short ');} Gets the long link 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::d owithresult ($result, ' Url_long ');}} Use the example as follows: $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 to generate Sina short link and restore Sina Short link class (method) Summary, hope can help everyone.