TinyURL allows long URL to be shortened. In this example, I’d like to show how you can build a text form that contains text keyed in by a user, and sends to the server-side PHP to detect those overly long URLs and shorten them one by one by sending them to TinyURL service.
Before I proceed, I’d like to let you know that TinyURL has an API, which is as follows and it returns a single string only that is the tiny url which has been shortened from the given “url” parameter.
In this example, I have a very simple web form that allows a user to input some text, and it uses AJAX to submit the input text to a PHP script at the server side when the user is typing the text away (triggered by the onKeyPress event). And the server-side PHP returned result is to be shown in the DIV with id “theresulttext”. The web form is as follows:
URLs contained in the input text and return back the input text.
The PHP script that takes the submitted text, checks for overly long URLs, shortens them one by one using TinyURL and return
the shortened text.
You can also try out the working example here or here. FYI, you can also use other URL shortening services besides TinyURL, there are some even provide you with shorter URL due to they have shorter domain names, such as follows: