Archive for the ‘PHP’ Category

< Older Entries


Displaying your latest statuses from Twitter February 6th, 2013

After a couple of years at IE I’ve moved on to pastures new and started work as a technical consultant at Farringdon based tech agency Amido. This move has allowed me to finally return to the LAMP stack after a two year absence and it feels good. In celebration of that here’s a little PHP something.

Latest Tweets from @scriptic

RT @brightoniant" target="_blank">brightoniant: #brighton #naked #bike #ride youtu.be/cAXWjUMbWDo
Jun 10, 2013

@Shabtique has just completed her first Boris bike ride. So proud!
Jun 05, 2013

Going up Big Ben in a bit...
Jun 05, 2013

So at some point between me implementing my new theme and a couple of weeks ago the Twitter feed on this site stopped working. After a little investigation I discovered that Twitter have changed the way their APIs work so I had to rebuild the functionality in my theme. Now with version 1.1 of the Twitter API it’s nice and easy, as long as you use OAUTH, to embed tweets on your page ready formatted with all the bells and whistles in place. It looks great but I guess not everyone, including me, wants that – so, after some very brief research garnered no results in regards to returning just a block of HTML for the status rather than the whole widget, I decided to cobble something together myself. A process which I shall share with you now.

First off, I didn’t do all this alone – I used the very handy tmhOAuth library by Matt Harris to take care of the authentication stuff, so you’ll need to grab that before we begin (it’s also included in this handy download of all the code you see here ready to go). While we’re on the subject you’ll also need to do all the Twitter application prep work before this’ll work. You can learn about that here.

So once you’ve got all that sorted we need to write some code. First off you need to call the Twitter API and grab your JSON object using ‘user_timeline’. The two variables in the request are your Twitter username and count, which is the number of tweets you wish to return.

<?php
	// Require files from the OAuth library
	require 'tmhOAuth.php';
	require 'tmhUtilities.php';
	function getTweets(){
            //OAuth
            $tmhOAuth = new tmhOAuth(array(
                'consumer_key'    => 'YOUR CONSUMER KEY',
                'consumer_secret' => 'YOUR CONSUMER SECRET',
                'user_token'      => 'YOUR USER TOKEN',
                'user_secret'     => 'YOUR USER SECRET',
            ));
            // Request
             $code = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/statuses/user_timeline'), array('screen_name' => 'username', 'count' => 3));
            $response = json_decode($tmhOAuth->response['response']);
	}
?>

So that’ll return a handy JSON object with everything you need to retrieve your Twitter stream, now if you want to just embed Twitters beautiful widgets you can grab the status ID from that object and use oembed but all I want is the tweet text and the date. Fortunately both of these are included in the object and can be extracted like so:

<?php
	foreach($response as $obj){
            echo $obj->text . '<br />';
            echo $obj->created_at . '<br /><br />';
	}
<?

All good, except you’ll notice that all you have is a couple of raw text strings, for my purposes I’d like the time stamp to be a little simpler and obviously I’d like all the hash-tags, mentions and links to be in-tact and working. So date first – we can just write a little function to extract and re-order the bits you want.

<?php
	echo formatdate($obj->created_at);
	function formatdate($date){
            $dateseg = explode(' ', $date);
            return $dateseg[1] . ' ' . $dateseg[2] . ', ' . $dateseg[5];
	}
?>

This just explodes the date string into separate elements and then reorders it into a nice simple ‘Jan 09, 2013’ type format. When that’s done we need to tackle the more complicated task of ensuring all the links come out intact. Fortunately that JSON object we retreived earlier contains all the information you need to do that – each hash-tag, mention and link has it’s own little array containing display text, shortened link and full link – so all we need to do is scan through our tweet and do some text replacing:

<?php
	echo formattweet($obj->entities->urls, $obj->entities->user_mentions, $obj->entities->hashtags, $obj->text);
	function formattweet($links, $mentions, $hashtags, $tweet){
            $buildtweet = $tweet;
            foreach($links as $link){
                $buildtweet = str_replace($link->url, '<a href="' . $link->url . '" target="_blank">' . $link->display_url . '</a>', $buildtweet);
            }
            foreach($mentions as $mention){
                $buildtweet = str_replace($mention->screen_name, '<a href="http://twitter.com/' . $mention->screen_name . '" target="_blank">' . $mention->screen_name . '</a>', $buildtweet);
            }
            foreach($hashtags as $hashtag){
                $buildtweet = str_replace($hashtag->text, '<a href="http://twitter.com/search?q=%23' . $hashtag->text . '&amp;src=hash" target="_blank">' . $hashtag->text . '</a>', $buildtweet);
            }
 
            return $buildtweet;
	}
