Shorten URLs with PHP and cURL

Here’s some quick stub-code for shortening urls using PHP + cURL and the 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 = "";
        $url = $url_stem . urlencode($url_to_shorten);
        $shortened_url = '';

        if(!$use_curl) {
            $fp = fopen($url, 'r');
            while (!feof($fp)) {
                $shortened_url .= fread($fp, 8192);            
        } 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);
        return $shortened_url;

Update: added support for fopen in addition to curl. python wordle. Fun with pylast


That’s sort of a visualization of the top tracks I’ve listened to and reported back to I’ve been wanting to do more research in the webservices area in anticipation of doing a larger collective intelligence type project, so when my friend Mark mentioned this python script called pylast I thought I’d try it out.

The pylast code is nicely done, but I didn’t see much in the way of documentation, so it took a little while to get this going, hopefully the links below will save you some time if you’re starting out hacking’s API with Python.

pylast module source
pylast installation
pylast documentation (from PyDoc, e.g. pydoc -w pylast pylast.html)

A few things you’ll need (in addition to a account with some data) API Account you’ll need you API Key, Secret Key, and password API documentation

Here is the source code. to extract your top tracks from (also below)

My top tracks overall
The wordle-ready file input file
Create your own Wordle

Please let me know how you’d improve this or if you have suggestions, links to / for cool things to do with’s API.


import time
import pylast
import re

from md5 import md5

user_name = 'mitsmb'
user_password = 'password_you_login_to_lastfm_with'
top_tracks_file = open('top_tracks_wordle.txt', 'w')

# to make the output more interesting for wordle viz. 
# run against all periods. if you just want one period, 
# delete the others from this list
# time_periods = ['PERIOD_OVERALL']
## shouldn't have to edit anything below here
md5_user_password = md5(user_password).hexdigest()
sg = pylast.SessionKeyGenerator(api_key, api_secret)
session_key = sg.get_session_key(user_name, md5_user_password)

user = pylast.User(user_name, api_key, api_secret, session_key)
top_tracks = []
for time_period in time_periods:
    # by default pylast returns a seq in the format:
    #  "Item: Andrew Bird - Fake Palindromes, Weight: 33"
    tracks = user.get_top_tracks(period=time_period)

    # regex that tries to pull out only the track name (
    # for the ex. above "Fake Palindromes"
    p = re.compile('.*[\s]-[\s](.*), Weight: [\d]+')

    for track in tracks:
        m = p.match(str(track))
        track = m.groups()[0]
    # be nice to's servers
top_tracks = "\n".join(top_tracks)

Clayton M. Christensen’s MIT World Videos

Christensen gave a keynote address today at the World Innovation Forum, here are three talks he’s given at MIT that are available via MIT World. (MIT World has over 630 video’s of significant talks given at MIT dating back to 2001).

MIT World Videos

The Innovator’s Prescription: A Disruptive Solution to the Healthcare Crisis

May 13, 2008
Clayton Christensen

China, Taiwan , and the U.S.: A Coming Conflict? Session Two

February 25, 2002
Moderator: Stephen W. Van Evera

  • Stephen W. Van Evera
  • Thomas J. Christensen
  • Harvey Feldman
  • Chas W. Freeman Jr.

The Innovation Economy: How Technology Is Transforming Existing Industries and Creating New Ones

May 23, 2002
Clayton Christensen

MIT Sloan Managment Review / World Innovation Forum 2009

Here’s a list of articles that have appeared in the MIT Sloan Management Review, Wall Street Journal / MIT Sloan Business Insight or SMR blogs. I’m tracking the #wif09 Twitter hashtag to see if other articles we’ve published would be relevant to the #wif09 crowd (on-site or, like me, virtual).


Clayton Christensen

How Hard Times Can Drive Innovation

An interview with Clayton M. Christensen
December 14, 2008

Sure, the economy’s bad. But it’s a good time to innovate, according to Clayton M. Christensen, a Harvard Business School professor who focuses on innovation. He is the author or co-author of a number of books on the subject, from “The Innovator’s Dilemma” to a book due out next month on health care, “The Innovator’s Prescription.”

Good Days for Disruptors

An interview with Clayton M. Christensen
April 1, 2009 (registration required)

In the minds of many, the financial crisis has given innovation a black eye. Disruption theorist Clayton Christensen disagrees.

Finding the Right Job For Your Product

By Clayton M. Christensen, Scott D. Anthony, Gerald Berstell and Denise Nitterhouse
April 1, 2007 (purchase: 6.50 or subscribe)

This article has three purposes: The first is to describe the benefits that executives can reap when they segment their markets by job. The second is to describe the methods that those involved in marketing and new-product development can use to identify the job-based structure of a market. And, finally, the third is to show how the details of business plans become coherent when innovators understand the job to be done.

The Great Leap: Driving Innovation From the Base of the Pyramid

By Stuart L. Hart and Clayton M. Christensen
October 15, 2002 (purchase: 6.50 or subscribe)

Billions of poor people aspire to join the world’s economy. Disruptive innovation can pave the way, helping companies combine sustainable corporate growth with social responsibility.

C.K. Prahalad

The New Frontier of Experience Innovation

By C. K. Prahalad and Venkatram Ramaswamy
July 15, 2003 (purchase: 6.50 or subscribe)

The next practices of innovation must shift the focus away from products and services and onto experience environments — supported by a network of companies and consumer communities — to co-create unique value for individual customers.

Dan Ariely

The Irrationalities of Product Pricing

An interview with Dan Ariely
September 22, 2008

This Business Insight piece is a shorter version of the interview published in MIT Sloan Management Review (see below).

A Manager’s Guide to Human Irrationalities

An Interview with Dan Ariely
January 7, 2009 (registration, purchase/6.50 or subscribe)

People aren’t stupid – they just often act that way. Noted behavioral economist Dan Ariely explains what that should mean for strategists.

3 MIT-inspired landing page strategies – Post-Click Marketing Blog – ion interactive

With the release of our book, Honest Seduction: Using Post-Click Marketing to Turn Landing Pages into Game Changers, I’ve been reflecting back on the start of the post-click marketing movement.

Our vision for post-click marketing as a new discipline — elevating the concept of landing pages into a more a holistic and strategic online marketing practice — was born in the spring of 2005. At the same time, I began a graduate program at MIT Sloan, and over the subsequent 2 years, interwove many of the latest management and marketing ideas from MIT’s best professors into our post-click marketing best practices.

Here are my top 3 “landing page secrets from MIT”, and who inspired them:

Read the full article “3 MIT-inspired landing page strategies – Post-Click Marketing Blog – ion interactive.”

Thanks to @MITSloanExecEd for the tip.