As the name suggests, a long link is a very long link; a short link is a very short link. Long links can generate short links, and short links are derived from long links.
What are long links and short links
As the name suggests, a long link is a very long link; a short link is a very short link. Long links can generate short links, and short links are derived from long links.
Why use long and short links
It is more beautiful. We can compare long links and short links. It is obvious that short links are shorter and more beautiful than links.
Security, long links may carry some parameters, e.g.?id=1¶m1=san,We can easily see that the url uses the get method, and we can also see which parameters are requested. However, short link/RNGQRUJ, we can only access conditions such as what parameters are carried in the place that cannot be easily viewed.
Sample code download address: Link:/s/1kVh4FQ3Password: 4r8p
Use scenarios
Share content on Weibo
Links included in the text message
WeChat share link
Implementation method
According to Baidu Encyclopedia, the conversion of long links to short links mainly uses md5 encryption to achieve conversion.
Code implementation
Create a curl tool function
// SINA_APPKEY is your appkey on the WeChat developer platformdefine('SINA_APPKEY', ''); function curlQuery($url) { //Set the attached HTTP header $addHead = array( "Content-type: application/json", ); //Initialize curl, of course, you can also use fsocopen 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 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 time curl_setopt($curl_obj, CURLOPT_TIMEOUT, 15); //implement $result = curl_exec($curl_obj); //Close curl reply curl_close($curl_obj); return $result; }
How to create short and long links
//Get short URL based on long URLfunction sinaShortenUrl($long_url) { //Split request address, you can view this address in the official document $url = '/short_url/?source=' . SINA_APPKEY . '&url_long=' . $long_url; //Get request result $result = curlQuery($url); //The following comment line is used for debugging. //print_r($result);exit(); //Parse json $json = json_decode($result); //Exception returns false if (isset($json->error) || !isset($json[0]->url_short) || $json[0]->url_short == '') { return false; } else { return $json[0]->url_short; } } //Get long URLs based on short URLs. This function reuses a lot of code in sinaShortenUrl to facilitate your reading and comparison. You can merge two functions by yourselffunction sinaExpandUrl($short_url) { //Split request address, you can view this address in the official document $url = '/short_url/?source=' . SINA_APPKEY . '&url_short=' . $short_url; //Get request result $result = curlQuery($url); //The following comment line is used for debugging //print_r($result);exit(); //Parse json $json = json_decode($result); //Exception returns false if (isset($json->error) || !isset($json[0]->url_long) || $json[0]->url_long == '') { return false; } else { return $json[0]->url_long; } }
Create a url handler function
//After simple processing of url, sina will return an error for addresses that do not start with protocol (http://) and are not standardized.function filterUrl($url = '') { $url = trim(strtolower($url)); $url = trim(preg_replace('/^http:\//', '', $url)); if ($url == '') { return false; } else { return urlencode('http://' . $url); } }
Calling functions
//The URL to be shortened$url = "/detail/25/"; //You can do it yourself here, modify it to the URL you want to shorten, get the post data, or what to do.$url = filterUrl($url); //Simple method of processing URLecho $short = sinaShortenUrl($url); //Produce short URLs based on the incoming long URLecho "</br>"; echo $ulong = sinaExpandUrl($short);
Summarize
The above is the implementation method of PHP long and short URLs introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!