?>

And now we have a nice simple block of HTML containing your tweet which you can embed and display any way you like. Which is exactly what I’ve done at the top of this post and, when I get around to it, on the sidebar of this theme. If you happen to know that there’s a much simpler, ready made, way of achieving this then please let me know so I can take this post down and save myself some embarrassment.

Thanks all!

Tags: , , , , , , , , , , ,
Posted in Dynamic content, HTML, PHP, Web | No Comments »


Greener Journeys site August 4th, 2010

screenshot of The Greener Journeys website

www.greener-journeys.com

A massive gap between posts there for which I apologise. It has been a busy couple of months for me though. At the end of June i said a tearful farewell to my colleagues at Anywhichway and moved onto pastures new. As of July i have taken up the post of Contracting Developer at The Crocodile, but more about them later. This post is about my final project at Anywhichway (which actually went live shortly after I left – compounding the delay on this post).

Greener Journeys is a campaign website aimed at encouraging people to get out of their cars and onto buses and coaches. The site contains loads of information on the cause, who’s involved in it and how they’re going about achieving their goals.

The site is built in Expression Engine – version 1.6 as sadly version 2 had not come out of beta when we started development. The site is fairly straight forward, content wise, and uses mostly static page types. There is, however, a news and events section, both of which automatically archive content, and a password protected ‘user resources’ area with downloadable goodies. Site pages also display related pages and documents on the site and the home page displays the latest news stories.

This is a really nice looking site (designed by the creatives at AWW of course) and was a great project to end on. See you around guys, i’ll miss you…

Tags: , , , , , , , ,
Posted in CMS, Dynamic content, Expression Engine, HTML, PHP, Website | No Comments »


Mayday Network site launch April 19th, 2010

screenshot of The Mayday Network website

www.maydaynetwork.com

This week, after a month of hard work, late nights and gnashing of teeth, The Mayday Network website finally went live. A social network aimed at businesses who want to reduce their carbon footprint and do other good environmentally things, the site is entirely built and updated in Drupal. The website was designed by Anywhichway and I was responsible for initial front end template build and subsequent Drupal theming.

The site, currently in phase 1, has member profiles, news feeds and events listings with member sign up functionality. It’s main draw at this stage is ‘The Journey’ a tool that helps guide members, step by step, through their own ‘low carbon journey’. Members are able to tick off steps as they go and periodically upload stories of their progress for other users to see.

A great deal was acheived in a short time here so well done to everyone involved and thanks for putting in the hours :) .

Tags: , , , , , , , , ,
Posted in Dynamic content, Flash, HTML, PHP, Web, Website | No Comments »


British Soap Awards website March 2nd, 2010

screenshot of the British Soap Awards website

www.britishsoapawards.tv

This week the British Soap Awards website 2010 website went live – built by me and designed by the good folk at Anywhichway! The site is built using the CodeIgniter framework and includes a basic vote authentication function and a reporting back end utilising the PHPExcel plugin. The voting aesthetics are coded in JavaScript.

Traffic on the site is extremely heavy and as of this post has attracted more than 9000 unique voters (in two days). With more than quarter of a million expected by the end of voting it’s, with the exception of the Orange Intranet (which doesn’t count), my highest profile and most visited work so far.

Get on there and vote people and make me look good.

Tags: , , , , , , , , ,
Posted in HTML, Javascript, PHP, Website | 3 Comments »


LDJ Sheet Metal Engineering website live January 26th, 2010

LDJ Sheet Metal Engineering

www.ldjmetal.co.uk

The LDJ website went live last night. It’s a small website I designed and built for a sheet metal engineering company based in Middlesex. LDJ has a highly skilled workforce who manufacture a wide variety of products.

This was a follow up project to a site I designed and built last year for Middlesex Laser Cutting, one of LDJ’s partner companies.

Tags: , , , , , , , ,
Posted in HTML, PHP, Website | No Comments »


< Older Entries