phpformstinyurl

Anyway to shorten URL via PHP FORM?


I currently have people filling out a form with the following information:

  1. Your Name
  2. Recipient Name
  3. Message
  4. Amount

I'm wondering how I can get when they fill out this information, it inputs it into a link.. for example this is the code I'm using right now which works:

http://mywebsite.com/picture.php?to=" . $rname . "&from=" . $sname . "&message=" . $message . "&amount=" . $amount .

I'm wondering now how I can get that link to be shorter? Maybe with a TINY URL API or something? But how would I go about doing that?


Solution

  • As you've already highlighted, the best way to do this is via an API. It's actually quite easy to use the Tiny URL API if you have CURL installed:

    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!
    $your_url = 'http://mywebsite.com/picture.php?to=' . $rname . '&from=' . $sname . '&message=' . $message . '&amount=' . $amount;
    $short_url = get_tiny_url($your_url);
    
    //returns your tiny url
    echo $short_url;
    

    Source