Jump to content
Server Maintenance This Week. ×
  • entries
    52
  • comments
    2
  • views
    6,389

Introduction to FileMaker Custom Web Publishing (CWP) with PHP


Smef

869 views

The purpose of this lesson is to create a simple PHP page that will make a connection to a FileMaker database hosted on FileMaker Server and pull information that will be displayed as HTML on a webpage.  This will help you get started with the basics of using FileMaker Custom Web Publishing

Objectives:

  • Configure FileMaker Server for CWP access
  • Configure the database for CWP access
  • Create a PHP file which will read data from a database and display the result on a web page
  • Search for a record and display data from the found record
  • Mix HTML and PHP

Requirements:

  • Admin access to FileMaker Server
  • Text editor or a PHP IDE (Integrated Development Environment)
  • Web server with PHP installed

Configuring FileMaker Server

The first step to getting started with CWP is to make sure your FileMaker Server is properly configured.  To do this, you’ll need to log in to your FileMaker Server admin console.

First, make sure that the Web Publishing Engine is enabled.  Scroll down on the Status page and look for the Web Publishing engine option.  Click the slider button to the right of the Web Publishing Engine to enable it.  Look for the green checkmark circle which will indicate if the WPE is successfully running.

WPE Enabled

From there, select “Web Publishing” from the menu on the left.  Select the PHP tab and check the “Enable PHP Publishing” checkbox. This will enable your PHP application to connect and communicate with FileMaker Server.

PHP Publishing Enabled

With the server properly configured for CWP we’re ready to move on to the FileMaker database

Configure the FileMaker Database

In addition to configuring the FileMaker Server for CWP we also need to make sure that our FileMaker Database is ready for access through CWP.  Create a new database and upload it to your FileMaker Server.  I’m going to name my database “Test Database” for this example.

Open the Manage Security dialog and create a user to be used for accessing the database through CWP.  For this example I’m going to create a user named “PHP User” which will be used only for accessing the database through CWP and give this user the [Data Entry Only] privilege set.

Go to the privilege sets tab of the Manage Security window and select the privilege set assigned to your newly created user.  Click Edit and enable the Access via PHP Web Publishing extended privilege set by clicking the checkbox next to it.  Press OK and close the Manage Security window to save your changes.

fmphp-enabled

After making this change you should be able to see that PHP access is enabled for your database from the FileMaker Server admin console.  Check the activity tab and you will see a dot in under the PHP column for your database if you have correctly followed the previous.  This dot indicates that at least one privilege set for the database has the fmphp extended privilege set enabled and that we’re ready to connect to it using PHP.

admin-console-dot

Create a table called “People” in this new database with _kp_ID (primary key), Name, and Favorite Color fields.  Also create layout called “PHP Test” with these fields on the layout.  Create some records and set some values in the name and color fields for testing your work.

table layout

IMPORTANT: Only fields placed on the layout can be accessed through the PHP API.  If you don’t have the field on the layout you won’t be able to get data from it in your PHP code. Be sure to include any fields you want to get or set the values of on your layout.

Setting up the web server

With HTML you’re able to write some code in a text file, save is as an .html file, and then open it in a web browser and see the result.  PHP is different from HTML in that it is code that needs to be executed, rather than just markup that gets read by your browser and displayed as a webpage.  Because of this difference we need to run the code on a web server and can’t just double-click the file to see our work.

You can use any web server with PHP installed to run your code.  Some options you may want to consider are: • FileMaker Server with Web Publishing enabled • A server provided by a web hosting company where you can upload files • On your machine by running a web server

For this tutorial I’ll be using the third option.  I like to use an AMP (Apache MySQL PHP) program for development and testing, as it makes it easy to manage the server.  MAMP (Mac Apache MySQL PHP) or WAMP (Windows Apache MySQL PHP) are free and easy to setup and configure on your local machine. These programs run a web server, php, and a MySQL database server on your computer with minimal setup and configuration, which for our purposes makes it so that you don’t have to worry about uploading or transferring files after you make a change in your code before you can see what the change does.

