Google places PHP API client.

Google have an API to provide data related place which is call “Google Places”. You can search to the api using different name of the location and also can append services (e.g Burger in Newyork) and it returns marched you keyword, location.

Last year I wrote a wrapper on PHP on top of the API to access it from PHP application.  Used Composer for the first time, as dependency manager for the wrapper.

google_places_berlin

To use it set the configuration (update the API key of you google place account) and you are ready to go.

Feel free to fork it and contribute 🙂

Advertisements

PHP and MongoDB web development beginners guide review

As I am working with mongodb for different application development,the more I explore mongo the more I am amazed.
I got a review copy from Packt couple of weeks ago and started to read PHP and MongoDB Web Development Beginner’s Guide.
Chapter 1:As Nosql is a new technology yet,people talk more than using it in their production.And in the first chapter author described what is NoSql,Types of NoSQl and why mongdb.Ttraditional and very effective way to start exploring a technology.The important part of this chapter is mongodb concepts section.There is a mongodb is quiet different than any other traditional db and here there is a difference in basic terminology too.Author described those point with example.
Chapter2:As its a beginners guide author chose blog application development and here the mongo querying comes through returning article from mongo back-end.Interesting part was,author focused on mongodb details implementation and feature.Like Upsert,Update and save.
chapter3:Building a session manager,here a session manager was built with the blog implementation.

chapter4:Aggregation queries one of the most important chapter of the book.For db administrating,building complex business model application developers have to gain mastery in db he is using for his application.In this chapter MapReduce was explained and implemented also found different essential querying based on count(),distinct.
The mapreduce is very essential concepts the examples are real life problems solving using mapreduce,I found it very useful.

chapter5:A very simple version of web analytics was built and why we can choose mongodb for different analytics solution was described.Mapreduce implementation for analytics is the fun part and reader will learn to aggregation in different use cases for his analytics.
Chapter 6: Focuses on Mongodb with mysql.when to use,how it fits.mongodb as a data archive system.
Chapter7 : This chapter is about Gridfs,mongodbs file storage solution.A simple application is built to show its power and author also mentioned when should not mongodb.
chapter8:Building location aware application.How to store geo location data using mongodb is described,interesting part of geospatial indexing,this topic well described with couple of real life use case.Also covered performing location query
Chapter 9 and 10:Security and administration section.Useful section is implementation indexing in mongodb,indexing in embeded document fields.Optimization rules will be very help full section to boost the  mongo performance and the end of the chapter replication is described.Chapter 10 is all about mongo administration using GUI application like rock mongo and PHPmongo.

So overall this book is a good reference/training book who wants to learn mongodb using PHP for first time,beside that author explained many under the hood topics.
Clear instruction and several pop up quizes made the chapters enjoyable to read and learn.What I expected bit more about scaling monogo,partitioning,sharding different topics.May be as the book is written for the Beginner these topics has been avoided.

Book Review : Yii 1.1. Application development cookbook.

As a Yii developer I love to explore the framework related books,blog posts.Recently PacktPub published a cookbook on Yii written by Alexander Makarov.

One of the best feature of Yii framework that you can develop applications really fast.The cookbook started with the chapter “Under The Hood”,beside rapid development its must to know the internal structure for maintaining good quality code.Alex tried to focus most common needs of a PHP programmer,like other frameworks you can write magic methods in Yii.In the first chapter he demonstrated that with getter,setter and event,components of Yii.Also wrote simple core method name by using which you can play with request response object.


In second,third chapter author worte abut how MVC pattern works in Yii with jquery based ajax implementation.Working with JSON is an interesting section where I learned how to handle JSON inside Yii.For REST Api based application the JSON tutorial will help a lot any Yii developer.
In third,fourth chapter he covered Yii form and testing.Common use cases like file uploading and captcha validation examples.
Chapter 6 is important in terms of application development,in the chapter how active recored pattern works,how to write and design models for db.But truly I expected ORM implementation in this chapter(like Doctrine or Propel).Highlighting code with Yii seems to be a not relevant example for this chapter in my opinion. Rather than this tricks example,data model and Yii data handling internal architecture can help a developer a lot.

Zii is a wonderful tool for handling data and make automated CRUD based basic application.I found this chapter really helpful because of writer tried to explain Under the hood of Zii,how all the classes are working.How to use grid and list view,this should help any developer.Custom grid implementation is a nice step by step example to customization Zii based code.Chapter 8,9 seems to be a well described chapters with some handy techniques for Yii core contribution and debugging.Other chapters seems interresting for advanced Yii developers,I personally liked chapter 12 as its described to add External classes,framework’s code into Yii application.Specially Zend Framework using Yii section is an exclusive section for any developer because following the steps you can easily migrate any other frameworks code to your Yii application and just use it.

