Php method for calling Sina short link API

Source: Internet
Author: User
This article mainly introduces how php calls the Sina short link API, and describes the specific steps and code functions of php to call the Sina short link API in the form of an instance, which is of great practical value, for more information about how to call the Sina short link API in php, see the following example. Share it with you for your reference. The specific method is as follows:

The code is as follows:

<? Php
// Sina App_Key
Define ('sina _ APPKEY ', '123 ');
Function curlQuery ($ url ){
// Set the additional HTTP header
$ AddHead = array (
"Content-type: application/json"
);
// Initialize curl. of course, you can use fsockopen instead.
$ Curl_obj = curl_init ();
// Set the URL
Curl_setopt ($ curl_obj, CURLOPT_URL, $ url );
// Add Head content
Curl_setopt ($ curl_obj, CURLOPT_HTTPHEADER, $ addHead );
// Whether to output the returned header information
Curl_setopt ($ curl_obj, CURLOPT_HEADER, 0 );
// Return the result of curl_exec
Curl_setopt ($ curl_obj, CURLOPT_RETURNTRANSFER, 1 );
// Set the timeout value
Curl_setopt ($ curl_obj, CURLOPT_TIMEOUT, 15 );
// Execute
$ Result = curl_exec ($ curl_obj );
// Disable curl callback
Curl_close ($ curl_obj );
Return $ result;
}
// In simple processing, sina returns an error for an address that does not start with the Protocol (http: //)
Function filterUrl ($ url = ''){
$ Url = trim (strtolower ($ url ));
$ Url = trim (preg_replace ('/^ http: //', ', $ url ));
If ($ url = '')
Return false;
Else
Return urlencode ('http: // '. $ url );
}
// Obtain the short URL based on the long URL
Function sinaShortenUrl ($ long_url ){
// Concatenate the request address. you can view the address in the official document.
$ Url = 'http: // api.t.sina.com.cn/short_url/shorten.json? Source = '. SINA_APPKEY.' & url_long = '. $ long_url;
// Obtain the request result
$ Result = curlQuery ($ url );
// The following line of comments is used for debugging. you can remove the comments to see what is returned from sina.
// Print_r ($ result); exit ();
// Parse json
$ Json = json_decode ($ result );
// Returns false if an exception occurs.
If (isset ($ json-> error) |! Isset ($ json [0]-> url_short) | $ json [0]-> url_short = '')
Return false;
Else
Return $ json [0]-> url_short;
}
// Obtain the long URL based on the short url. this function reuse a lot of codes in sinaShortenUrl to facilitate your reading and comparison. you can merge the two functions by yourself.
Function sinaExpandUrl ($ short_url ){
// Concatenate the request address. you can view the address in the official document.
$ Url = 'http: // api.t.sina.com.cn/short_url/expand.json? Source = '. SINA_APPKEY.' & url_short = '. $ short_url;
// Obtain the request result
$ Result = curlQuery ($ url );
// The following line of comments is used for debugging. you can remove the comments to see what is returned from sina.
// Print_r ($ result); exit ();
// Parse json
$ Json = json_decode ($ result );
// Returns false if an exception occurs.
If (isset ($ json-> error) |! Isset ($ json [0]-> url_long) | $ json [0]-> url_long = '')
Return false;
Else
Return $ json [0]-> url_long;
}
// URL to be shortened
$ Url = $ long; // You can view it here and modify it to the url you want to shorten or to get the post data.
$ Url = filterUrl ($ url );
$ Short = sinaShortenUrl ($ url );
$ Ulong = sinaExpandUrl ($ short );
?>


In this article, the appkey that has been attached to the short connection does not need to apply for the KEY by yourself. of course, if you need to use your own appkey, you can replace it by yourself. As for the usage, you can insert it into other programs and create a separate page for generating short connections. I will not talk much about the usage.

I hope this article will help you with PHP programming.

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.