logo

Impressed by our work? Hire us for exceptional Web Development Services Fiverr . Upwork

Url shortner tool in php with api

In this blog post we will discuss the url shortener tool we developed using the php programming language php is the best language for fast server side processing or programming so let s move on to the golden topic creating a url shortener tool using free shrtink api

About URL Shortener Tool By Using Free Shrtink API

In This Blog Post We Will Discuss The URL shortener tool we developed using the PHP programming language. PHP is the best language for fast server side processing or programming. So let's move on to the golden topic: creating a URL shortener tool using free Shrtink API.

Let's split the complete logic into parts to easily understand the complete logic behind the URL shortener tool. We will write the HTML part in which there will be one input of type URL. To Get Only the URL value from the user through the input tag [input=type=” URL”].

Then we add another input with a copy button to copy the URL that we get through Shrtlink API. So this is the basic overview of the inputs we add to our program then we move on to the next step where we discuss in detail the javascript part and the PHP part lets first put an eye on the javascript part

About Shrtlnk Free API (Only Rate Limiting For Just 30 seconds after 10 to 15 request)

Shrtlnk and many other APIs let you people not use your domains but use the websites to shorten the long URLs. Shrtlnk is one of the best free APIs with only 30 seconds of rate limiting.

(they used rate limiting to fast up their server and refresh the current shortened URLs for better and perfect response)

JavaScript Implementation For Sending XMLHttpRequest to the PHP

We use advanced javascript programming techniques to handle our XML Request Properly with PHP. You Can also do the same with Fetch API which is also the best built in feature provided by javascript.

We will create some default functions in javascript like the error function to print error messages same for success messages and a function to send requests to PHP and get responses in JSON format. So this is the javascript implementation with PHP. First let's discuss the next block which is about PHP handling after this we will see how we can copy the URL in javascript

PHP Implementation With URL Validation and CURL Operation

After the JavaScript part our main logic starts which is about implementing the API integration of Shrtlink API to shorten the long URLs for this we will first create some default functions to handle messages that we send as a response from PHP to JavaScript. Let's start by first checking out the URL validation step.

In PHP We Can Use the filter_var($url FILTER_VALIDATE_URL) function to validate the URL this function will check if the coming input contains a valid URL or not. Next paragraph is the last paragraph in which we will understand CURL operation in PHP.

CURL stands for (client URL). Through CURL we can make a request from PHP to the server we want to get the result from. In PHP You first have to initialize CURL by using curl_init() and end on curl_close(). the complete implementation of CURL is in the Youtube Clip That you can watch on my YouTube well the last block contains the embedding piece of the video CLIP.

Watch Complete Clip to get the best guidance and understanding through coding. We will handle the curl request perfectly by using try and catch statements in PHP. So last we get the response and send it back to the javascript. Let's come to the copying part in JavaScript

Copying URL to the Clipboard in JavaScript

In JavaScipt we can use multiple types of logic to copy the text to the clipboard but the most perfect one in my overview is to copy specific text by using range selectors that are built in features in JavaScript. A short overview of code would be something like document.createRange() and document.execCommand(“copy”). The complete implementation is inside YouTube Video. So after this Our Post is finished.

Steps to Follow

1. Writing HTML Part

2. Beautiful Layout Using CSS

3. Integrating JavaScript

4. Creating default functions in JavaScript

5. XMLHttpRequest implementation to send requests to PHP

6. creating default PHP functions

7. Validating URL in PHP

8. Integrating Perfect Advanced CURL Operation to speed up requests and get a response

9. Copying URL to the Clipboard Using JavaScript

10 . That’s It

Final Conclusion

After reading and understanding the complete Blog Post You are 100% able to make any kind of request in PHP and JavaScript. And Finally About Creating a Custom URL shortener Tool by using Shrtlnk Free API

Download Full Project

Download Source Code

Tags

Feel Free to customize the name and specific offerings to match your vision and the unique features of your website.

© All Rights Reserved AiToolsKit 2024