And final chapter about deployment staffs which will help new Yii developers.Finally I will vote up for this book because this cookbook is well combination of “Under the Hood” and “Tricks and best Practices”.If you are a new Yii developer,I am sure it will pave your way to Yii based application development,if you are an advanced Yii expert,it will help you to extend your expertise  further.Happy Coding 🙂

Getting started MongoDB with PHP.

Web development industry is moving so fast and new solutions like NoSql is getting popular day by day.Most of the web developer and software engineers are familiar with db like Mysql.And there are specific use cases when to sue  MongoDb and when to use Mysql.

You can check this interesting discussion  from  here  to be clear.

If you want to dig more details,you can checkout this post

In this post I will code a very small PHP app,its very simple and only to get familiar with Mongo.

There are several steps like these:

1.Download and start the Mongodb from shell.

2. Download the mongo extension for PHP and enable it.

3.Declaring the mongo connection details and Creating a PHP form to collect user data.

I am assuming that in your system Apache and PHP is running already.

Step 1:Download and start the Mongodb from shell.

According to your OS and system configuration you have to download MongoDB from here.

Extract the downloaded package,go to the   terminal and naviagate your shell to the mongodb package.

Suppose  the pacakge is in Desktop/blog_post/mongodb****/

And then have to start mongoDB server by starting service mongod

so the commands will be like:


cd Desktop/blog_post/mongodb****/bin
mongod

Will see something this:


2. Download the mongo extension for PHP and enable it.


Next step is to download mongo extension for PHP.You can download it from here.
For unix based system it is very straightforward and for windows system you have to know your PHP compiler version and thread safety(check your phpinfo).
And then have to paste it to the extension directory and write into the php.ini file for enabling it.
like this:

extension=php_mongo.dll

Restart your Apache and check the phpinfo(),you will see a section like below:


3.Declaring the mongo connection:
It is simple,all you have to create a new instance of Mongo Class,it will work as a connection.
It is like:

try{
    $connection = new Mongo();
    $db = $connection->selectDB('blog_post');

}
 catch (Exception $e){
     echo 'Caught exception: ',  $e->getMessage(), "\n";
 }

Now you can create a simple form to get data,and then process make a document before inserting into the db.
I have created a 10 mins app(it took 10 min to code the form with process it for inserting).
Interface of the app is very simple:

After inserting you can check the mongo shell for the entry.For that you have to go to the mongo directory inside bin and have to run mongo service.

cd Desktop/blog_post/mongodb****/bin
mongo

In this shell you can play with mongo,to check we have to run command like below:

use blog_post
show collections
db.topic.find({"title":"Newyork"})

The output will be like below:

That’s it,you dont have to create a db,collections(tables) like other solutions and most interesting part its shell where you can play,discover new features which are not available in traditional databases.

To know more about mongoDb,I would suggest following books:

1.Little MongoDb Book.

2.MongoDB in Action

MongoDB command Reference is here

If you are lazy ( ! ) like me,you can fork  my basic app from here.

Creating a YII application theme from a HTML template.

Last week I was working on a  project,that was developed  using YII.Client requested some new features in the existing application and asked us to change the old layout of the application.In this post I am using a HTML template to integrate in YII app.

I have built a default webapp using YII command line tool.If you are not familair with YII command line basic app creation you can check my previous post on YII.

The three simple steps are:

1.Choose a HTML template and keep it under the theme directory an declare it in application configuration.

2.Making valid directory structure so that framework configuration can identify the theme

3.Add PHP code to the static HTML template

Step 1.For this post I have used a Open Source Free HTML5  template,you can choose you own and play with it.

Declare the theme name into the application configuration and it is application/protected/config/main.php and add


'theme'=>'custom_theme',

Here “custom_theme” is the name of the theme I am using.

Step 2.Keep the HTML template into the themes folder of the application.And create four directory named as follows:

>>js: here we will keep all the javascript files

>>css: here we will keep all the css files

>>images:will keep images related to the theme

>>views:here we will have to create two more directory inside it=> sites and layouts.Here we will keep our files.

Inside layout the main.php file should be kept and this file is inherited by all over the theme.

And inside layout we can keep static and custom layouts like contact us,log in etc.

Step 3.In t his step we will add php code to our html template so that it can work properly and dynamic.

<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->theme->baseUrl;?>/css/style.css" />
<?php Yii::app()->clientScript->registerScriptFile(Yii::app()->theme->baseUrl.'/js/script.js'); ?>

For title you have to add

<title><?php echo CHtml::encode($this->pageTitle); ?></title>

For menu you have to add

