Christian Heilmann

How I built icant.co.uk – source code

Wednesday, June 3rd, 2009 at 3:24 pm

After my talk at FOWA in Cambridge yesterday I showed off that http://icant.co.uk is fully driven by YUI and YQL and maintained elsewhere. I’ve recorded a screencast about this earlier which is also available for download as a M4V but hadn’t released the code yet.

So here goes – this is the PHP source of icant.co.uk (without the HTML which is more or less done with YUI grids builder


// get all the feeds to grab the data
$feeds = array(
‘http://feeds2.feedburner.com/wait-till-i/gwZf’,
‘http://feeds.delicious.com/v2/rss/codepo8/myvideos?count=15’,
‘http://feeds.delicious.com/v2/rss/codepo8/sandbox’,
‘http://feeds.delicious.com/v2/rss/codepo8/icantarticles’,
‘http://www.slideshare.net/rss/user/cheilmann’
);

// assemble the YQL statement
$yql = ‘select meta.views,content.thumbnail,content.description,title,’.
‘link,description from rss where url in ‘;
$yql .= “(‘” . join($feeds,”’,’”) . “’)”;

// assemble the request url
$root = ‘http://query.yahooapis.com/v1/public/yql?q=’;
$url = $root . urlencode($yql) . ‘&format=json’;

// get the feeds and populate the data to echo out in the HTML
$feeds = renderFeeds($url);
$blog = $feeds[‘blog’];
$videos = $feeds[‘videos’];
$articles = $feeds[‘articles’];
$presentations = $feeds[‘slides’];

// this function loads all the feeds and turns them into HTML
function renderFeeds($url){

// grab the content from YQL via cURL
$c = getStuff($url);

// as the content comes back as JSON, turn it into PHP objects
$x = json_decode($c);

// reset counter for videos and presentations
$count = 0;
$vidcount = 0;

// start new array to return
$out = array();

// loop over YQL results, if they exist.
if($x->query->results->item){
foreach($x->query->results->item as $i){

// if the link comes from the blog, add to the blog HTML
if(strstr($i->link,’wait-till-i’)){
$out[‘blog’] .= ‘

  • link . ‘”>’ . $i->title .

    ’ . html_entity_decode($i->description) .

  • ‘;
    $vidcount++;
    }

    // for interviews and articles, add to the articles section
    if(strstr($i->title,’Interview’) ||
    strstr($i->title,’Article:’)){
    $out[‘articles’].= ‘

  • link . ‘”>’ . $i->title .

    YQL doesn’t send a diagnostics part

    // grab the books from my blog
    $yql = ‘select * from html where url=’.
    ‘”http://wait-till-i.com/books/”’.
    ’ and xpath=”//div[@class=’entry’]”’;
    $books = renderHTML($root.urlencode($yql).’&format=xml&diagnostics=false’);

    // this is a quick and dirty solution for the HTML output
    function renderHTML($url){
    // pull the information from YQL
    $c = getStuff($url);
    // check that something came back
    if(strstr($c,’<’)){
    // remove all the XML parts
    $c = preg_replace(“/.*|.*/”,’‘,$c);
    $c = preg_replace(“/ ” encoding=”UTF-8”?>/”,’‘,$c);
    // remove all comments
    $c = preg_replace(“//”,’‘,$c);
    }

    // send it back
    return $c;
    }

    // a simple cURL function to get information
    function getstuff($url){
    $curl_handle = curl_init();
    curl_setopt($curl_handle, CURLOPT_URL, $url);
    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
    $buffer = curl_exec($curl_handle);
    curl_close($curl_handle);
    if (empty($buffer)){
    return ‘Error retrieving data, please try later.’;
    } else {
    return $buffer;
    }

    }

  • Tags: , , , , ,

    Share on Mastodon (needs instance)

    Share on Twitter

    My other work: