Twilio REST API with CodeIgniter

16 Jun

Following up on my earlier post about using the Twilio API, here is how you can use the libraries provided by Twilio and utlize their REST api with your CodeIgniter application.

  1. To get started, download the Twilio PHP helper library
  2. Unzip twilio.php from the zip file into your CodeIgniter application/libraries directory.
  3. Create a new PHP file in the application/libraries directory (lets call it twilioapi.php)
  4. Open twilioapi.php & put in the below lines of code. Explanation for the code is below. ( I assume you know how to make custom libraries in CodeIgniter. If not, read this before proceeding further)

What we are basically doing above is creating an interface between CodeIgniter and the Twilio helper library. I think this works really well and its a much better approach (in my opinion) that hacking the helper library.

In line 2, you can see that we're including the Twilio helper library so that our library can reference its classes and methods. We declare two variables for storing the Account SID and the Auth Token. Both these values can be found in your Twilio developer account. We create a new client object $client and simply return the object. The idea is that wherever you call the library in your controller, you should get a client object, which you can then use the same way as shown in the Twilio examples.

That's it! A simple 15 line solution to integrate Twilio REST API with your CodeIgniter application. Please post suggestions to improve/add on to this library in the comments :)

Update: Here is an example method that you can put in your controller to test the API.



------

Update: I've put the code on a SVN repository hosted at Google Code. Please feel free to contribute and build around it.

8 Comments

Weekend = API

06 Jun

This weekend can officially be branded as API weekend. I started Friday by playing with the Twilio API, and ended up integrating their SMS and voice functionality into my project (more on the project later :) ). I have to say, the Twilio documentation is pretty nice when it comes to API docs, but still lacks some coherence. There is plenty of duplication of content and this ends up getting confusing once you’ve played around for a bit. The navigation and doc structure can definitely be improved from a usability perspective.

On the bright side, Twilio provides some awesome and powerful API’s if you’re looking to integrate SMS and voice interaction in your web application. With some simple to understand examples in Ruby, PHP and other languages, its a breeze really to work with their API. I was pretty impressed on how easy it was to integrate their voice feature and start making outgoing calls from my application. I see huge potential in this, especially in the areas of web conference, providing online support and SMS notifications.

The thing missing from Twilio (and the web) is information on how to integrate their API with a framework such as CodeIgniter or CakePHP. Since I’m working in CodeIgniter, I decided I might as well do it the right way and ended up writing a small library to use the Twilio API. I’ll be writing about the library in a separate post with complete details on how to integrate Twilio with a CodeIgniter application.

The other API that I’m starting to use from today (Saturday) is SendGrid. I heard about SendGrid from a couple of startup blogs and people I follow on twitter (read: @davidcohen) and it seems email delivery IS infact a big issue (read Jeff Atwood’s nice article explaining the horrors). Since the application I’m working on would (hypothetically) be sending tons of email daily, I decided I had to cover my base here rather than get burnt later after deployment. So, Saturday ended with me signing up as a SendGrid customer (free at the moment :P ) and reading through their API documentation and PHP sample code. They are using Swift Mailer as a mailing library so I had to dig around a bit for codeigniter implementations of Swift Mailer libraries, and fortunately found one posted on the CodeIgniter forums (here). I have yet to integrate Swift Mailer in codeigniter, but I’ll blog about that experience once I’m done :)

So as it stands, Sunday looks to be a day full of writing reports (for my course at CMU) and playing with the SendGrid API along with Swift Mailer integration with CodeIgniter. Plenty of API food for a week :P

Next week: Watching Machine Learning lectures by Stanford prof. Andrew NG (here)

http://www.youtube.com/watch?v=UzxYlbK2c7E
4 Comments

Update on last week’s Goal

24 Feb

Last week was pretty hectic in terms of work load and number of different projects I’m involved with right now, both in and outside my course. On a positive note, I was able to complete my last week’s goal, much to my joy!

