Codeigniter, API, Web-service, PHP, jQuery, MySQL
I have working with codeigniter for last 3/4 years. i have the opportunity to developed some application using codeigniter. I found some feature common almost in all application. Some parts of the development needs extensive care such as handling the Models. It is very difficult to develop new tool thinking about deadline of project. So its better if we use some community libraries and contribute on those things. it will take less time and increase the productivity as well.
Today i am going to focus on some libraries which i personally use and get much benefits from those things. Here are those libraries:
Authentication Library: DX Auth
Authentication is a basic and most common part of every web applications. Login, Logout, Registration, change password, forgot password etc. We can create such libraries but sometime its so boring to recreate them as i fill sometime. Therefore in the codeigniter forum, there are lots of authentication libraries available. But i personally found Dx_auth very useful. Because this libraries has maximum number of features with a well documentation. Here is some of the core features:
Documentation Link: http://dexcell.shinsengumiteam.com/dx_auth/
Codeigniter ORM Library: Datamapper ORM
Deal with model is very important in every application. ORM is one of the core features for Data Mapping. Some time we create this mapper by ourself. but as there is a build in Library specially for Codeigniter, why we should build another one. DataMapper ORM is such one which saves lots of our time and give us a clear view of managing models.
Here is some core features of the Library:
Documentation link: http://datamapper.wanwizard.eu
CodeIgniter and Doctrine
This is another way to mapping database. You will have a complete guideline from the following link:
Documentation Link: http://www.phpandstuff.com/articles/codeigniter-doctrine-from-scratch-day-1-install-and-setup
Here is the list of articles. Please have a look on this as well.
Hope that, this links will work for you and i will come with more updates on such things.
Please have a look on part one if don’t read the previous one:
Google Contact’s API with Codeigniter Framework [part-01]
In the previous post we are missing a function called “processFeedResult($feed)” at 36. Here is the details:
/**
* Parse Feed Result
*
* @access private
* @param Zend Gdata Object $feed
* @return object $results
*/
private function _processFeedResult( $feed ){
if( $feed ){
$results = array();
foreach( $feed as $key => $entry ){
$xml = simplexml_load_string( $entry->getXML() );
//create a new Object
$obj = new stdClass;
//set value to the object
$obj->name = (string) $entry->title;
$obj->orgName = (string) $xml->organization->orgName;
$obj->orgTitle = (string) $xml->organization->orgTitle;
$obj->edit = (string) $entry->getEditLink()->href; // require for edit/delete
foreach( $xml->email as $email ){
$obj->emailAddress[] = (string) $email['address'];
}
foreach ($xml->phoneNumber as $p) {
$obj->phoneNumber[] = (string) $p;
}
foreach ($xml->website as $w) {
$obj->website[] = (string) $w['href'];
}
$results[] = $obj;
}
return $results;
}
return false;
}
Now are simplifying the process by creating two functions. One will create the connection with a API call and returns the Object. Then we will create another function for adding new contacts to that account.
Step:01
Creating common connection with Google Account
/**
* Create connection to google Account
*
* @access private
* @param none
* @return object $gData
*/
private function _connectToAgentE3GoogleAccount( $update=false ){
$this->load->library('zend');
$this->zend->load('Zend/Loader');
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Http_Client');
Zend_Loader::loadClass('Zend_Gdata_Query');
Zend_Loader::loadClass('Zend_Gdata_Feed');
try{
//perform account login and set protocol version
$client = Zend_Gdata_ClientLogin::getHttpClient('TEST_EMAIL_ACCOUNT', 'PASSWORD', 'cp');
if( $update ){
$client->setHeaders('If-Match: *');
}
$gData = new Zend_Gdata( $client );
$gData->setMajorProtocolVersion(3);
return $gData;
}catch(Exception $e){
die('Error: ' . $e->getMessage());
}
}
Step:02
Add new account to Google Account using the connection object.
/**
* Add New Contacts to E3 Gmail account
*
* @access public
* @param string $emailAddress
* @param string $fullName [optional]
* @param string $phone [optional]
* @param string $company_name [optional]
* @return string $entryId
*/
public function addContactToE3GoogleAccount( $emailAddress, $fullName='', $phone='', $company_name=''){
// create new entry
$doc = new DOMDocument();
$doc->formatOutput = true;
$entry = $doc->createElement('atom:entry');
$entry->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:atom', 'http://www.w3.org/2005/Atom');
$entry->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:gd', 'http://schemas.google.com/g/2005');
$doc->appendChild($entry);
// add name element
if( !$fullName ){
$fullName = 'Unkown Name';// keep the google db not empty field
}
$name = $doc->createElement('gd:name');
$entry->appendChild($name);
$fullName = $doc->createElement('gd:fullName', $fullName);
$name->appendChild($fullName);
if( !$phone ){
$phone = "000001"; // keep the google db not empty field
}
$phoneNumber = $doc->createElement('gd:phoneNumber', $phone);
$phoneNumber->setAttribute('rel' ,'http://schemas.google.com/g/2005#mobile');
$entry->appendChild($phoneNumber);
// add email element
$email = $doc->createElement('gd:email');
$email->setAttribute('address', $emailAddress);
$email->setAttribute('rel' ,'http://schemas.google.com/g/2005#work');
$entry->appendChild($email);
// connect to google Account and insert entry
$contactObj = $this->_connectToAgentE3GoogleAccount();
$entryResult = $contactObj->insertEntry($doc->saveXML(), 'http://www.google.com/m8/feeds/contacts/default/full');
return $entryResult->id;
}
Note:
So far i am able to insert some certain information using the google api’s. Here is some key note:
In: Linux
28 Mar 2011Example:
1.suppose we have two server source.com and destination.com
2.we are going to transfer a backup.sql from source.com to destination.com server.
3.we have ssh access(username & Password) to both server.
Step:01
Login to destiantion.com using ssh terminal
root@destination.com#:
step:02
Now create a a folder called back_from_source in destination.com
root@destination.com#:mkdir back_from_source root@destination.com#:
step:03
we have backup.sql file at source.com in /var/www/sql/backup.sql. we are going to copy the file in destination.com as backup.sql(you can use different name as well). we are using SCP command to do the job.
command syntax:
scp user@hostname.com:file_path/filename destionation_path/filename
example:
root@destination.com#:scp root@source.com:/var/www/sql/backup.sql /home/destionation_folder/backup.sql root@source.com password: Copy progress information will be shown with bandwidth root@destination.com#:
That’s all. Have fun.
Google has vast number of API’s. Among them Google Contacts API is one of them. Recently i have to work with this API including Codeigniter. But there is very limited number of post which describes everything very clearly. Therefore as i worked with this one, i am sharing my knowledge here.
Tools that i used:
What will be done:
A. Import Contacts from a Google Account
First of all, add the Zend Library folder in codeigniter libraries folder. Create a library file in codeigniter/library folder called Zend.php. Put the following contents into the file:
<?php if (!defined('BASEPATH')) {exit('No direct script access allowed');}
class Zend
{
/**
* Include the Zend library once
* we load the zend Library class
*
* @param none
* @return none
*/
function __construct(){
ini_set('include_path',
ini_get('include_path') . PATH_SEPARATOR . APPPATH . 'libraries');
}
/**
* Load Zend Library
*
* @param string $class
* @return object $class
*/
function load($class){
require_once (string) $class . EXT;
}
}
/* End of Zend.php */
Then we need a Existing Google Email account User Name and Password. We will pass the parameter through our controller methods. Inside the method we will load the library Zend.php. Then using this library we will load the Zend Gdata Library for importing the Contacts using Google API’s.
Have a look on the following code:
/**
* Import Contacts using Zend Library
*
* @access private
* @param string $accountUserName
* @param string $accountPassword
* @return none
*/
private function _importContacts($accountUserName, $accountPassword){
$this->load->library('zend');
$this->zend->load('Zend/Loader');
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Http_Client');
Zend_Loader::loadClass('Zend_Gdata_Query');
Zend_Loader::loadClass('Zend_Gdata_Feed');
try{
//perform account login and set protocol version
$client = Zend_Gdata_ClientLogin::getHttpClient($accountUserName, $accountPassword, 'cp');
$gData = new Zend_Gdata( $client );
$gData->setMajorProtocolVersion(3);
//perform query and get result from feed
$query = new Zend_Gdata_Query('http://www.google.com/m8/feeds/contacts/default/full');
$query->startIndex = 0;
$query->maxResults = 25;
$query->setParam('orderby', 'lastmodified');
$query->setParam('sortorder', 'descending');
$feed = $gData->getFeed($query);
$this->data['AccountTitle'] = $feed->title;
$this->data['currentNumber'] = 25;
$this->data['TotalResults'] = $feed->totalResults;
$this->data['contactList'] = $this->_processFeedResult($feed);
}catch(Exception $e){
die('Error: ' . $e->getMessage());
}
}
Comming soon…
This blog will have regular article on latest Framework, API’s, Web-service etc. You will get series of articles so that you can use them to your application. Again Professional Service on development is available .
Please follow us on Twitter, Facebook for Updates.
Codeginter Expert. Beside Codeigniter, my expertise includes Zend Framework, jQury, CSS/XHTML, API's, Web-service etc.