Download and install MAMP/WAMP (xAMP) and follow the setup options and start the Apache web server through the xAMP console.  Find the web root directory for the new web server running on your machine, which will probably be called htdocs will be located in your xAMP installation directory.  This is where we’ll be putting our files so that they show up on our new web server.  If you’re running xAMP on your local machine you should be able to see the start page by going to http://localhost:8888/ in your browser (this can also be changed to use port 80 in the xAMP console).

Put the FileMaker API on the server

Now that we have our server set up we’re ready to install the FileMaker API.  The first thing we’re going to need to do is grab the FileMaker PHP API (Application Programming Interface) from our FileMaker Server installation.  The PHP API is a set of PHP files that we will use with our code to make the connection to FileMaker Server.

The PHP API is located at:

  • Windws – drive:\Program Files\FileMaker\FileMaker Server\Web Publishing\FM_API_for_PHP_Standalone.zip
  • Mac – /Library/FileMaker Server/Web Publishing/FM_API_for_PHP_Standalone.zip

Copy the zip file to your computer, extract it, and put the extracted files in your web root.  The files should look like this:

api-root

Note: Normally you would put this in what is called an “includes” directory, but we’re going for a quick tutorial of how to get connected. Working with includes folders makes configuration a bit more challenging, so we’re going to skip it for now.

Pick your editor

Writing PHP code can be done in any text editing application, such as Notepad or TextEdit, but there are lots of applications that will help you write your code with features like automatic code completion or code highlighting to help you better see the organization of your code.

There are also more advanced programs which will help you manage files, offer version control, and will spot errors and make recommendations about best practices while you’re writing your code.  They can even help you debug your code in a way similar to the script debugger and data viewer in FileMaker.  These programs are great for coding, but they take a bit of setup work to get everything running.

I recommend installing one of these code editor programs to help make your job easier while writing code:

Simple, free code editors

Integrated Development Environment (IDE) programs

  • IntelliJ (free for personal use)
  • PHPStorm (paid, almost exactly the same as IntelliJ for PHP but cheaper to buy)

Let’s start coding!

