Method of calling Sina short link API by php

  • 2021-07-26 07:14:37
  • OfStack

This paper describes the method of calling Sina short link API by php. Share it for your reference. The specific methods are as follows:

<?php
//Sina App_Key
define('SINA_APPKEY', '31641035');
function curlQuery($url) {
 // Set attachment HTTP Head
 $addHead = array(
 "Content-type: application/json"
 );
 // Initialization curl Of course, you can also use fsockopen Substitute
 $curl_obj = curl_init();
 // Set up the URL
 curl_setopt($curl_obj, CURLOPT_URL, $url);
 // Additional Head Content
 curl_setopt($curl_obj, CURLOPT_HTTPHEADER, $addHead);
 // Whether to output return header information
 curl_setopt($curl_obj, CURLOPT_HEADER, 0);
 // Will curl_exec Returns the result of
 curl_setopt($curl_obj, CURLOPT_RETURNTRANSFER, 1);
 // Set timeout time
 curl_setopt($curl_obj, CURLOPT_TIMEOUT, 15);
 // Execute
 $result = curl_exec($curl_obj);
 // Shut down curl Answer
 curl_close($curl_obj);
 return $result;
}
// Under simple treatment url , sina For no agreement (http://) Beginning and irregular addresses return errors
function filterUrl($url = '') {
 $url = trim(strtolower($url));
 $url = trim(preg_replace('/^http:///', '', $url));
 if ($url == '')
 return false;
 else
 return urlencode('http://' . $url);
}
// Get short URLs from long URLs
function sinaShortenUrl($long_url) {
 // Splice request address, which you can see in the official document
 $url = 'http://api.t.sina.com.cn/short_url/shorten.json?source=' . SINA_APPKEY . '&url_long=' . $long_url;
 // Get the result of the request
 $result = curlQuery($url);
 // The following line of comments is used for debugging. You can remove the comments and see from sina What is the information returned
 //print_r($result);exit();
 // Analyse json
 $json = json_decode($result);
 // Exception return false
 if (isset($json->error) || !isset($json[0]->url_short) || $json[0]->url_short == '')
 return false;
 else
 return $json[0]->url_short;
}
// Getting long URLs from short URLs, this function reuses a lot sinaShortenUrl In order to facilitate your reading and comparison, you can merge the two functions by yourself
function sinaExpandUrl($short_url) {
 // Splice request address, which you can see in the official document
 $url = 'http://api.t.sina.com.cn/short_url/expand.json?source=' . SINA_APPKEY . '&url_short=' . $short_url;
 // Get the result of the request
 $result = curlQuery($url);
 // The following line of comments is used for debugging. You can remove the comments and see from sina What is the information returned
 //print_r($result);exit();
 // Analyse json
 $json = json_decode($result);
 // Exception return false
 if (isset($json->error) || !isset($json[0]->url_long) || $json[0]->url_long == '')
 return false;
 else
 return $json[0]->url_long;
}
// Website to be shortened
$url = $long; // It's up to you here. Modify it into the website you want to shorten or get it post The data or how to drop.
$url = filterUrl($url);
$short = sinaShortenUrl($url);
$ulong = sinaExpandUrl($short);
?>

The short connection in this article already comes with 1 appkey without applying for KEY yourself. Of course, if you need to use your own, you can also replace it yourself. As for usage, it's up to you. Insert it into other programs and make a page that generates short links alone. I won't say much about how to use it.

I hope this article is helpful to everyone's PHP programming.


Related articles: