1 Comparison of APIs for Google, Baidu, NetEase, and Sina short website services
The API interface of Baidu short website is completely open to the outside world. Users do not need to apply for the APPKEY of the open platform or use the OAuth protocol, so it is relatively simple and convenient; google's short url api interface has two forms: one is similar to Baidu, which does not require complicated OAuth Authentication, but there are many restrictions; the other is to use the OAuth2.0 authentication method on the GAE platform, limited. Sina Weibo's short url api interface service is also similar to Google's. The first method is to obtain the APPKEY of the Sina Weibo open platform, and the second method is OAuth2.0 certification. NetEase only provides the second API call method similar to that provided by Sina Weibo, that is, the APPKEY needs to be applied, but the application is very easy to pass, which is different from Sina Weibo. It is worth mentioning that Netease's short url api interface seems to have a bug through the blogger's test. The test results of the three interfaces are shown in the following figure:
Baidu NetEase Sina Weibo short url api interface
2. PHP calls the Baidu short URL API
The API interface of Baidu short website is not encapsulated well. You need to request different pages (create. php and query. php), and the official sample program is also incorrect.
The code is as follows: |
Copy code |
<? Php /** * @ Author: vfhky 20130304 * @ Description: PHP calls the Baidu short URL API ** @ Param string $ type: a non-zero integer indicates that the long url is converted to the short URL, and 0 indicates that the short URL is converted to the long URL. */ Function bdUrlAPI ($ type, $ url ){ If ($ type) $ Baseurl = 'http: // dwz.cn/create.php '; Else $ Baseurl = 'http: // dwz.cn/query.php '; $ Ch = curl_init (); Curl_setopt ($ ch, CURLOPT_URL, $ baseurl ); Curl_setopt ($ ch, CURLOPT_POST, true ); Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true ); If ($ type) $ Data = array ('URL' => $ url ); Else $ Data = array ('tinyurl' => $ url ); Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data ); $ StrRes = curl_exec ($ ch ); Curl_close ($ ch ); $ ArrResponse = json_decode ($ strRes, true ); If ($ arrResponse ['status']! = 0) { Echo 'errorcode :['. $ arrResponse ['status']. '] ErrorMsg :['. iconv ('utf-8', 'gbk', $ arrResponse ['err _ msg ']). "] <br/> "; Return 0; } If ($ type) Return $ arrResponse ['tinyurl']; Else Return $ arrResponse ['longurl']; } Echo '<br/> ---------- Baidu short url api ---------- <br/> '; Echo 'long to Short: '. bdUrlAPI (1, 'http: // www.111cn.net'). '<br/> '; Echo 'short to Long: '. bdUrlAPI (0, 'http: // dwz.cn/evlhW').' <br/> '; ?> |
3. Use PHP to call the NetEase short URL API
NetEase short URL API
First, you need to apply for an appkey at http://126.am/. you can apply for an appkey as soon as you go, and the application will be quickly reviewed. However, after testing, we found a bug: The short URL generated using the interface cannot be restored to the previous long URL through the API interface, prompting "NOT_MATCH" (the corresponding official description is: the Key and the short address do not match and cannot be restored ). However, as shown in the figure above, if the short Network segment generated on the page of http://126.am/user.actioncan be changed to the original persistent network segment through api.
The code is as follows: |
Copy code |
<? Php /** * @ Author: vfhky 20130304 * @ Description: PHP calls the NetEase short URL API ** @ Param string $ type: a non-zero integer indicates that the long url is converted to the short URL, and 0 indicates that the short URL is converted to the long URL. */ Function wyUrlAPI ($ type, $ url ){ If ($ type) $ Baseurl = 'http: // 126.am/ api! Shorten. Action '; Else $ Baseurl = 'http: // 126.am/ api! Expand. Action '; /* This is the appkey I applied for. You can test it */ $ Key = '4f0c04771d4e40b4945afcfdc0337e3d '; $ Ch = curl_init (); Curl_setopt ($ ch, CURLOPT_URL, $ baseurl ); Curl_setopt ($ ch, CURLOPT_POST, true ); Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true ); If ($ type) $ Data = array ('longurl' = >$ url, 'key' => $ key ); Else $ Data = array ('signed url' = >$ url, 'key' => $ key ); Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data ); $ StrRes = curl_exec ($ ch ); Curl_close ($ ch ); $ ArrResponse = json_decode ($ strRes, true ); If ($ arrResponse ['status _ Code']! = 200) { Echo 'errorcode :['. $ arrResponse ['status _ Code']. '] ErrorMsg :['. iconv ('utf-8', 'gbk', $ arrResponse ['status _ txt ']). "] <br/> "; Return 0; } Return $ arrResponse ['URL']; } Echo '<br/> ---------- NetEase short url api ---------- <br/> '; Echo 'long to Short: '. wyUrlAPI (1, 'http: // www.111cn.net'). '<br/> '; Echo 'short to Long: '. wyUrlAPI (0, 'http: // 126.am/ huangky ').' '; Echo 'short to Long: '. wyUrlAPI (0, '2017. am/XRYsJ2'). '<br/> '; ?> |
4. PHP calls the Sina Weibo short URL API
Similarly, you must first apply for an appkey for the Sina Weibo open platform. The application address is http://open.t.sina.com.cn/. the audit process is relatively strict and slow. There are two ways to implement the Sina Weibo short url api interface. The first is the original OAuth1.0 verification method, which is relatively simple and does not require token application. The second is the OAuth2.0 verification method, this requires access_token (although the official document http://t.cn/8fgol8says that the appkeyverification can be passed directly in the first way, but the test fails ). Therefore, the following example uses the first method, that is, directly using appkey verification.
The code is as follows: |
Copy code |
<? Php /** * @ Author: vfhky 20130304 * @ Description: PHP calls the Sina short URL API ** @ Param string $ type: a non-zero integer indicates that the long url is converted to the short URL, and 0 indicates that the short URL is converted to the long URL. */ Function xlUrlAPI ($ type, $ url ){ /* This is the appkey I applied for. You can test it */ $ Key = '000000 '; If ($ type) $ Baseurl = 'http: // api.t.sina.com.cn/short_url/shorten.json? Source = '. $ key.' & url_long = '. $ url; Else $ Baseurl = 'http: // api.t.sina.com.cn/short_url/expand.json? Source = '. $ key.' & url_short = '. $ url; $ Ch = curl_init (); Curl_setopt ($ ch, CURLOPT_URL, $ baseurl ); Curl_setopt ($ ch, CURLOPT_HEADER, 0 ); Curl_setopt ($ ch, CURLOPT_TIMEOUT, 15 ); Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true ); $ StrRes = curl_exec ($ ch ); Curl_close ($ ch ); $ ArrResponse = json_decode ($ strRes, true ); If (isset ($ arrResponse-> error) |! Isset ($ arrResponse [0] ['url _ long']) | $ arrResponse [0] ['url _ long'] = '') Return 0; If ($ type) Return $ arrResponse [0] ['url _ short ']; Else Return $ arrResponse [0] ['url _ long']; } Echo '<br/> ---------- Sina short url api ---------- <br/> '; Echo 'long to Short: '. xlUrlAPI (1, 'http: // www.111cn.net'). '<br/> '; Echo 'short to Long: '. xlUrlAPI (0, 'http: // t.cn/8FdW1rm').' <br/> '; ?> |
5 Postscript
In summary, Baidu's short url api is relatively convenient and has few restrictions; Sina and Netease's API interfaces are relatively troublesome; NetEase's short url api is the only one with the API request statistics function, however, it is vulnerable to "too frequent requests and REQUEST_LIMIT ". In addition, you must use the error message provided by the API to debug any API, for example, the $ arrResponse ['status'] field of the Baidu interface and the $ arrResponse ['status _ Code'] field of NetEase.
Original translated from: http://www.huangkeye.cn/web/php/839.html