Posts Tagged ‘codeigniter’

CodeIgniter Google Static Maps V2 API Library

Posted in PHP, Web Development on July 5th, 2011 by Steve Marks – Be the first to comment

The standard Google Maps JavaScript API is great for making fully functional, interactive maps. A lot of people don’t know however that Google also provide an API for generating static maps in the form of a normal image. This is great if you don’t need all the advanced features available in the JavaScript API, and due to the increased speed are especially useful for mobile websites.

I’ve built a CodeIgniter library for the Google Maps JavaScript API in the past so decided to take up challenge of building a library for the static maps API too. The library and documentation can all be found by following the links below:

Download Library (Last updated 5th July 2011)

View GitHub Repository

View Documentation PDF (74kb)

Demonstrations coming soon…

If you have any questions about the library or would like to leave feedback please leave a comment below or get in touch.

There is more functionality to be added to this library soon so keep checking back for any progress or follow me on GitHub.

Share

Tumblr API CodeIgniter Library

Posted in PHP, Web Development on May 31st, 2011 by Steve Marks – 1 Comment

Tumblr API CodeIgniter Library

Over the past few years Tumblr has put up a good battle against other blogging platforms and still stands strong against the likes of WordPress and Blogger. Not wanting to be narrow-minded I decided to take a look into Tumblr and was actually very impressed. I don’t think I would consider using it myself (yet) but it’s clean layout and ease of use will definitely keep me checking back once in a while.

Whilst playing with the Tumblr interface I noticed they provide a pretty nifty API that allows developers to read, create, edit and delete posts from a users blog. Already being a massive fan of the PHP framework CodeIgniter, and a potential Tumblr convert, I decided to take up the challenge of writing a CodeIgniter library that easily allows these Tumblr API calls to be made through the framework. And so I give you first release…

Downloading and Installing the Library

You can download the Tumblr API library from the following location:

Download Library

View GitHub Repository

Within the ZIP there is a config file containing various authentication and display settings, and the library itself. These should be placed into ‘application/config’ and ‘application/libraries’ respectively.

Using the Library

Using the library is relatively straight-forward with most actions only taking a few lines of code. I will provide more in-depth documentation very soon but for now let’s take a look at a few examples. The Tumblr API documentation may also help a little.

Reading Posts

The tumblr.php config file in the ‘application/config’ directory:

// URL of tumblr blog.
// Replace to be the URL of the blog you want to read posts from
$config['tumblr_blog_url'] = 'http://[YOU].tumblr.com';

The tumblr.php library file in the ‘application/libraries’ directory:

// Load the library
$this->load->library('tumblr');

// Obtain an array of posts from the specified blog
// See the config file for a list of settings available
$posts = $this->tumblr->read_posts();

// Output the posts
print_r($posts);

Example output:

Array
(
   [0] => Array
   (
      [title] => This is a sample post title
      [body] => <p>And this is the body</p>
      [id] => 6010787147
      [url] => http://[YOU].tumblr.com/post/6010787147
      [url-with-slug] => http://[YOU].tumblr.com/post/6010787147/post-1
      [type] => regular
      [date-gmt] => 2011-05-30 20:41:17 GMT
      [date] => Mon, 30 May 2011 16:41:17
      [unix-timestamp] => 1306788077
      [format] => html
      [reblog-key] => Wfd5YDHP
      [slug] => post-1
   )
   [1] => Array
   (
      [title] => And another post
      [body] => <p>More lovely post content</p>
      [id] => 6011797127
      [url] => http://[YOU].tumblr.com/post/6011797127
      [url-with-slug] => http://[YOU].tumblr.com/post/6011797127/post-2
      [type] => regular
      [date-gmt] => 2011-05-29 20:43:17 GMT
      [date] => Sun, 29 May 2011 16:43:17
      [unix-timestamp] => 1306788077
      [format] => html
      [reblog-key] => Wfd4XDVP
      [slug] => post-2
   )
)

