Tag Archives: codeigniter

CodeIgniter and XMLRPC

What are we going to do?

We’ll mimic what we already achieved in CodeIgniter and NuSOAP post using XMLRPC method.

So, we’ll create two endpoints, one is for getting a CD info (function name : getCdInfo) and the other is for getting all of CDs listed (function name : getCds).

We’ll still implement all of our function inside Welcome controller. The difference is that the registration process is placed inside index() function while the implementation is outside index() function but still within Welcome class.

The Server

Here’s the configuration and registration process inside index() function:

Notice how I defined the endpoint name and register it. So, getCdInfo endpoint will be implemented in Welcome controller and getCdInfo function, while getCds endpoint will be implemented in Welcome controller as getCds function.
Now, here’s the implementation:

getCdInfo takes one parameter, so, we need to pass $request as getCdInfo function parameter while getCds function doesn’t take any parameter, so we omit it.
Pay attention on getCds function, you’ll learn how I implement an XMLRPC endpoint that return an array of array (a data table).

The Client

Same as CodeIgniter and NuSOAP, for simplicity, we’ll implement the client as part of Welcome controller. Here’s the client code:

getCdInfo call will output something like this:

While getCds will return something like this:

As you can see, the output of XMLRPC method is the same as NuSOAP method. No fixing needed!
Here’s the complete source code of our controller:

CodeIgniter and NuSOAP

What are we going to do?

We’ll use NuSOAP to create a webservice using CodeIgniter and consume it using CI also.

In this tutorial, we’ll use “cds” table from “cdcol” database bundled from XAMPP. “cds” table content looks like this:

cds table content

cds table content

Download NuSOAP here : https://sourceforge.net/projects/nusoap/
Extract “lib” folder from nusoap-0.9.5.zip into your CI “application/libraries/”. We’ll create simple library warper for NuSoap.
Now, enter your CI “application/libraries/” folder and create a new file named “Nusoap_library.php”. Here’s the content:

We’ll use default controller from CI, i.e. Welcome.php. We’ll create two webservice endpoints: one for getting info about a CD based in it’s “id” and one for getting all of CDs.
All of webservice data types definition, registration and implmentation will be placed inside Wellcome controller’s “__construct()” function. Here’s the complete code for “__construct()” function:

The first step is loading the library and configuring your webservice:

We’ll define a new data type for our CD info based on “cds” table structure:

Now, our complex type that will hold list of CDs (array of CDs).

After finished defining all needed data types, we’ll register all needed webservice endpoint.
First endpoint is “getCdInfo” to get info about a CD based on it’s “id”. It takes one parameter (“id”) and return a “Cd” type.

Second endpoint is “getCds” to get all stored CD in the “cds” table. It takes no parameter and return an array of CDs.

Registration is finished, now onto the implementation. Here’s the code:

Pay attention, that I’m using “get_instance()” in order to query the database because we can’t call “$this” inside these endpoint functions.
Next, we’ll edit the default “index()” function, replace it with these:

You may try accessing your webservice from your browser and see how it looks

WSDL schema

WSDL schema

webservice description

webservice description

Now, some fixing!

Before accessing our webservice via a SOAP client, we need to fix NuSoap library. Open “application/libraries/lib/nusoap.php”, go to line 6132 and comment that line so it would look like this:

If you didn’t do this fixing step, you’ll get a blank result when calling an endpoint that return an array of data. Further testing using SoapUI will reveal some error like this:

Fixing is done, now we’ll test it.
For simplicity reason, we’ll put our client inside our Welcome controller. Here’s the code:

As you can see, the code is quite simple, $res1 will contain output from “getCdInfo” endpoint

and $res2 will contain an array of CDs as a result from calling “getCds”.

Here’s the complete source code of our controller, in case you missed something:

[Example] Solving math linear equations with 4 unknown variables

We’ll put some use of Matrix library for CodeIgniter that we already implemented to solve a linear equation.
Let’s say we have linear equations with 4 unknown variables like this:
\begin{array}{rcl}  2a+3b+c+6d &=& 4 \\  4a-2b+0.5c+d &=&2 \\  6a+b-d&=&-1 \\  2a+2b-c+3d&=&4 \end{array}

That linear equation can be written in Matrix notation like this:
\left[ \begin{matrix} 2&3&1&6\\ 4&-2&0.5&1 \\ 6&1&0&-1\\ 2&2&-1&3 \end{matrix} \right] \left[ \begin{matrix} a\\b\\c\\d \end{matrix} \right] = \left[ \begin{matrix} 4\\2\\-1\\4 \end{matrix} \right]

And can be solved by this matrix equation:
\left[ \begin{matrix} a\\b\\c\\d \end{matrix} \right] = \left[ \begin{matrix} 2&3&1&6\\ 4&-2&0.5&1 \\ 6&1&0&-1\\ 2&2&-1&3 \end{matrix} \right]^{-1} \left[ \begin{matrix} 4\\2\\-1\\4 \end{matrix} \right]

And this is how we solve it on php/CodeIgniter:

Quite simple isn’t it?!

PS.: I’m glad I can use my Latex knowledge in this post 😛

Matrix operation library for CodeIgniter

This tips is useful if you someday handling web-based project that involving Matrix handling and you want to implement it using CodeIgniter. We’ll use PHPMath library for this.

  • Download the latest version of phpmath library from here: http://www.phpmath.com/build02/downloads/
  • Open the archive (build02.tar.gz) and see the content, you’ll see “build02” folder, go inside, you’ll see more folders there:
phpmath archive content

phpmath archive content

  • Because we only need the matrix handling and operation library, we just need the “JAMA” folder inside.
  • Extract this “JAMA” folder into your CodeIgniter’s application/third_party/ library, rename it to more meaningful folder, I prefer “phpmath”. Your third_party library will be look like this:
phpmath inside third_party folder

phpmath inside third_party folder

  • Go inside “phpmath” folder, you’ll see more folders there. We don’t need “docs”, “examples” and “tests” folder, we can delete them, your phpmath folder will be like this:
phpmath after deleting unnecessary folders

phpmath after deleting unnecessary folders

And you are done! (In this tips: not like mPDF and PHPExcel library, we don’t need to create a new file/class in the application/libraries folder)

How to use it?

Quite simple, check and test these code samples inside your controller:

Easily integrate mPDF into CodeIgniter Framework

Got the tip from here: http://davidsimpson.me/2013/05/19/using-mpdf-with-codeigniter/
On this previous post, I tell you on how to easily integrate ezPDF into CodeIgniter but the problem is: the PDF generation is not template-based. So, you have to write a lot of codes just to generate simple PDF output.
By integrating mPDF into your CodeIgniter-based site, you can create a PDF output via templating system just as simple as loading a view and pass through some variables into it. mPDF also support CSS, so, you can style your HTML template as cool as you wish.
Here’s the steps-by-steps guide:

  • Download mPDF from http://www.mpdf1.com/mpdf/index.php?page=Download . Remember to download the “Full installation” first (a bit big) and then you can download the patch/upgrade (a little smaller size)
  • Extract it to your application/third_party/ folder of your CodeIgniter. Your third_party folder would be like this:

    mPDF in third_party folder

    mPDF in third_party folder

  • Create a new file in your application/libraries/ name it m_pdf.php and these are the contents:

  • Your library folder would be like this:
    mPDF in the library folder

    mPDF in the libraries folder

  • And it’s done!

How to use it?
Create a CodeIgniter “view file“, let’s named it pdf_output.php:

Now, inside the controller, you can do this: