Sina Short Link API Interface example

Source: Internet
Author: User

<?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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.