<?php $this->widget('zii.widgets.CMenu',array(
			'items'=>array(
				array('label'=>'Home', 'url'=>array('/site/index')),
				array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),
				array('label'=>'Contact', 'url'=>array('/site/contact')),
				array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),
				array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest)
			),
		)); ?>

And the main layout is ready,as other pages are inherited from this main.php the theme is almost ready.
As we are working with YII default command line app,I have to create a form for contact us page.The form is created during the application creation now
we have to put a custom widget to have the contact us form.I have used all the default code from the app just added form inside new custom theme.
The theme is ready.

And Contact us is:

You can fork into my git repo and play with it from here.


N.B:My git YII_THEME app is outside the YII Framework directory,to run it you should keep the framework directory just before it.

To learn more about YII theming,you can check it.

PHP framework YII-Yes It Is :)

I have played with two of the PHP frameworks.

1.CodeIgniter

2.YII

I found that  YII powerful l framework as it’s a new framework there is low resource density in the net.Me and saikat developed a Human Resource Recruitement System using YII.We had great time with YII.We were new to this framework and we have to meet a tight deadline.And we took the courageous decision to go with it.I am going to share how to build very basic web application with this.

1.Download YII framework form here.

2.Install wamp server(for windows)

3.Then keep the framwork to the www directory.

4.Go to the CMD and type commands(structure as follows)


cd  /wamp/www/yii/framwork/
yiic.php  webapp  D:/wamp/www/test_app2(path to your new web app)

my command was like below:

Run Commands

Run Commands For First Basic Web App using YII

5.After pressing “y”  the basic web application will be created.Go to the browser and just type:

http://localhost/test_app2(name of your application directory)

The basic app is like:

Home Of YII Basic web Application

Contact Us Form: Of YII Basic web Application

For further modification and functionality,developer should understand the basic of MVC pattern.Then he  could easly add new features to this basic app.

To know the features of YII .Click Here

Download YII Agile Book

So have fun with YII-YES IT IS 😀

উবুন্টুতে LAMP(লিনাক্স,আপ্যাচি,মাইএসকিউএল,পিএইচপি)ইন্সটেলেশন

উবুন্তুর যে কোন নতুন ভার্সন রিলিজ হওয়ার সাথে সাথেই আমি এক বা একাধিক কপি পেয়ে যাই(canonical হতে ফ্রি কপি)।এবার উবুন্টু 10.04 এর গ্রাফিক্স আমার দারূন লাগায়,সিধান্ত নিয়েই ফেলি এখন থেকে বেশির ভাগ কাজই উবুন্টুতে করবো।তাই প্রথমেই আমাকে পিএইচপি,মাইএস্কিউলের জন্য LAMP ইন্সটল করতে হয়।
এ জন্য পিসিতে নেট কানেকশন থাকতে হবে।
প্রথমে র্টামিনালে (Accessories > Terminal) যেতে হবে,তারপর  ~sudo tasksel install lamp-server কমান্ডটি চালাতে হবে।আপনার root পাসওয়ার্ডটি চাওয়া হবে।

পাসওয়ার্ডটি দেয়ার সাথে সাথেই ইন্সটেলেশন শুরু হবে।ইনস্টেলেশনের একটি ধাপে মাইএসকিউএলের root পাসওয়ার্ড চাবে,root পাসওয়ার্ড দেয়ার কিছু ক্ষনের মধ্যে ইন্সটেলাশন শেষ হবে।

চেক করার জন্য ব্রাউজারে http://localhost লিখুন।নিচের ইমেজটি দেখা যাবে

এখানে বলে রাখা উচিত যে web root এর পাথ হলো  /var/www/

এখানে নিজের মত ফোল্ডার তৈরী করে কাজ করা যাবে।আমি default index.html ফাইল্টিকে delete করে নিজের মত index.php ফাইল তৈরী করেছি।এ জন্য র্টামিনালে যেয়ে নিচের command গুলো চালাতে হবে

~cd /var/www/

[ডিরেক্ট্রি পরিবর্তন করলাম]

~rm index.html

[index.html ফাইলটি delete করলাম]

~touch index.php

[ index.php file তৈরি করলাম]

~sudo gedit /var/www/index.php

[ index.php file  এডিট করার command দিলাম]

একটি ব্ল্যাঙ্ক ডকুমেন্ট খুলবে তাতে কিছু তাতে কিছু test পিএইচপি কোড করে সেভ করলেই চলবে।

index.php ফাইলে php কোড লেখা

এরপর localhost লিখকেই ব্রাউজারে index.php এর কনটেন্ট দেখা যাবে ঃ)

উবুন্টুতে ল্যাম্প ইন্সটেলেশন এতোই সহজ 😀