Adding New Posts

The tumblr.php config file in the ‘application/config’ directory:

// Email address and Password used to log in to Tumblr.
$config['tumblr_email'] = '[YOUR-EMAIL]';
$config['tumblr_password'] = '[YOUR-PASSWORD]';

The tumblr.php library file in the ‘application/libraries’ directory:

// Load the library
$this->load->library('tumblr');

// Add the post
$post_data = array();
$post_data['type'] = 'regular';
$post_data['title'] = 'This is a post created through the API';
$post_data['body'] = 'And this is the body';
$post_id = $this->tumblr->write_post($post_data);

// Output the newly created post ID
echo $post_id;

Editing Existing Posts

The tumblr.php config file in the ‘application/config’ directory:

// Email address and Password used to log in to Tumblr.
$config['tumblr_email'] = '[YOUR-EMAIL]';
$config['tumblr_password'] = '[YOUR-PASSWORD]';

The tumblr.php library file in the ‘application/libraries’ directory:

// Load the library
$this->load->library('tumblr');

// Add the post
$post_data = array();
$post_data['post-id'] = '6010787147'; // The ID of the post that we want to edit
$post_data['type'] = 'regular';
$post_data['title'] = 'This is an editing post updated through the API';
$post_data['body'] = 'And even an updated body';
$this->tumblr->write_post($post_data);

Deleting Posts

The tumblr.php config file in the ‘application/config’ directory:

// Email address and Password used to log in to Tumblr.
$config['tumblr_email'] = '[YOUR-EMAIL]';
$config['tumblr_password'] = '[YOUR-PASSWORD]';

The tumblr.php library file in the ‘application/libraries’ directory:

// Load the library
$this->load->library('tumblr');

// Delete the post
$this->tumblr->delete_post('6010787147');

I will be adding more examples and documentation very soon so be sure to check back. Alternatively please leave a comment if you have any questions or feedback about the library.

Share

CodeIgniter MailChimp 1.3 API Library

Posted in PHP, Web Development on May 15th, 2011 by Steve Marks – 2 Comments

CodeIgniter MailChimp 1.3 API Library

There are lots of email marketing applications out there nowadays but by far my favourite is MailChimp. Alongside its strong use of AJAX to build an easy-to-use interface and its witty banter displayed throughout, it also has a great API allowing third parties to integrate with MailChimp accounts through simple POST requests.

By using the existing class provided by MailChimp I have now amended it so that it can be easily referenced through the PHP framework CodeIgniter by way of a new library.

Downloading and Installing the Library

You can download the library from the following location:

Download Library

View GitHub Repository

Within the ZIP there is a config file containing your MailChimp API key and the library itself. These should be placed into ‘application/config’ and ‘application/libraries’ respectively.

Your API key can be obtained by logging into your MailChimp account and navigating to ‘API Keys & Info’.

Using the Library

To use the library I recommend that you first get a good understanding of the MailChimp API itself. You can find the documentation for this at the following URL:

View the MailChimp API Documentation

Without going into every single method (that would take days) I have included just a few of the more common scenarios below to show how the MailChimp API and CodeIgniter can be used together:

Outputting lists:

$this->load->library('mcapi');

$retval = $this->mcapi->lists();

if ($this->mcapi->errorCode){

	echo "Unable to load lists()!";
	echo "\n\tCode=".$this->mcapi->errorCode;
	echo "\n\tMsg=".$this->mcapi->errorMessage."\n";

}else{

	echo "Lists that matched:".$retval['total']."\n";
	echo "Lists returned:".sizeof($retval['data'])."\n";

	foreach ($retval['data'] as $list){
		echo "Id = ".$list['id']." - ".$list['name']."\n";
		echo "Web_id = ".$list['web_id']."\n";
		echo "\tSub = ".$list['stats']['member_count'];
		echo "\tUnsub=".$list['stats']['unsubscribe_count'];
		echo "\tCleaned=".$list['stats']['cleaned_count']."\n";
	}

}