In brief, here is what I did:

  • Integration of PHP Client Library for Google API’s that is distributed by Zend. You can get it here at http://framework.zend.com/download/gdata
  • I found a library that had some neat functions for initializing and doing a bunch of stuff with the Zend Libraries. But I couldn’t get it to work properly so I ditched that approach and build my own function that prep data for accessing calendars and such. If you’re interested you can find the library that I found originally at http://github.com/seanmcgary/Codeigniter-Gcal/
  • Created a couple of fake calendars in one of my google apps account, and tried to access them using PHP code. I wrote code to fetch, edit, add and delete events. The google documentation is really very helpful in understanding small things and making it easy to write usable code quickly.

Some of the things I’m still fighting with to improve my final output:

  • Copy Google Calendar’s CSS or find a way to display something similar to what they show on their google apps page.
  • If not the above approach, integrate this with jQuery UI Calendar and build on top of it.

This week’s Goal: Catch up on some sleep, work more on Codeignitor, specifically on Doctrine ORM and how to use it to power a web app I’m working on. Doctrine is particularly exciting to me as it offers millions of possibilities and new things to learn about, some of which people who work on RoR have been boasting for quite some while.

1 Comment

Goal for the week

15 Feb

This week I’m aiming to use the Google Calendar API for a side project of mine. A quick outline of what I’ll be trying to do:

- Create new calendars for users who register on the site.
- Modifying a user’s calendar from an admin interface.
- Modiyfing multiple calendars at one via a script.

3 simple goals, 7 days to achieve it. I’ll be programming in PHP and using jQuery as my JS library.

Leave a Comment

Phone Number Formatting Library for CodeIgnitor

18 Dec

Here is a nifty class I wrote in PHP for a side project of mine. I’m using it with CodeIgnitor, so I’m going to provide the steps to integrate it with your CodeIgnitor application quickly.

Naming Rules for custom classes:

  • File names must be capitalized. For example:  Myclass.php
  • Class declarations must be capitalized. For example:  class Myclass
  • Class names and file names must match.

Create a new file /system/application/libraries/Phoneformat.php

In line 3 that I've highlighted, you will notice that the class name is the same as the file name, without the .php extension of course.

Next, in your autoload.php file in the config folder, add the name of the file in the libraries to auto load (if you want to auto load it that is).

Now to use this to format your number, simply call the function format in the library phoneformat with the string you want to format as parameter. For example:

That's all there is to creating a library, and using it in your CodeIgnitor application. Hope this tutorial/code helped you! Lookout for more similar stuff in the future or write a comment if you need some PHP help :)

Leave a Comment

PHP, MVC and some updates

17 Dec

I’ve been coding a lot in PHP lately for a couple of side projects I’ve been working on the.  I’m trying to learn best practices while writing well structured, readable and easy to debug code as well as using the MVC approach to Web App development. This past semester at CMU I’ve got to understand some core principles behind MVC and how powerful it can be if applied/used properly!

Let me be frank now, I’ve struggled a lot with grasping MVC fundamentals in the past 2years, but I think I’ve finally cracked it after spending countless hours working on my Java assignments and side-projects.

So moving ahead, I’m now planning to share some neat tips and tricks that I’ve learnt over the past couple of months, as well as put up some code that I think can be reused in a lot of applications.

This Christmas is going to be really busy and full of  echo “fun & code”;

Leave a Comment

Pittsburgh & Carnegie Mellon

23 Aug

So I finally arrived back on this side of the World this August, and boy, was it good to be back :) To be frank, I quite like the US lifestyle where everything is so organized and calm and you can really enjoy life as a whole. Don’t quite like the hustle bustle of India though you still can’t beat the food and entertainment quotient that it provides.

The journey back this time was much more pleasurable than previous occasions. I had some great company on board and time simply flew! The part that sucked was waiting 5hrs at JFK for our connecting flight to Pittsburgh in the afternoon!!! The wait was more tiring than the 15hr Air India flight !!!!!!

