Hướng dẫn Create a TinyURL with PHP - Generate a TinyURL with PHP

lyly2009

Gà Con

Đóng góp nội dung
#1
TinyURL is an awesome service. For those who don't know what TinyURL is, TinyURL allows you to take a long URL like "" and turn it into "https://tinyurl.com/y5or97dk". Using the PHP and TinyURL API, you can create these tiny URLs on the fly!

The TinyURL with PHP

PHP:
//gets the data from a URL  
function get_tiny_url($url)  {  
    $ch = curl_init();  
    $timeout = 5;  
    curl_setopt($ch,CURLOPT_URL,'http://tinyurl.com/api-create.php?url='.$url);  
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);  
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);  
    $data = curl_exec($ch);  
    curl_close($ch);  
    return $data;  
}

//test it out!
$new_url = get_tiny_url('https://vnzoom.org/resources/watermark-system-stwm2-1-3-7-%C4%90%C3%B3ng-d%E1%BA%A5u-%E1%BA%A2nh-cho-xenforo-2-1.2/');

//returns https://tinyurl.com/y5or97dk
echo $new_url
If you don't want to use cURL, you can use file_get_contents.
PHP:
<?php
function tinyurl($url) {
return file_get_contents('http://tinyurl.com/api-create.php?url='.$url);
}
//test it out!
$url = tinyurl('http://www.developer-paradize.blogspot.com/');
//returns http://tinyurl.com/n48rbym
echo $url;
?>
Note: If the server has disabled the file_get_contents() it will create unwanted problem. So I strongly recommend to use cURL() and it will work fine.

Simply provide the URL and you'll received the new, tiny URL in return. If you use Twitter, you'll have noticed that Twitter automates tiny URL creation for URLs in tweets.
 
Top