Generate TinyURLs With 1 Line Of PHP

Reading time: About 0 minutes

All you need is allow_url_fopen enabled in your php.ini and PHP version 4.3.0 or higher. If you don’t know if you have allow_url_fopen enabled, just run the function phpinfo(), which displays all of your PHP settings.

Here’s the 1 line you’ve been waiting for…

$shortenedurl = file_get_contents('http://tinyurl.com/api-create.php?url=' . urlencode('http://' . $_SERVER['HTTP_HOST']  . '/' . $_SERVER['REQUEST_URI']));

What you end up with is a tinyurl.com url of the current page. Sweet, huh? This can be especially important if you have a “Tweet this” link!

9 comments skip to comment form

  1. Pliggs said— 6 minutes later

    I am working on a Tweet This function on one of my sites that will use Thickbox, I was trying to find a way to have it auto tinyURL the link as well, this help.

    Thanks.

    #1
  2. Brenelz said— 9 minutes later

    Nice post Brian. Really simple, but very usefull!

    #2
  3. Drew Douglass said— 1 hour later

    Great stuff Brian, keep them coming!

    #3
  4. Rob said— 15 hours later

    I love one-line tricks like this!

    #4
  5. serj said— 20 hours later

    nice information, always get bugged about the urls

    #5
  6. Abhisek said— 6 days later

    Super cool! I used cURL earlier to do the job. Never thought it this way. Thanks!

    #6
  7. Brian Cray said— 1 week later

    cURL is good when you need to authenticate. In this case, simple is good :)

    Thanks for the comments everyone!

    #7
  8. einsteinsboi said— 3 weeks later

    Very nice Brian. Thanks!

    See you on Twitter

    #8
  9. James said— 3 months later

    thanks!
    how would I add to page.php and single.php?

    #9
  10. Respond to this post—

Return to navigation
61