With everything in place we’re ready to start writing our code.  Create a new file in editor and save it in your web root as fmdemo.php (in the same directory as filemaker.php. You can name your file anything you like, but give it the .php extension to help the server see that we’re going to be using PHP code in this file.

The first thing to do is to show our web server that the code we’re writing is PHP code.  This is done using the PHP tags.

<?php
?>

Any code we write in-between these tags will be executed as PHP code on the server and won’t be seen by the client’s browser.

Now, tell the server we’re going to be using the FileMaker PHP API and let it know where to find the classes we’ll be referencing.  This sets us up for making calls to it in the next few steps

<?php
include("filemaker.php");

?>

Now, we can set up our connection to FileMaker Server.  The four parameters for the function we’re going to call are in order:

Database name Server address Username password
<?php
include("filemaker.php");

$fm = new FileMaker("Test Database", "fms.mycompany.com", "PHP User", "password");
?>

Let’s prepare to get a random record from our People table.  To do this we’ll use the findAnyCommand method, which does exactly that.  This method takes a single parameter, which is the name layout to pull the record from.

<?php
include("filemaker.php");

$fm = new FileMaker("Test Database", "fms.mycompany.com", "PHP User", "password");

$findCommand = $fm->newFindAnyCommand("PHP Test");

?>

Now we can execute the find we’re prepared and set the result to a variable

<?php
include("filemaker.php");

$fm = new FileMaker("Test Database", "fms.mycompany.com", "PHP User", "password");

$findCommand = $fm->newFindAnyCommand("PHP Test");
$result = $findCommand->execute();

?>

A result from the FileMaker PHP Api will either return a FileMaker_Error object if there is an error or an FileMaker_Result object, which contains an array of records (even if there’s really only one in the result).  It’s important to always check for errors when there is a possibility of getting one, and then handling the error appropriately.  We’ll do this by adding in an if statement to check to see if our result is an error, and then write out the error message before exiting our script since we can’t do anything else useful right now to better handle the error.

<?php
include("filemaker.php");

$fm = new FileMaker("Test Database", "fms.mycompanydomain.com", "PHP User", "password");

$findCommand = $fm->newFindAnyCommand("PHP Test");
$result = $findCommand->execute();

if(FileMaker::isError($result)){
    echo($result->getMessage());
    return;
}
?>

If we didn’t get an error we’re able to view our results and print out some of our fields.  Use the getFirstRecord and getFieldName methods to store the first record in a variable and then read a field value from the record.  I’ll retrieve the Name field from the People record that was returned and write it out using the echo function.

<?php

include("filemaker.php");

$fm = new FileMaker("Test Database", "fms.mycompany.com", "PHP User", "password");

$findCommand = $fm->newFindCommand("PHP Test");
$findCommand->addFindCriterion("name", "Adam");
$findCommand->setRange(0, 1);

$result = $findCommand->execute();

if (FileMaker::isError($result)) {
    echo($result->getMessage());
    return;
}

$record = $result->getFirstRecord();

$name = $record->getField("Name");
$color = $record->getField("Favorite Color");

echo("$name - $color");

?>

Try loading this page in your browser now by going to http://localhost:8888/fmdemo.php (use the correct address of your server if it isn’t localhost:8888).  If everything was done correctly you should see one of the values from the Name field that you entered in your database.  Since we’re using the findAnyCommand method the value will be randomly selected from one of the records in your database, so it will change every time you reload the page.

Find a specific record

Let’s modify our code a little bit to find a specific person and display their name and favorite color.  We can use the findCommand method instead of findAnyCommand to create a find request just like we would in FileMaker.  We’ll also add a addFindCriterion call to the $findCommand object to specify the field we’re going to search on, also very similar to a FileMaker script.

It’s important to set a “range” value here, which will limit the maximum number of records returned.  If you don’t limit the range and your found set is very large it could make your code very slow or even possibly cause your script to crash due to your server running out of memory.  Set a range using the setRange method, which takes two parameters – a number of records to skip and a number of records to return in the result.  Set these to 0 and 1 for now to not skip any records and only return one record in your result.

Finally, let’s show both the person’s name and their favorite color at the same time by changing our echo at the bottom of our code.  I’m also going to assign the values to a variable before echoing them out to make it a bit easier to read what the echo statement should be writing.  PHP will actually evaluate variables that are placed inside double quotes, so you don’t have to worry about concatenation in this case.

<?php

include("filemaker.php");

$fm = new FileMaker("Test Database", "fms.mycompany.com", "PHP User", "password");

$findCommand = $fm->newFindCommand("PHP Test");
$findCommand->addFindCriterion("name", "Adam");
$findCommand->setRange(0, 1);

$result = $findCommand->execute();

if (FileMaker::isError($result)) {
    echo($result->getMessage());
    return;
}

$record = $result->getFirstRecord();

$name = $record->getField("Name");
$color = $record->getField("Favorite Color");

echo("$name - $color");

?>

Mix your HTML and PHP

You can mix HTML and PHP on a page to make a webpage with dynamic data from your database.  HTML can be placed outside of the <?PHP ?> tags and will be sent to the browser like normal.  You can also have multiple PHP tags on the page to mix your code and your HTML as needed.

<html>
<body>

<?php $name = "Adam" ?>
<h1>Favorite Color of <?php echo($name) ?></h1>


    <?php
    include("filemaker.php");

    $fm = new FileMaker("Test Database", "fms.mycompany.com", "PHP User", "password");

    $findCommand = $fm->newFindCommand("PHP Test");

    $findCommand->addFindCriterion("name", $name);
    $findCommand->setRange(0, 1);

    $result = $findCommand->execute();

    if (FileMaker::isError($result)) {
        echo($result->getMessage());
        return;
    }

    $record = $result->getFirstRecord();

    $color = $record->getField("Favorite Color");

    echo("We searched for $name and found the favorite color to be $color");

    ?>

</body>
</html>

That should cover the basics for using FileMaker CWP.  Let me know if this has been useful for you, or what you would like to see from me in the future.  Be sure to share this article using the buttons below if you think this would be helpful for other people as well.


View the full article

0 Comments


Recommended Comments

There are no comments to display.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.