Monday, 6 July 2009

Working With The Authorize.net Customer Information Manager (CIM), Part 1: Overview

I’m going to spend several posts discussing authorize.net’s Customer Information Manager, a Web service for storing and retrieving personally identifiable information about the people who place credit card orders on your Web site.

Today, I’m focusing solely on an overview of CIM: What it is, how it works, why it works that way, and approaches to integrating CIM into your custom storefront / ordering systems. In future posts, I will discuss actual implementation via PHP and MySQL; my intent is to use 2-3 posts to cover the process, but it may take more or less. My intent is to post every day, but there may be a delay of a day or two between posts.

(I’m uncertain on the number and timing of posts on this topic because I’ll be blogging about implementation as I implement CIM for the first time. And lest that gives you pause, I’ve extensively reviewed the documentation, tested the basics of using the service, and have over 10 years’ experience with PHP / XML / MySQL. And because I’m doing the actual implementation for pay, I will have extensively tested the solution for elegance, reliability and security.)

What Is A Web Service?

The Customer Information Manager is a Web service. In its most common implementation on the World Wide Web, you send a Web service an XML document describing information you want, and the service responds with an XML document that contains the information you requested. (Not all Web services work this way; there are many kinds of Web services out there. But rather than bog down the point, let’s stick with this basic description.)

We can therefore think of a Web service as a remote database; the Customer Information Manager Web service is, in fact, a way to get data into and out of a remote database.

In the case of a Web service, rather than connecting to a local database, writing a SQL query and asking it to send us a result set (or Boolean false on failure), such as we would do in PHP / MySQL, we instead write an XML document that provides our authentication credentials and describes the data we want, send that to the CIM Web service via cUrl (or, if your server allows it, fopen), and get back from the CIM an XML document that contains the records we want (or an error code describing any problems encountered).

In other words, for most intents and purposes, the CIM Web service works exactly like a database, only different. So why in the world does authorize.net bother with a Web service, rather than simply giving you access to their database directly?

Continue reading: Working With The Authorize.net Customer Information Manager (CIM), Part 1: Overview »

Saturday, 20 June 2009

JavaScript Function Arguments: They’re An Array And You Can Treat Them As Such

One of the nicest features of JavaScript is the ability to treat function arguments as an array. We all know how to write a function that takes a known number of arguments:

function foo(bar) {
	alert(bar);
}

function getArea(shape, measure, unit) {
	var output = 'The area of a ' + measure + ' ' + unit + ' ' + shape + ' is ';
	switch(shape) {
		case 'square':
			output += (measure * measure);
			break;
		case 'circle':
			output += (3.14 * (measure * measure));
			break;
		default:
			output += 'unknown to this function.';
	}
	alert(output);
}

But sometimes, your script will require you to pass some unknown number of parameters to a function. So how do you deal with that?

Luckily, JavaScript functions treat all of their arguments as an array. So, if you’re not certain how many arguments your function will take, don’t declare them in your function code; simply use the reserved word arguments to iterate the array of arguments passed to the function.

Continue reading: JavaScript Function Arguments: They’re An Array And You Can Treat Them As Such »

Wednesday, 13 May 2009

Using JavaScript To Perform A Task Traditionally Solved With Server-Side Scripting

Asked recently on Yahoo! Answers:

Does anyone know how to automatically update a birthday list on a web page?
Our web page has a section that displays all staff birthdays for the week. This section is updated manually every Monday morning. Is there a way to have this update done automatically everyday. Must display all birthdays for that day and all birthdays for the next 6 days. The format required is:
Date (eg: 13 May)
Name Surname, Department (a new line entry for each employee with a birthday on that day)
Above repeated for each of the other 6 days.

This question presents me with an excellent opportunity to discuss a topic I’ve not really had a chance to broach before: namely, the decision process on whether to use JavaScript or a server-side scripting language, such as PHP or ASP.NET, to accomplish a given task.

Using JavaScript to accomplish a traditional server-side scripted, data-driven task is becoming increasingly popular, especially as ECMAScript standards are more widely adopted, libraries such as jQuery gain prevalence and supporting technologies, such as Google Gears, remove many of the traditional roadblocks in writing client-side scripts for stateless Web applications.

In this case, we have a problem that can be solved either client-side (JavaScript) or server-side (in this case, PHP / MySQL). Which to choose is largely a function of scale: If we’re dealing with hundreds or thousands of employee birthdays we want to display, we’re probably best off using a server-side technology; but if we have fewer employees, it might make sense to simply use JavaScript, and push the workload from the server and onto our visitors.

I’ll demonstrate both the traditional PHP / MySQL approach, as well as a JavaScript approach.

Continue reading: Using JavaScript To Perform A Task Traditionally Solved With Server-Side Scripting »

Sunday, 2 November 2008

The Trouble With PHP’s Weak Data Types: An Example Examined And Explained

Asked recently on Yahoo! Answers:

Multiple nested MySQL functions in PHP?

I was going through code today, trying to make some things more compact. The application worked without problems, so I knew that mysql error statements were superfluous.

This is the type of statement I was changing, I figured that I’d remove the seemingly unnecessary $result2 variable

$result2 = mysql_query($carts) or die(mysql_error());<br />
while ($row2 = mysql_fetch_array($result2)) {<br />
	echo "<option>$row2[0]</option>";<br />
}

so this is what I changed it into (I basically replaced where it said $result2, to what $result2 had contained, and removed the error check)

while ($row2 = mysql_fetch_array(mysql_query($carts))) {<br />
	echo "<option>$row2[0]</option>";<br />
}

but this code returned infinite loops, much to my surprise. Why is it doing this? is there a way around it?

Recently, I wrote about how PHP’s weak data types often can lead to problems for new programmers who don’t understand the difference between null, empty and zero-length variables. Here’s another opportunity to expose why strong data typing is essential for best programming practices, and to show how PHP’s weak data types — normally, a source of comfort for beginning programmers — can be the source of extensive frustration.

Continue reading: The Trouble With PHP’s Weak Data Types: An Example Examined And Explained »

Wednesday, 22 October 2008

The Difference Between Null, Empty And Zero-Length Data / Strings

A common problem faced by new programmers is understanding the difference between null, empty and zero-length variables, especially when working with database records.

While, for most intents and purposes, the three things have the same effect — either you have some data you can work with, or you don’t — they arise from different circumstances. Understanding how null, empty and zero-length are different can help you avoid data errors in your programs.

The short version is this: If a variable simply doesn’t exist — usually because it hasn’t been declared, but sometimes because it hasn’t been assigned a value — it’s null. If the variable exists but contains no data, it’s empty. And if a structured data variable, such as an array, exists but doesn’t contain any items, it’s zero-length.

An analogy is in order.

Think of a database as a house. A house has rooms, and in each of these rooms are furniture and accessories that are appropriate to that room.

For example, the kitchen has a stove, refrigerator and sink. The bedroom has a dresser, bed and armoire. The living room has a couch, television and coffee table.

Continue reading: The Difference Between Null, Empty And Zero-Length Data / Strings »