Lessn More: An open-source personal URL shortening service

Lessn More: An open-source personal URL shortening service.

There’s got to be an easier way … bookmarklet sends current url to web service which launches a pop-up window like WordPress “Press This.” Service presents default options for shortening service and analytics campaign code. User selects [Twitter|Facebook|LinkedIn] chooses correct GACT code, submits. Service shortens via bit.ly api adding in google analytics tracking code and displays the shortened url selected in a form field (like v.gd). User copies via cmd-c or ctrl-c, clicks close button on pop-up window (which closed pop up window and opens new window loading [Twitter|Facebook|LinkedIn].

Shorten URLs with is.gd PHP and cURL

Here’s some quick stub-code for shortening urls using PHP + cURL and the http://is.gd/ api.



    function is_gd_shorten_url($url_to_shorten, $use_curl=false)
    {
        # uses cURL or fopen to return the short version of a URL
        # fopen requries the php.ini setting allow_url_open=on which
        # many web hosts don't allow, cURL requires PHP to be compiled
        # with cURL support. Many hosts allow/provide cURL. check a 
        # phpinfo() page if you don't know which vs. to use.
        $url_stem = "http://is.gd/api.php?longurl=";
        $url = $url_stem . urlencode($url_to_shorten);
        $shortened_url = '';

        if(!$use_curl) {
            $fp = fopen($url, 'r');
            while (!feof($fp)) {
                $shortened_url .= fread($fp, 8192);            
            }
            fclose($fp);
        } elseif(defined(curl_init)) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            # return the transfer as a string
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $shortened_url = curl_exec($ch);
            curl_close($ch);  
        }        
        return $shortened_url;
    }

Update: added support for fopen in addition to curl.