PHP implementation of long URLs and short URLs method

Source: Internet
Author: User
Tags md5 encryption
This article mainly introduces the PHP long URL and short URL implementation method, the need for friends can refer to the following

As the name implies, a long link is a long link, and a short link is a short link. Long links can generate short links, and short links come from long links.

What are long links, short links

As the name implies, a long link is a long link, and a short link is a short link. Long links can generate short links, and short links come from long links.

Why use long links, short links

More beautiful, we can compare long links and short links, it is clear that the short link is more than a link to a shorter, more beautiful.

Security, long links may carry some parameters, such as Http://xxx.xxx.xxx?id=1¶m1=san, we can easily see that the URL takes the way of get, but also can see which parameters are requested. However, short link http://t.cn/RNGQRUJ, we can only access to not easily see what parameters are carried and other conditions.

Example code download Address: Link: https://pan.baidu.com/s/1kVh4FQ3 Password: 4r8p

Usage Scenarios

Sharing of Weibo content

Text message contains a link

Share Links

Implementation method

According to the explanation of Baidu Encyclopedia, long link conversion to short link is mainly using the MD5 encryption method to achieve the conversion.

Code implementation

Create a Curl tool function


Sina_appkey is your APPKEY define on the developer platform (' Sina_appkey ', '); function Curlquery ($url) {   //Set additional HTTP header   $addHead = Array (     "Content-type:application/json",   );   Initialize curl, of course, you can also use Fsockopen instead of   $curl _obj = Curl_init ();   Set URL   curl_setopt ($curl _obj, Curlopt_url, $url);   Additional head content   curl_setopt ($curl _obj, Curlopt_httpheader, $addHead);   Output return header information   curl_setopt ($curl _obj, Curlopt_header, 0);   Return the results of the curl_exec to   curl_setopt ($curl _obj, Curlopt_returntransfer, 1);   Set timeout time   curl_setopt ($curl _obj, Curlopt_timeout,);   Execution   $result = curl_exec ($curl _obj);   Turn off Curl reply   curl_close ($curl _obj);   return $result; }

Ways to create short links and long links


Get short URL function sinashortenurl ($long _url) {//Stitching request address according to the long URL, this address you can see in the official documents $url = ' Http://api.t.sina.com.cn/short_ur L/shorten.json?source= '. Sina_appkey. ' &url_long= '.   $long _url;   Get request Result $result = Curlquery ($url);   The following line of comments is used for debugging,//print_r ($result); exit ();   Parse json $json = Json_decode ($result);  Exception condition 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 you to read the comparison, you can merge two functions function Sinaexpandurl ($short _url) {//Stitching request address, This address can be viewed in the official documentation $url = ' http://api.t.sina.com.cn/short_url/expand.json?source= '. Sina_appkey. ' &url_short= '.   $short _url;   Get request Result $result = Curlquery ($url);   The following line of comments is used to debug//print_r ($result); exit ();   Parse json $json = Json_decode ($result); Exception condition returns False if (Isset ($json->error) | |!isset ($json [0]->url_long) | | $json [0]->url_long = =] {return f Alse;  } else {return $json [0]->url_long; } }

Create a URL handler function


Under simple processing, Url,sina will return an error function Filterurl ($url = ") {   $url = Trim (Strtolower ($url)) for non-canonical addresses that begin with no protocol (http:   //). $url = Trim (preg_replace ('/^http:\//', ' ', $url));   if ($url = = ") {     return false;   } else {     return UrlEncode (' http://'. $url);   }}

Calling functions


To shorten the URL $url = "http://www.qqdeveloper.com/detail/25/1.html"; It's up to you to see if you want to shorten the URL or get post data or drop it. $url = Filterurl ($url); A simple way to handle a URL echo $short = Sinashortenurl ($url); Produce a short URL based on the incoming long URL echo "</br>"; echo $ulong = Sinaexpandurl ($short);

Summarize

Related Article

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.