Subscribing an email address:

$this->load->library('mcapi');

$listID = "XXXXX"; // obtained by calling lists();
$emailAddress = "email@address.com";
$retval = $this->mcapi->listSubscribe($listID, $emailAddress);

if ($this->mcapi->errorCode){

	echo "Unable to subscribe email using listSubscribe()!";
	echo "\n\tCode=".$this->mcapi->errorCode;
	echo "\n\tMsg=".$this->mcapi->errorMessage."\n";

}else{

	echo $emailAddress." added successfully\n";

}

Disclaimer: Please note that I did not write the class to calling the MailChimp API. Credit for this should go to the MailChimp developers. I have simply modified it to work within the CodeIgniter framework.

Share

Resolving the CodeIgniter “No Input File Specified” Error

Posted in PHP, Web Development on November 16th, 2010 by Steve Marks – 5 Comments

A site that I work with that is built on the CodeIgniter framework today started showing the error “No Input File Specified” when visiting any page other than the homepage. The cause wasn’t immediately available as I’d never seen this error before. It also wasn’t your average PHP error so I had to dig a bit further. After a while I came across a possible solution that, after trying, seemed to do the trick.

The Solution

The error wasn’t in my actual code (that’s what a developer likes to hear!), but was rather to do with the .htaccess file for my site. I’ve included below the part of my .htaccess file I had to change to get this to work correctly:

Before

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

After

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

Spot the difference? Note the question mark following ‘index.php’ in the second snippet. In adding this my site now worked as expected. Why this changed randomly I’ll never know but I hope this helps if you find yourself in the same situation.

Share

Clearing Attachments Using CodeIgniter’s Email Class

Posted in PHP, Web Development on November 4th, 2010 by Steve Marks – Be the first to comment

The CodeIgniter Email Class makes it very simple to send emails by handling the creation of mail headers and more on your behalf. My favourite benefit of the class however is it’s attachment handling. Whereas using standalone PHP it would take multiple lines of complex code to attach a single file, CodeIgniter allows us to do the same thing by calling just one function like so:

$this->email->attach('/path/to/my/file.jpg');

I had a problem recently however where I had attached a file using the above code, sent the email and needed to send another email straight after in the same script. As outlined in the CodeIgniter documentation I used the following code to clear all the variables and begin a fresh email:

$this->email->clear();

This seemed to work to a degree. The attachment however was still coming through on the second email, even after calling the clear() function.

So how do I clear the attachments as well?

After reading further into the documentation I found that there is an optional boolean parameter that can be passed to the email classes clear() function that, if set to TRUE, will clear attachments also. It turns out that by default attachments are not removed. Our code should now look as follows:

$this->email->clear(TRUE);

Voila! No more attachments on the following emails sent out.

Share

CodeIgniter Labels Library

Posted in PHP, Web Development on October 11th, 2010 by Steve Marks – Be the first to comment

CodeIgniter Labels Library

Generating labels on the web via an application is something I’ve come across numerous times in my career and never have I been able to find a reliable and versatile enough method for doing so. As a result I generated a library for the PHP framework CodeIgniter to handle the hundreds of different variations that labels come in and output them in a number of formats (Word, HTML and soon PDF). I’ve included a couple of quick samples below to demonstrate the kind of output that can be generated by using the library:

View Microsoft Word Labels Sample Output

View HTML Labels Sample Output

Download and Installing the Library

The library can be downloaded by clicking the link the below:

Download The Labels Library

The downloaded ZIP file includes one file, labels.php, which should be placed straight into the ‘libraries’ folder of your application directory.

Using the Library