Once in Pittsburgh, we were fortunate enough to have some senior Indian grads host us for a couple of days at their apartments. It ended up being a full party week as every evening one of us cooked something or the other which included Dal Makhani, Aloo gobi, Pulao, Capsicum Aloo bhaji, Yellow Dal and our daily dose of scrambled eggs. It was fun getting to know the new surroundings and meeting tons of grad students everyday who, just like me had crossed shores to experience the “American Dream”.

Carnegie Mellon CampusCarnegie Mellon has a huge campus, and by huge, I mean really huge if you’ve never studied outside India! Here is a pic of what they call “The Cut” which is surrounded by the University Center and other building in the main campus. The lush green grass and well maintained  buildings are a sight to enjoy. There are so many buildings that I barely know even 5 till now! Definitely need a campus tour sometime soon :P

Pittsburgh in it self is a pretty good city as far as what I have seen. We’ve been through a lot of places in the city (free bus passes :) ) and have been quite impressed till now. I will blog about our trip to Duquesne Incline later which should prove why I hold the above opinion!

Leave a Comment

Back in Town

31 May

After the hustle bustle of Senior week and graduation at Wilkes, and a loooong 15hr flight on Air India, I’m landed back in saddi Dilli on the 19th. Its an understatement to say thats it been quite hot here, and the weather sucks big time :P Nothing has changed since I last left this town 10months back, and probably nothing will change (except the roads in East Delhi, courtesy CWG 2010). You can still hear more honking on the roads than the number of birds in the sky, and more people+cars to accompany them.

But somethings should never change, like the Nizams in CP or the Kake Da Dabha in Patel Nagar, or Dilkhush in Paschim Vihar that server awesome food than even the best chef in the US can’t tempt you with. If its one thing I came back for, it was to whet my palettes with all the street food/punjabi delicacies that I could in the 84 days that I have here, other than of course meeting my near and dear ones :)

Some observations I’ve made in the 10 days that I’ve been in town:

- Nizams: Prices have shot up from Rs. 80-100 to Rs.120-150 !!!!!

- Dhabas are still flourishing in West Delhi with news ones coming up and serving better food than the oldies at some places. Check out Kake Da Dhaba at Patel Nagar (near metro station/red light).

- Clubs and discs charge more than an average paint guy earns in a month !!! Which by the way is ridiculous if you ask me.

- Auto wallas are still assholes and never run by meter.

- Haldirams makes better pav bhaji and Chole Bhature than Bikaner. I used to love Bikaner one’s but Haldirams beats them in taste hands down. Price: Bikaner wins hands down :)

- CNG stations have improved and my car is all battered and bruised! Time to hit the service center!

Enough for the update.. Expect the blog to get more content in the coming weeks as time frees up from supervising Paint walas and P.o.P. guys who have the house under seige :P

3 Comments

Murg Malai Kabab – Tried & Tested

19 Apr

As you guys might have noticed by now, I’m a big foodie and when I’m in the mood to tease my palette, I can go to lengths. So two weeks back, while pondering over which homework to pick up first, I heard my tummy grumble for some yummy food. Opened VahrehVah.com to see for some simple quick fix(btw, vahrehvah rarely opens !), and voila, my eyes fell on this scrumptious looking Murg Malai Kabab. Not how can a hungry carnivore resist this awesomely yummy and tempting dish! 10 mins later I was in the kitchen with my laptop getting out all the ingredients for my cooking rendezvous!

I have always found that most of the cooking recipe sites and all have very generic recipes that are good as a baseline, but if you’re a beginner or a guy(ahem..), it can be quite confusing finding replacements for unavailable ingredients or figuring out how to scale quantities. But if you couple recipes with awesome videos as this chef at VahRehVah.com has done, it makes it really simple to understand and comprehend exactly what needs to be done. So without further ado let me delve into what you need for this recipe and some nifty tips and suggestions that might make your job a tad bit easier :)

Ingredients:

1 tsp Cardamom powder.

1/2 bowl(normal dal eating bowl. Not cereal bowl!).

White Amul cheese (definitely not Cheddar) or Mozzarella Cheese, shredded.

1 pound Chicken Breast (about 450grams).

1 tbsp Corn Flour OR 1/2 tbsp Corn Starch.

