Get Started with PHP in FileMaker Pro

Get Started with PHP in FileMaker Pro

Leverage the power of PHP, a server-side scripting language, to speed and secure Web queries.

By Bob Bowers, Contributing Editor

PHP is a popular, powerful, and free middleware tool for building Web applications. It’s a server-side embedded scripting language, much like other middleware tools such as Cold Fusion, ASP, JSP, and Lasso. You can configure PHP for use with a variety of Web servers on a variety of platforms, including Linux, Windows, and Mac OS X.

And you can use it to publish FileMaker Pro data to the Web.

You’ve likely heard or read something about the XML capabilities of FileMaker Pro 6. In addition to XML import and export, the Web Companion can output XML in response to an appropriately structured query. It’s this ability that lets PHP communicate with FileMaker Pro.

It’s beyond the scope of this article to teach you how to be a PHP developer. My goal is to teach you enough to whet your appetite and hopefully inspire you to give PHP a try on your next Web project. At the end of the article, I’ll list some resources you can consult for help with configuring and using PHP. Dozens of books on PHP are available as well.

How it works

PHP has built-in functions for sending requests to Web servers and processing the results. Using those functions, PHP can send a query to the Web Companion and get results back in XML. Now, if you had to assemble these queries and parse the incoming XML manually, you’d likely give up on PHP pretty quickly. Thankfully, there’s an easier way. Chris Hansen, a FileMaker developer, has written a class for PHP that does all the hard work for you. The class, which he calls “FX,” is free from his Web site, Using FX means you don’t have to know a lick of XML to get PHP to interact with FileMaker Pro. You just have to know how to use the functions Chris provides in the class, and that’s fairly easy. The documentation provided with the download is thorough and easy to follow.

Try it out

Often, the best way to understand something new is to play through a simple scenario. After you have a working model, it’s easier to go back and learn variations and exceptions. Let’s look, then, at a simple, typical Web publishing scenario using PHP. Imagine you have a FileMaker Pro database (Employee.fp5) containing employee names and phone numbers. Your goal is to make search and results pages so users can access the database via a browser over the company intranet.

A typical hardware configuration for FileMaker/PHP solutions is to have two machines: a Web server running Apache or IIS, with PHP installed, and a separate machine running FileMaker Unlimited. Your databases must be open on that second machine (locally or as the guest of FileMaker Server) and shared to the Web Companion. To have FileMaker Unlimited respond to requests for XML, the only configuration required is to have the Web Companion enabled. In the Web Companion configuration screen, however, it’s a good idea to restrict access to only the IP address of the Web server. Limiting the traffic to that single IP address secures your data-bases from being accessed directly from a Web browser by hackers.

To test that you have the Web Com-panion configured properly and it will respond to requests for XML, open a browser (on any machine on the network) and type the following URL:

http:// /FMPro?-format=-fmp_xml&-dbnames

In this request, you’re simply asking the Web Companion at that IP address for a list of accessible databases, formatted as XML. If you’re using a recent copy of Microsoft Internet Explorer, you should see the XML result in your browser. With other browsers, you might have to View Source to see the response.
Figure 1: Web interaction topology — In a typical FileMaker/PHP solution, Web users submit requests to a Web Server, which in turn requests information via XML from a FileMaker Unlimited machine.

All of the HTML pages (both static and those with embedded PHP code) reside on the Web server. Users interact with this machine rather than directly with your FileMaker Unlimited machine. Figure 1 illustrates this topology.

Any requests the Web server receives for pages with a .php extension are handed off to PHP for processing. If necessary, PHP talks to the Web Companion on your FileMaker Unlimited machine to send or retrieve data. PHP then passes back plain HTML to the Web server, which is, in turn, sent to the user’s browser. Users never see the PHP code embedded in your HTML pages. They’ll only see the post-processing results, which is simply HTML. So, take the following page for example, which I’ll call test.php:

$a = 5;

$b = 8;

$c = $a * $b;

echo “If you multiply $a and $b, you’ll get $c”;


When a browser requests test.php, the Web server notices the .php extension, and hands the document off to PHP for processing. PHP executes the program code contained between the symbols. Here, you’re asking PHP to multiply 5 and 8, then write a line of output. After execution, PHP hands back the processed document to the Web server. Thus, what the Web server sends back to the browser is simply:

If you multiply 5 and 8, you’ll get 40

