<?PHP/** * URL address length switch, generated by Sina Sina Short link API * USER:CHENQT * DATE:2016/8/23 * time:18:45*/classurlswitch{ Public function__construct () {//access to Sina key $this->sina_appkey = ' 31641035 '; } Private functionCurlquery ($url) { //set additional HTTP headers $addHead=Array( "Content-type:application/json" ); //Initialize curl, of course, you can also use Fsockopen instead $curl _obj=Curl_init (); //Set URLcurl_setopt ($curl _obj, Curlopt_url,$url); //Attach head contentcurl_setopt ($curl _obj, Curlopt_httpheader,$addHead); //whether to output return header informationcurl_setopt ($curl _obj, Curlopt_header, 0); //returns the result of the curl_execcurl_setopt ($curl _obj, Curlopt_returntransfer, 1); //setting the time-out periodcurl_setopt ($curl _obj, Curlopt_timeout, 15); //Execution $result= Curl_exec ($curl _obj); //Turn off Curl .Curl_close ($curl _obj); return $result; } //Simple Handling Url,sina Returns an error for non-canonical addresses that begin with no protocol (/HTTP) Public functionFilterurl ($url= ' ') { $url=Trim(Strtolower($url)); $url=Trim(Preg_replace('/^http:\/\//', ',$url)); if($url= = "') return false; Else return UrlEncode(' http://'.$url); } //get short URLs based on long URLs Public functionSinashortenurl ($long _url) { //Stitching request Address, this address you can see in the official documents $url= ' http://api.t.sina.com.cn/short_url/shorten.json?source= '.$this->sina_appkey. ' &url_long= '.$long _url; //GET request Results $result=$this->curlquery ($url); //parsing JSON $json= Json_decode ($result); //abnormal condition returns false if(isset($json->error) | | !isset($json[0]->url_short) | |$json[0]->url_short = = ']) { return false; } Else { return $json[0]->Url_short; } } //get long URLs based on short URLs, this function reuses many of the code in Sinashortenurl to make it easier for you to read the comparison, you can merge two functions yourself functionSinaexpandurl ($short _url) { //Stitching request Address, this address you can see in the official documents $url= ' http://api.t.sina.com.cn/short_url/expand.json?source= '.$this->sina_appkey. ' &url_short= '.$short _url; //GET request Results $result=$this->curlquery ($url); //parsing JSON $json= Json_decode ($result); //abnormal condition returns false if(isset($json->error) | | !isset($json[0]->url_long) | |$json[0]->url_long = = ']) { return false; } Else { return $json[0]->Url_long; } }}$URLOBJ=NewUrlswitch ();$url=$URLOBJ->filterurl (' Http://www.test.com/url.php?type=url&openid=2323kfjdskljfldsajfdsa ');Echo $URLOBJ->sinashortenurl ($url);
Long link to short link, does not affect the link with the parameters
Sina Short Link API Interface example