Call the Sina Weibo short address API code

Source: Internet
Author: User
I have mentioned in a previous blog that calls the Sina Weibo API to turn to a short address, and I found that many people now have the need to use the Sina Weibo short-chain interface. This is naturally a piece of cake for veterans.

I have mentioned in a previous blog that calls the Sina Weibo API to turn to a short address, and I found that many people now have the need to use the Sina Weibo short-chain interface. This is naturally a piece of cake for veterans, but it may take a lot of time for beginners to write this code, so I will share my own code for your reference.
Before using the API, create an application on the Sina Weibo open platform to obtain the APPID (however, based on my experience, an error occurs when calling the APPID of an application that has not passed the review because the permission is insufficient, but don't worry, just search for something that can be used on Baidu ).
PS: This Code Reference since Jucelin (http://jucelin.com/) share the old version of the API call code, because the new version of the API has changed a lot of places, the old code can not be used, so I modified to share it

<? Php
$ Backurl = "";
If (isset ($ _ GET ['type']) {
$ Type = $ _ GET ['type'];
Switch (trim ($ type ))
{
Case 1:
If (isset ($ _ GET ['URL']) {
$ Backurl = signed url (urlencode ($ _ GET ['URL']); // you must go through urlencode
}
Else {
$ Backurl = "error0 ";
}
Break;
Case 2:
If (isset ($ _ GET ['URL']) {
$ Backurl = expandurl ($ _ GET ['URL']);
}
Else {
$ Backurl = "error1 ";
}
Break;
Default:
$ Backurl = "error2 ";
}
}
Echo $ backurl;

Function shortenSinaUrl ($ long_url ){
$ ApiKey = 'xxxxxxxxxx'; // replace it with your APPID.
$ ApiUrl = 'https: // api.weibo.com/2/short_url/shorten.json? Source = '. $ apiKey.' & url_long = '. $ long_url;
$ CurlObj = curl_init ();
Curl_setopt ($ curlObj, CURLOPT_URL, $ apiUrl );
Curl_setopt ($ curlObj, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ curlObj, CURLOPT_SSL_VERIFYPEER, 0 );
Curl_setopt ($ curlObj, CURLOPT_HEADER, 0 );
Curl_setopt ($ curlObj, CURLOPT_HTTPHEADER, array ('content-type: application/json '));
$ Response = curl_exec ($ curlObj );
Curl_close ($ curlObj );
$ Json = json_decode ($ response );
Return $ json-> urls [0]-> url_short;
}

Function expandSinaUrl ($ short_url ){
$ ApiKey = 'xxxxxxxxxx'; // replace it with your APPID.
$ ApiUrl = 'https: // api.weibo.com/2/short_url/expand.json? Source = '. $ apiKey.' & url_short = '. $ short_url;
$ CurlObj = curl_init ();
Curl_setopt ($ curlObj, CURLOPT_URL, $ apiUrl );
Curl_setopt ($ curlObj, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ curlObj, CURLOPT_SSL_VERIFYPEER, 0 );
Curl_setopt ($ curlObj, CURLOPT_HEADER, 0 );
Curl_setopt ($ curlObj, CURLOPT_HTTPHEADER, array ('content-type: application/json '));
$ Response = curl_exec ($ curlObj );
Curl_close ($ curlObj );
$ Json = json_decode ($ response );
Return $ json-> urls [0]-> url_long;
}

Function compute URL ($ long_url ){
$ ApiKey = 'xxxxxxxxxx'; // replace it with your APPID.
$ ApiUrl = 'https: // api.weibo.com/2/short_url/shorten.json? Source = '. $ apiKey.' & url_long = '. $ long_url;
$ Response = file_get_contents ($ apiUrl );
$ Json = json_decode ($ response );
Return $ json-> urls [0]-> url_short;
}

Function expandurl ($ short_url ){
$ ApiKey = 'xxxxxxxxxx'; // replace it with your APPID.
$ ApiUrl = 'https: // api.weibo.com/2/short_url/expand.json? Source = '. $ apiKey.' & url_short = '. $ short_url;
$ Response = file_get_contents ($ apiUrl );
$ Json = json_decode ($ response );
Return $ json-> urls [0]-> url_long;
}
?>

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.