PHP long web site and short Web site implementation method, php long web site implementation

Source: Internet
Author: User

PHP long web site and short Web site implementation method, php long web site implementation

As the name suggests, a long link is a long link; a short link is a short link. Long links can generate short links. Short links come from long links.

What is long link or short link?

As the name suggests, a long link is a long link; a short link is a short link. Long links can generate short links. Short links come from long links.

Why do we need to use long links and short links?

More beautiful, we can compare the long link and short link, it is obvious that the short link is more short than the link, it looks more beautiful.

Secure, long links may carry some parameters such as http://xxx.xxx.xxx? Id = 1 ipvm1 = san, we can easily see that the url adopts the get method, and also can see which parameters are requested. However, with the short link http://t.cn/rngqruj, we can't easily find some equal conditions.

Sample Code: Link: https://pan.baidu.com/s/1kVh4FQ3 password: 4r8p

Use Cases

Share Weibo content

Link contained in the SMS

Share Link

Implementation Method

According to Baidu encyclopedia, the conversion of long links to short links mainly uses md5 encryption.

Code Implementation

Create a curl tool function

// SINA_APPKEY is your appkey define ('sina _ APPKEY ', '') on the developer platform; function curlQuery ($ url) {// set the additional HTTP header $ addHead = array ("Content-type: application/json",); // initialize curl, of course, you can also use fsockopen to replace $ curl_obj = curl_init (); // set the url curl_setopt ($ curl_obj, CURLOPT_URL, $ url); // Add the header content curl_setopt ($ curl_obj, CURLOPT_HTTPHEADER, $ addHead); // whether to output the returned header information curl_setopt ($ curl_obj, CURLOPT_HEADER, 0); // return the curl_exec result 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 session curl_close ($ curl_obj ); return $ result ;}

How to create a short link and a long link

// Obtain the function sinaShortenUrl ($ long_url) {// url of the short url. You can find $ url = 'HTTP: // api.t.sina.com.cn/short_url/shorten.json? Source = '. SINA_APPKEY. '& url_long = '. $ long_url; // get the request result $ result = curlQuery ($ url); // the following line of comment is used for debugging, // print_r ($ result); exit (); // parse json $ json = json_decode ($ result); // return false if (isset ($ json-> error) if abnormal |! 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 reuses a lot of codes in sinaShortenUrl to facilitate your reading and comparing, you can merge two function sinaExpandUrl ($ short_url) {// concatenate the request address. You can view $ url = 'HTTP: // api.t.sina.com.cn/short_url/expand.json in the official documentation? Source = '. SINA_APPKEY. '& url_short = '. $ short_url; // get the request result $ result = curlQuery ($ url); // the following line of comment is used for debugging // print_r ($ result); exit (); // parse json $ json = json_decode ($ result); // return false if (isset ($ json-> error) if abnormal |! Isset ($ json [0]-> url_long) | $ json [0]-> url_long = '') {return false ;} else {return $ json [0]-> url_long ;}}

Create a url Handler

// In simple processing, sina returns an error function filterUrl ($ url = '') for URLs that start with or do not have a protocol (http '') {$ url = trim (strtolower ($ url); $ url = trim (preg_replace ('/^ http: \//', '', $ url )); if ($ url = '') {return false;} else {return urlencode ('HTTP ://'. $ url );}}

Call a function

// The url to be shortened $ url = "http://www.qqdeveloper.com/detail/25/1.html"; // here you can do it yourself, modify it to the url you want to shorten or get the post data or how to drop. $ Url = filterUrl ($ url); // Method for simple URL Processing echo $ short = sinaShortenUrl ($ url ); // generate the short URL echo "</br>" based on the input long URL; echo $ ulong = sinaExpandUrl ($ short );

Summary

The above section describes how to implement PHP Long and Short web sites. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.