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