Now that you’ve tested both the Web Companion and the Web server independently, it’s time to write your search and results pages. As with other embedded languages (e.g., CDML, Lasso, Cold Fusion, ASP, JSP), you don’t have to have any special tools to embed PHP code in an HTML document. Just about any Web-authoring tool or text editor does the trick.

The page where users enter their search criteria is just a plain old HTML form. To display that form to the user, there’s no need to have PHP talk to the database. It’s only when the form is submitted that PHP has to perform a find in the database. So, keeping formatting to a minimum for now, your search.html page consists of the following:

XYZ Corp Employee Directory

Last Name:


First Name:


Figure 2: Search page — The search page itself is just a plain HTML form, but it contains instructions to send the user’s entries to a PHP page for processing when the form is submitted.

Take a look at the form tag on line 7. A form’s “action” attribute tells the Web server what to do with the form after it has been submitted. In this case, it instructs the Web server to send all the form data to something called findEmp.php, the next page you’re going to write. Figure 2 shows the search page as it would appear to users.

Now you write the code for the search results page, which I’ll call findEmp.php. Remember, the .php extension tells the Web server this page contains embedded PHP code.

include (”FX.php”);

$query = new FX(”″, “591″); // IP address and

// port of the Web

// Companion

$query->SetDBData (”Employee.fp5″);

$query->AddDBParam (’Last Name’, $_POST[‘LastName’]);

$query->AddDBParam (’First Name’, $_POST[‘FirstName’]);

$result = $query->FMFind();

if ($result[‘foundCount’] == 0) {

echo “No records matched your selection. Please try


} else {

echo “Search Results


echo “

echo “


foreach ($result[‘data’] as $recordKey =>$recordData) {

echo “

echo ”


echo ”


echo ”


echo “



echo “
Last Name First

Phone Number
“.$recordData[‘Last Name’][0].” “.$recordData[‘First Name’][0].” “.$recordData[‘Phone Number’][0].”




Search Again

The first PHP function invoked is an include function. By including the FX.php document, you’ll have access to the functions in the FX library. So, to use FX, all you do is upload FX.php to your Web server and include it at the top of each page you want to connect to a database.

The next chunk of PHP code provides FX with parameters it must have to query the Web Companion. The IP address and port of the FileMaker Unlimited machine are specified as part of the instantiation of a new FX object, called $query here. (FX, by the way, is written using object-oriented techniques. PHP supports an object-oriented style, but doesn’t mandate it.) The SetDBData function then specifies the database (and optionally, layout) to query. The two AddDBParam functions turn the user’s entries from the search page into search arguments. Because you accessed this page as the result of a form submission, this page has access to all the form parameters submitted by the user. PHP stores this in an array called $_POST.

Figure 3: Finding Fred — The results of your search for “Flintstone.”

In the next line, you invoke the FMFind function and place the results in a variable called, aptly enough, $result. It’s at this point that PHP makes a connection to FileMaker. FileMaker responds with a stream of XML (because that’s the format PHP requests), and the FMFind function parses the incoming data and puts it into a big, multi-dimensional array. One of the items in that array contains the number of records returned by the query. You use a simple conditional statement to evaluate the number of records, then either output to the browser window a “No records found” message, or proceed to loop through the search results (using the foreach function) and output them to the screen. Figure 3 shows what the search results might look like after searching for “Flintstone” in the Employee database.

With that, this simple example is complete. If you’ve never seen PHP code before, it might seem a bit strange at first. But really, if you have any experience with procedural or object-oriented programming, you’ll pick up PHP syntax quickly.

In addition to searching for records, you can use FX to submit requests to the Web Companion that will add, edit, or delete records from a database. You can also submit multiple requests in a single document. That’s akin to using the inline functions of Lasso or CDML.

Worth considering

I hope I’ve convinced you that PHP is worthy of your consideration as a Web development tool. PHP is powerful, fast, full-featured, well-supported, and easy to learn. To top it off, it’s free. Drop Chris Hansen a note thanking him for his generous contribution to the FileMaker community.


These resources will get you started with FX, PHP, and Web development in general. — Chris Hansen’s FX site. — Includes installers, tutorials, and online documentation.

Advanced FileMaker Pro 6 Web Development, by Bob Bowers and Steve Lane (Wordware Publishing, 2003).

Get Started with PHP
No reader comments … yet.

Posted by: Posh Web Solutions


~ by poshwebsolutions on December 3, 2008.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: