Install & Using Google API in Yii2 using Composer

Many of us are still going in the process of learning Yii2 and how to use composer with Yii2 to include packages. Today I am explaining about how to install and use Google API in Yii2 using Composer.

Using Composer may seems to be confusing in the beginning, but in reality its not. Composer really makes the library management and dependency checking not at all a headache for the developer.

Lets start with the Tutorial on installing Google API using Composer and using it in your Yii2 Application. First of all we have to install the Yii2 Application using composer. For those who are new you can see the tutorial on installing Yii2 using composer here.

Once your Yii2 Application is installed, you can install the Google API library for PHP using composer by editing the composer.json file in the root folder of your Yii2 application.

You have to modify the composer.json file and add “google/apiclient”: “1.0.*@beta” in the require block. Once you have saved the file you have to open your composer console and run the command composer update. This command will update all the exisiting libraries and also install the latest Google API library in your Yii2 project. You can verify by going into the vendor folder in your root folder and you will see a folder ‘google’ which consists all the library files for Google API.

Now you have the library files within your project and we can start using it in your Yii2 Project. All the libraries installed via Composer is automatically loaded in your Yii2 Application. So there is no need to include new library in your action.

You can simply create objects of your library classes as usual. Since Yii2 is using namespaces you have to added ‘’ before the class name. That is the only change in Yii2 while using a thrid party library.

On a special note I noticed most of the class names given in the Google API documentation is using old class names. Google havn’t updated the official documentation to use the new class name. So do not get panic if you see Class not found error while you use code from the Google API documentation. You can check the new class names in the source file inside vendorgoogleapiclientsrcGoogle folder. The new class names looks similar to the old name. but each words are now separated by underscore.

is now

Hope this article helps you in inegrating Google API in your Yii2 Application. If you are having any queries just let me know via comments.

Create Excel Sheet from Array in PHP using PHPExcel

Recently I have published an article about how to read an excel sheet and create an array with that data in PHP using PHPExcel. Now I am giving another tutorial to create excel sheet from array in php using PHPExcel. Usually exporting an array of data into Excel is done in csv format because it is very easy to do so. But it is very easy to create a very customized Excel sheet with the array using PHPExcel.

PHPExcel provides a function fromArray() which helps us to export our array to excel sheet without any complications.The following code snippet will help you to create an excel sheet from array using php.

Here is formal definition of the fromArray function in the PHPExcel Class

I hope with code snippet to export an array of data in php to an excel sheet will help you next time. PHPExcel is a very useful library when you are dealing with data exports. Follow us for more code snippet related to PHP and Excel like excel sheet from array.

Convert Excel Sheet into PHP Array using PHPExcel

I was playing with PHPExcel library for a few days. PHPExcel is very good library to deal with Excel files in PHP.

PHPExcel is a library written in pure PHP and providing a set of classes that allow you to write to and read from different spreadsheet file formats, like Excel (BIFF) .xls, Excel 2007 (OfficeOpenXML) .xlsx, CSV, Libre/OpenOffice Calc .ods, Gnumeric, PDF, HTML.

PHPExcel offers wide range of functionality which cannot be desccribed in a single article. So I thought giving multiple article about different functionality provided by the PHPExcel library. Now I would like to point out one of the simple and might be a very helpful funtion provided in the PHPExcel class which allows  us to convert an Excel sheet into a PHP Array with a few simple lines of codes. Once we have the Excel data in a PHP array I hope everybody know how to deal with the data in array in the way you want to process it.

PHPExcel has several classes inside it to handle different objects. Inorder to handle a worksheet we need to use PHPExcel_Worksheet. This class provide a funtion called toArray() which can be used to convert an Excel worksheet into a php array.

There are some arguments which can be passed to this function to customize the output array. Here is the signature of the function with the argument list

There are some other functions which are related to toArray function. They are namedRangeToArray() and rangeToArray(). These function can be used to convert a specific range of data inside a sheet to an array. In these functions we have to specify the Range of cells (i.e. “A1:B10”), or just one cell (i.e. “A1”), to convert that data into an array.

Generate PDF from HTML code in PHP using mPDF

Recently I came across a requirement where I need to generate a pdf file in PHP. There are several PHP libraries which can be used to generate PDF. But after a little research I found an interesting library, mPDF which can convert HTML code to PDF keeping the structure.

mPDF is a smart library which consider the CSS attached to the HTML.  Not only CSS, mPDF takes care of almost all the HTML tags like, form tags, tables, images, lists etc..

Here is a basic example of using mPDF to generate PDF for a simple HTML page.

Wondering only this much? Yes, it is as simple as that, include the library, create the object and invoke the WriteHTML function passing the HTML you want to convert into PDF. Output function will render the generated PDF in the browser.

Here is another example in which we can see the mPDF consifering the CSS along with the HTML

And here is one more example in which you can email the PDF generated using mPDF directly.

More detailed examples are given here. I hope this helps you some where in your development.

If you need any kind of assistance regarding generating PDF using mPDF, I am happy to invest my time for you, Feel free drop a comment.