Swapping two variables without using third one.

It was an interesting way to swapping the values of two variable.There are several ways to do that.I just tried two of the techniques using php5 that was like as belows:

Created a file called class.Swap.php .

It contained the following codes:

<?php
class Swap{
    private $a = 2;
    private $b = 5;
    private $x = 13;
    private $y = 3;
    public function before_swap(){
        echo '<h2>The values Before Swap</h2>';
        echo '<br>';
        echo " Value of A = ".$this->a;
        echo '<br>';
        echo " Value of B = ".$this->b;
        echo '<br>';
        echo " Value of X = ".$this->x;
        echo '<br>';
        echo " Value of Y = ".$this->y;
        echo '<br>';
    }
    public function swap1(){
        echo '<h3>The values After Swap Technique 1</h3>';

        $this->a = $this->a+$this->b;
        echo '<br>';
        $this->b = $this->a-$this->b;
        $this->a = $this->a-$this->b;
        echo "value of A= " ."$this->a"."<br>"."value of B= " ." "."$this->b";
    }
    public function swap2(){
    echo '<h3>The values After Swap Technique 2</h3>';
        $this->x = $this->x * $this->y;//$x = 13,$y = 3 now $x =39
        $this->y = $this->x / $this->y;
        $this->x = $this->x / $this->y;
    echo "value of X= " ."$this->x"."<br>"."value of Y= " ." "."$this->y";
    }
}
?>
Another file to call the methods.I named the file test_swap.php
It contained following codes:
<?php
include 'swap_oop.php';
$objSwap = new Swap();
$objSwap->before_swap();
$objSwap->swap1();
$objSwap->swap2();
?>

পিএইচপি তে ওওপি দারুন ফান,যারা জাভা হাল্কা পাতলা জানে খুব সহজেই তা পিএইচপিতে ধরতে পারবে।
Check it out .It is fun 😀

Fun with php :D

The proof that I am becoming lazy and learning less,that I am not posting for a long while.Its really pain for me that  the I am not doing any R&D myself… 😦

Lets pull those down,at last I sat and just googling aimlessly and found an interesting quetsion “How to reverse a given string in C?” This is the common question for the entry level programmer,if the interviewee can answer ..then to judge his swiftness employer ask a little deep “How you will revrese position of the words?” That means “Who are u” should get the form  ”u are who”..I found the question interesting to solve..then just open my NetBean started to code.At first I have to revese the whole string like this:”u era hwo”.It is much easier in php coz there is a function “strrev()” to revese any given string.Then the  tougher portion come .. how I will get it in “u are who”.I just tought to put the given string into an array then just reverese the array elemnets separately,atlast put those elements together to build an string J.as usual it was easy to think but difficult to implement.Coz the array elements must be idntified to put into a loop.I was wondeering how to index the arry elements….after trying several different techniques I put up the lead of my laptop L.then just read theory about the array in php,those were not helpful enough to solve the problem .But I learned theat the index can be a variable that means  $mushfiq[$i] = array(a,b,c,d);

At last I did that without assiging any index variable in the array.The final code was as follows:

<?php

$st = "who are u";
echo "<b>palin text :</b>".$st;
$o = strrev($st);
echo "<br>";
echo "<b>reverse text:</b> ".$o;
echo "<br>";
$array = explode(" ",$o);
echo "<br>";
echo '<b>Alter the word position item:</b>';
for ($i = 0; $i < count($array); $i++) {
$r = strrev($array[$i]);
echo ' ';
echo $r;
?>

Output :

palin text :who are u
reverse text: u era ohw

Alter the word position item: u are who

that’s it 🙂

date :09-08-2010

Interesting functions in php (1)

Couple of days ago I was just practicing some of the interesting  built in fuctions in php.Actually I was searching for those function, I didn’t use any where but could be used in necessary purpose in near future.Found two pairs of function those can be very handy time to time.

The first pair is :

1.file_get_contents()  ->read the contents of a file into a string,

2.file_put_contents ->writes a string into a file

3.touch() ->create a blank file

4.fopen() ->opens file/url,by specifieing the mode(I opend the file read/write mode as why ‘w’ was parameter)

5.fwrite()->writes into a file

My codes were as follows:

<?php
$data = file_get_contents("mushfiq.doc");//$data = file_get_contents("http://localhost/sb");
echo $data;
touch("m4.doc");//craeting a blank file (same format)
$file = fopen("m4.doc","w");
file_put_contents("$file","$data");//fwrite($file,$data);
?>

Interesting outfut comes when I changed the parameter of file_get_contents() to url .Just check it and have fun 🙂

TiTbIt: To know more about the functions just goole or search the php manual