1 tbsp Cream (also known as Whipping Cream. Preferably heavy).

1 tbsp Curd thick (thick means thick! No water).

1 tsp Ginger Garlic paste.

1 Green Chilli or Jalapenos, finely chopped.

1/4 tsp Meat tenderizer or raw papaya paste (optional, this makes the tikka really soft and melt in your mouth).

1-2 tbsp Oil.

1/2 tbsp Pepper powder(preferably white pepper, black will also do).

1/2 tbsp Salt (add more if you require).

Handfull of coriander leaves (optional).
Chat Masala (optional).

Here’s the video from the VahrehVah.com site, which shows how to make this. For simplicity sake, I’m adding a textual transcript for the same.

Cooking Process:

This recipe contains two marinations which are then combined into one later on.

1. Cut chicken breast to bite size pieces and dry them with paper towel and marinate with pepper powder, meat tenderizer and squeezed ginger garlic paste. Mix well and keep in fridge for 30 min before adding to other marination

For the second part, take shredded mozzarella or Amul cheese and rub it into a nice paste as shown in the video.

2.  Take a medium size bowl and add cheese, curd (thick), sour cream or plain whipping cream, corn flour, salt and cardamom powder. Do not add too much salt as the cheese will also have salt. For added taste, grind some green chillis (or Jalapenos) and coriander leaves and add it to this mix. Make this into a nice paste. There should be no lumps and all ingredients should be well mixed.

After you’ve prepared the two marinations, mix them both well. Add about 1tsp of oil in this marination and put it in the fridge for another 1 hour.

Once your chicken is marinated, take it out. If you have skewers, then put them on skewers as shown in the video, or else you can simply put them on a baking tray/pan and cook them.

Put the hottest possible setting on your oven ,if possible on broil mode and cook for 10-15 min on each side till you see fine color on the kabab. Sometimes it might take more than 15mins to cook depending on how many kababs you put in at one time.

Serve hot with sprinkled chat masala and lime juice(optional).

1 Comment

Sno Mountain & Spring Break

27 Feb

It’s the time of the year when us seniors start counting down the days to our graduation, and slowly start finding a starting point in the professional world, also known as the job market. With the economy in the US being battered from all around, its wise to say that many to-be-graduates are actually considering a post-grad than a job right now. Well, I hate to digress, but this post is more about my trip to Sno Mountain this week with my fellow seniors, and the upcoming spring break rather than sulking on the future!

Sno Mountain is an absolutely awesome place if you’re a ski lover! For us ski illiterates, they had Snow Tubing, which as you might have guessed, is what we did on Thursday evening. Just one word to describe the experience: Exhilarating ! Here are a few pictures from the trip.

Top Half of the Tubing Slope

Top Half of the Tubing Slope

[caption id="attachment_34" align="alignright" width="225" caption="Bottom Half of the Tubing Slope"]Bottom Half of the Tubing Slope[/caption]

If you ever get the chance to do this, I would say go for it! In India, you can do this in Manali. I did it in 2004 in a huge inflated Truck tyre tube, and it was not even half the fun we had on this slope! Its a perfect example of commercializing a simple activity and enhancing it to provide maximum excitement and entertainment.

The best part was that the Alumni association had organized free drinks and food after our 3hour run on the slopes! We topped the day off gorging some chilled beer and Chicken wings with Nachos on the side. What more can you ask now :)

Spring Break

Spring break starts from the 28th, and its a whole week long of relaxation and finishing all the pending work from the past 2 months! I sure have got loads piled up! Will be preparing for my GRE exam in march, as well as testing out my prototype and building some more essential features for it in the next 10 days. As they say it, Time is money, and this Spring break should be worth a lot if used judiciously! Obviously, you just can’t sit at home all day and slog out, so hitting the clubs and bars and chilling out on some yummy Indian food is also on the list of things to do next week :)

1 Comment

Great Intellects

You can fool some of the people all of the time, and all of the people some of the time, but you can not fool all of the people all of the time. — Abraham Lincoln