Once you’ve placed the library into the ‘libraries’ folder you’re ready to begin using it. A quick example of a controller’s code that generates labels in MS Word format can be seen below:

	// Load the library
	$this->load->library('labels');

	// Specify the format
	$config['format'] = 'word';
	// Specify the address layout, using HTML <br /> tags to determine line breaks
	// The elements listed here become the address array keys (see below)
	$config['layout'] = "first_name last_name<br />address_1<br />address_2<br />town<br />county<br />postcode";
	$this->labels->initialize($config);

	// List the addresses to used on the labels
	// Notice how the array keys correpond to the 'layout' element above
	$addresses = array(
            array(
                'first_name'=>'Steve',
                'last_name'=>'Marks',
                'address_1'=>'22 Sweet Avenue',
                'address_2'=>'Mardi Tres',
                'town'=>'Cheltenham',
                'postcode'=>'NY6 6TR'
            ),
            array(
                'first_name'=>'Bobbie',
                'last_name'=>'Marley',
                'address_1'=>'132 Reggae Lane',
                'address_2'=>'East Hunting',
                'town'=>'Northampton',
                'postcode'=>'NN2 5TR'
            ),
            array(
                'first_name'=>'James',
                'last_name'=>'Shack',
                'address_1'=>'23 Leapord Road',
                'address_2'=>'Oaklingbury',
                'town'=>'Cambridge',
                'postcode'=>'CB4 7YT'
            )
	);

	// Output the labels
	$this->labels->output($addresses);

As well as the ‘format’ and ‘labels’ configuration options we can specify a lot of other options to determine the size, font and layout of the labels as follows:

Option: format
Type: string
Default: ‘word’
Options: ‘word’, ‘html’ or ‘pdf’ (PDF support coming soon)
Description: The type of file to be produced

Option: layout
Type: string
Default: ”
Description: The layout for the label. Only ‘<br />’ tags are supported when using the ‘word’ format to specify line breaks, however standard HTML tags can used in ‘html’ format to adding bold and italic etc.

Option: labels_across
Type: integer
Default: 2
Description: The number of labels across each page in a horizontal direction

Option: labels_down
Type: integer
Default: 7
Description: The number of labels down each page in a vertical direction

Option: label_height
Type: decimal
Default: 3.81
Description: The height of each label, in centimetres

Option: label_width
Type: decimal
Default: 9.9
Description: The width of each label, in centimetres

Option: pitch_horizontal
Type: decimal
Default: 10.16
Description: The width in centimetres of the label plus, if required, the width of the space/margin to the right of each label

Option: pitch_vertical
Type: decimal
Default: 3.81
Description: The height in centimetres of the label plus, if required, the height of the space/margin underneath each label

Option: page_margin_top
Type: decimal
Default: 1.51
Description: The top page margin in centimetres. Not applicable for HTML output where you will have to adjust your print settings to obtain the required margins.

Option: page_margin_side
Type: decimal
Default: 0.47
Description: The left and right page margins in centimetres. Not applicable for HTML output where you will have to adjust your print settings to obtain the required margins.

Option: page_height
Type: decimal
Default: 29.69
Description: The height in centimetres of the page. Defaults to A4 size. Not applicable for HTML output where you will have to adjust your print settings to obtain the required paper type.

Option: page_width
Type: decimal
Default: 21
Description: The width in centimetres of the page. Defaults to A4 size. Not applicable for HTML output where you will have to adjust your print settings to obtain the required paper type.

Option: align_horizontal
Type: string
Default: ‘left’
Options: ‘left’, ‘center’ or ‘right
Description: Specifies the justification of the text within each label

Option: align_vertical
Type: string
Default: ‘center’
Options: ‘top’, center’ or ‘bottom
Description: Specifies the vertical alignment of the text within each label

Option: padding_left
Type: decimal
Default: 1
Description: The left padding in centimetres of each label

Option: padding_top
Type: decimal
Default: 0
Description: The top padding in centimetres of each label

Option: font_face
Type: string
Default: ‘Arial’
Description: The font family used for the labels

Option: font_size
Type: integer
Default: 12
Description: The font size in points used for the labels

