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.