gzip nusoap requests

Recently for a project involving travelport xml api integration, we badly needed the soap requests to be gzipped, since the technical support people suggested. Also we were aware of the benefits of using gzEncoded data when transmitting through the Internet. For the same we checked the wid internet searching with all sort of combinations of gzip nusoap request. Speaking from the inner view, we already had developed a handful of classes to abstract the SubmitXML api provided by Galelio Travelport. And frankly were reluctant to trash all the code already written.
At this point, we swam through the whole code of nusoap.php; should say thanks to easyeclipse, and made some minor tweaks here and there. The following was all that was required, though I doubt if this would be a generic solution, this definitely serves our purpose and the versions we used are 0.7.3/Revision: 1.114.

in class soap_transport_http added property gzipRequests (line 2148)
var $gzipRequests = true; // gzip any requests..

in function buildPayload at the top (line 2799)
if($this->gzipRequests){
$data = gzencode($data);
$this->setHeader("Content-Encoding",'gzip');
}

in function sendRequest (about line 2874), just in case the request is using curl
if($this->gzipRequests){
$data = gzencode($data);
}

Using Foreign Keys in MySQL

On the article at php|architect, Ligaya Turmelle explains you how to handle Foreign Keys in MySQL so they can serve your whims.

No, foreign keys aren’t from Brazil or Italy or even the US. Though they can be a bit strange to those who do not understand them, have no fear. We are here to teach you how to talk to them so they can serve your whims. So what are foreign keys exactly?

Not to put too fine a point on it, they are what make a relational database “relational.” They are the links between tables that keeps everything connected. They are what allows you to put a customer in one table and their order in another and the products they are ordering in a third table so the database has minimal data redundancies.

She continues with the article with a good example. Read it

Building File Uploaders :: Object Method

On DevShed there’s a new tutorial showing how to build file upload functionality into your scripts.

If you’re a PHP developer who has built a certain number of web applications, then it’s quite probable that you’ve already worked with HTTP file uploads. […] First I’m going to teach you how to handle file uploads using a procedural approach, and then, with the topic well underway, by way of the object-oriented paradigm.

The introduce the beginners out there to the $_FILES array (a superglobal) that contains the details about the file(s) that have been submitted. Next comes the construction of a simple form and how to handle the submission on the PHP side.

php wrapper for zapatec tree widget

To quote the zapatec site ” The Zapatec DHTML Tree is an easy, attractive, and versatile way to display information. You can use the DHTML tree as a menu, a site map, or a way to display your data. Our DHTML Tree works in many different browsers, but if visitors use older browsers, they will still be able to see and use the underlying HTML code. ”

I did find this very good, and in the sense that it provides a method for the Search Engines to see all the links. Recently in an administrative backend, I needed to provide a method to browse multilevel deep category subcategory, where they add new products, to their existing line, for which I did choose the lite version of zapatec suite.

The generation of nested ul/li tags for the tree was a real pain for me, from the existing database. And I thought about a wrapper which could do the job if provided with an array of data rows. Hence I made the class-tree.php. The included file is having an implementation sample which will need the lite version from zapatec site.

The sample implementation with the help of zapatec could generate a tree as shown from the code attached below.

tree screen shot

from

The Data

To start with the implementation, we create an instance of the zptree after including the class-tree.php, and passing the path of zapatec js files.

create instance

Then initiate the tree data using $catTree->treeData = array(); Then go along selecting the data from mysql using the select statement select category_id as id,category_name,parent_category_id from category order by parent_category_id, inside a while loop, the tree data is added to the member array, while($rd = $db->fetch_array($rs)) $catTree->treeData[] = $rd;

The attached class-tree.php also has an example implementation at the end, which should be removed before using in production.

Please excuse me about the sloppy documentation, since I am no good at explaining things.

class-tree.php

PHP – Quotes or No quotes in associative Array?

Our team was having a debate on why we should put a ‘ (single quote) when referencing arrays in php. Well what is the problem if we use $_SESSION[key], instead of $_SESSION[‘key’].

Well when you think that when encountering non functions or keywords, php tries to evaluate it as a declared constant. So referencing $_SESSION[‘key’] would be better against $_SESSION[key], unless we have defined the constant key and want to use the value of key.

Single Quotes vs Double Quotes!

I recommend using ‘ (single quotes) when programming with PHP; Always use ‘ (single quotes) unless you need the features of ” (double quotes). You might think it’s much easier to write code as:

echo "Today is the $day of $month";

However, using single quotes forces variables to be outside the quotes; instead when you use the ” (double quotes), forces php to evaluate the string, where as with ‘ (single quotes), the string content is taken as such.

error_reporting(E_ALL); make code faster

The single most important thing I tell people who use PHP is to turn error reporting to its maximum level. Why would I want to do this? Generally the error reporting is set at a level that will hide many little things like:

  • declaring a variable ahead of time,
  • referencing a variable that is not available in that segment of code, or
  • using a define that is not set

These factors might not seem like that big a deal — until you develop structured or object oriented programs with functions and classes. Too often, writing code with the error reporting turned up high would cost you hours as you scoured long functions that didn’t work because a variable was misspelled or not accessible.

PHP won’t tell you anything in that case it’ll just create the new variable for you and initialize it to zero. The remedy is to put the following line at the top of every PHP document as you develop:

error_reporting(E_ALL);

It simply forces the error reporting to be at its highest level. Try putting this line in other PHP programs, and more often than not you’ll receive a barrage of warning messages that identify all the potentially wrong elements of the code.

Eventually this when enabled, will force you to write cleaner, structured and fast code.