I hope you find this library as useful as I have since building it. I’ll continue to develop it over time, however please leave any feedback, bugs or feature requests via the comments box below.

Share

CodeIgniter Rightmove V3 .blm Data Feed Parser Library

Posted in PHP, Web Development on October 2nd, 2010 by Steve Marks – 10 Comments

About The .blm Data Feed Format

Initially introduced by Rightmove, the .blm (Bulk Load Mass) data feed format for transferring property data has become somewhat of an industry standard. If you’ve had any interaction with working on a property website there’s a high chance you will have come into contact with it at some point.

Sending properties and constructing a feed is simple enough, but what if you need to parse a .blm feed. Maybe you’re building your own property portal or building an estate agent website that receives properties from a third party in this format. If so it can be challenging and time consuming to extract the data correctly, perform the relevant lookups, move images etc.

Introducing the CodeIgniter Library

This library will take a .blm file, or a ZIP file that includes the .blm, extract the data, move the images, floorplans etc to the specified directories, perform lookups on fields such as the property type, and will even archive it if required to a separate folder. The library is easy to use and I’ve also included extensive debugging so that you can see exactly where the issue lies should one arise. The result is an array of properties that you can then do with what you wish.

Downloading the Library

You can download the library by clicking the link below:

Download The Libary

The dowloaded ZIP file includes three files:

Rightmove_v3_parser.php – This file is the brains of the operation and is where all the magic happens. Simply place this straight into the ‘libraries’ folder of your application directory.

Unzip.php – This file is required if the .blm data feed and media will be provided in a zip. Again, place this file into the ‘libraries’ folder of your application directory.

sample.blm – A sample file to test the library.

Putting it into Action

Once the libraries have been put into place you’re ready to rock. I have included below a simple example to be placed in a controller that loads the library, sets a few optional parameters and processes the files:

// Load the library
$this->load->library('rightmove_v3_parser');

// Set any required parameters
// file_name - (optional)
// If it is a specific file that you are looking to get processed
// enter the filename here. Can be a .blm or ZIP file. If no
// filename is provided the library will use the last modified
//file in the directory specified by the file_location parameter
$config['file_name'] = '';

// file_location - (optional)
// The directory where the files to be processed reside. If
// blank the root directory shall be used
$config['file_location'] = '';

// image_destination - (optional)
// floorplan_destination - (optional)
// document_destination - (optional)
// epc_destination - (optional)
// hip_destination - (optional)
// The directories where the various media should be moved
// to. If blank the media will not be moved
$config['image_destination'] = 'images';
$config['floorplan_destination'] = 'floorplans';
$config['document_destination'] = 'documents';
$config['epc_destination'] = 'epcs';
$config['hip_destination'] = 'hips';

// archive_location - (optional)
// If specified, the processed file will be moved here once
// processing is completed
$config['archive_location'] = 'archived';

// Initialize the library
$this->rightmove_v3_parser->initialize($config);

// Process the data feed and return the properties as an array
$data['properties'] = $this->rightmove_v3_parser->process();

// For debugging purposes output the results
echo $this->rightmove_v3_parser->print_debugger();

// Output the properties that have been processed
print_r($data['properties']);

So there we have it. A whole data feed parsed in just a few lines of code. Please leave any feedback, bugs or change requests via the comments box below. Enjoy!

Share

CodeIgniter Google Maps V3 API Library

Posted in Javascript / jQuery, PHP, Web Development on July 2nd, 2010 by Steve Marks – 124 Comments

CodeIgniter Library - Google Maps API

In early May 2010 I wrote a Google Maps API library for CodeIgniter. This used Version 2 of the Google API and was pretty limited.

Well, I’ve been working tirelessly over the past weeks to get a new version wrote that uses Version 3 of the API instead and I am very pleased to announce that it is now avalaible for download.

The library enables you to create a map and overlay multiple markers, polylines, polygons, rectangles, ground overlays and/or circles, all of which are fully customisable. The library also supports showing directions between two points, including the ability to show the textual directions alongside the map too, and marker clustering. The first stages of integration with the Google Places API are available for use too.

To get a copy, read the documentation or to view a demo simply follow the links below:

Download Library (Last updated 27th October 2011)

View GitHub Repository

View Documentation PDF (185kb)

View Demonstrations

The demonstrations above are basic examples of what can be achieved by using the library. I recommend you read the documentation to view a full list of configurable options.

An Introduction to using the Library (Watch on YouTube)

Library Features

  • Change the maps appearance including zoom, central position, size and control positioning.
  • Add multiple markers, ground overlays, circles, rectangles, polylines and polygons. Add them all on the same map, each with a whole host of configurable options to change the appearance and how they function.
  • Add event handlers to all items on the map. This means you can perform a custom action when, for example, a marker is dragged, or the map is clicked.
  • Too many markers? Enable marker clustering to speed up and clean up your map.
  • Geocode lookups. You don’t need to know the exact lat/long co-ordinates to center the map or add a marker. You can use a building name, an address, an area, in fact any textual location will work.
  • Javascript minimisation. Include the Jsmin.php file and minify the JS output by the library.
  • Either output the Javascript to the page or write it to a separate .js file.
  • Drawing Manager library integration
  • Add traffic, bicycling and Panoramio overlays.
  • Add directions by specifying a start and end point, as well as having the option to output the textual directions to a separate element on the page.
  • Caching of Geocode requests. Speed up the map by caching any geocode lookups made.
  • Integration with the Google Places API.
  • Support for HTML5 geocoding meaning you can center the map on a user’s location.
  • Support for use within an application using HTTPS
  • Allows you to integrate with your Google Adsense account and overlay ads on top of the map.

Upcoming Features

  • Support for KML and GeoRSS Layers
  • Support for creating multiple maps on the same page
  • Viewport Marker Management
  • Extensions to the directions integration
  • Extensions to the Google Places API integration
  • Possible integration with the Elevation API

If you have any questions, spot a bug, have any feature suggestions or would like to leave feedback please leave a comment below or drop me a line.

There is a lot of functionality to be added to this library soon so keep checking back for any progress or follow me on GitHub.

Share

Losing CodeIgniter Sessions

Posted in PHP, Web Development on May 21st, 2010 by Steve Marks – 4 Comments

I had a scenario earlier today where I was setting a session using the CodeIgniter Session library, it was being set fine but then as soon as I navigated to another page it was being lost.

After much research and testing it turns out it was due to the fact my session name contained an underscore. Removing the underscore then resolved the issue:

$this->session->set_userdata('my_session_name', 'My Session Data');

Became:

$this->session->set_userdata('mysessionname', 'My Session Data');

I’m not sure why this occured and I’m suprised it’s not mentioned anywhere in the CodeIgniter user guide. Anyway, I hope that this helps someone who is experiencing similar problems with regards to losing session data.

Share

Google Maps API Library for CodeIgniter Framework

Posted in Javascript / jQuery, PHP, Web Development on May 1st, 2010 by Steve Marks – 13 Comments

CodeIgniter Library - Google Maps API


UPDATE: I have now released a new version of this library that uses Version 3 of the Google Maps API.

Click Here to Read More


I am pleased to announce that the first release of my Google Maps API library for the PHP framework CodeIgniter is now ready and available for download.

Seeing as every other website I create seems to use the Google Maps API in some shape or form I finally decided to take it upon myself to create a library in order to reduce the work required to get this setup.

To get a copy or to read the documentation simply follow the links below:

Download Googlemaps.zip

View Documentation PDF (61kb)

If you have any questions, spot a bug or would like to leave feedback please leave a comment below or drop me a line.

I have lots of ideas on how to enhance this library and will continue to announce updates as development continues.

Enjoy!!

Share