Filemaker Server 9: Custom Web Publishing With PHP
Filemaker Server 9: Custom Web Publishing With PHP
Filemaker Server 9: Custom Web Publishing With PHP
Edition: 01
Contents
About this guide 7
Chapter 1
Introducing Custom Web Publishing 9
About the Web Publishing Engine 10
How a Web Publishing Engine request is processed 10
Custom Web Publishing with PHP 11
Custom Web Publishing with XML and XSLT 11
Comparing PHP to XML and XSLT 12
Reasons to choose PHP 12
Reasons to choose XML and XSLT 12
Chapter 2
About Custom Web Publishing with PHP 13
Key Features in Custom Web Publishing with PHP 13
Web Publishing Requirements 13
What is required to publish a database using Custom Web Publishing 13
What web users need to access a Custom Web Publishing solution 14
Connecting to the Internet or an intranet 14
Manually installing the FileMaker API for PHP 14
Where to go from here 15
Chapter 3
Preparing databases for Custom Web Publishing 17
Enabling Custom Web Publishing with PHP for databases 17
Creating layouts for Custom Web Publishing with PHP 17
Protecting your published databases 18
Accessing a protected database 18
Publishing the contents of container fields on the web 19
How web users view container field data 20
FileMaker scripts and Custom Web Publishing 20
Script tips and considerations 20
Script behavior in Custom Web Publishing solutions 22
4 FileMaker Server Custom Web Publishing with PHP
Chapter 4
Overview of Custom Web Publishing with PHP 23
How the Web Publishing Engine works with PHP solutions 23
General steps for Custom Web Publishing with PHP 23
Using FileMaker PHP Site Assistant to generate PHP websites 24
Before using PHP Site Assistant 25
Starting PHP Site Assistant 25
Using PHP Site Assistant generated sites 26
Chapter 5
Using the FileMaker API for PHP 27
Where to get additional information 27
FileMaker API for PHP Reference 27
FileMaker API for PHP Tutorial 27
FileMaker API for PHP Examples 28
Using the FileMaker class 28
FileMaker class objects 28
FileMaker command objects 28
Connecting to a FileMaker database 29
Working with records 29
Creating a record 29
Duplicating a record 30
Editing a record 30
Deleting a record 30
Running FileMaker scripts 31
Obtaining the list of available scripts 31
Running a ScriptMaker script 31
Running a script before executing a command 31
Running a script before sorting a result set 31
Running a script after the result set is generated 31
Script execution order 32
Working with FileMaker layouts 32
Using portals 33
Listing the portals defined on a specific layout 33
Obtaining portal names for a specific result object 33
Obtaining information about portals for a specific layout 33
Obtaining information for a specific portal 33
Obtaining the table name for a portal 33
Obtaining the portal records for a specific record 33
Creating a new record in a portal 34
Deleting a record from a portal 34
| Contents 5
Chapter 6
Staging, testing, and monitoring a site 43
Staging a Custom Web Publishing site 43
Testing a Custom Web Publishing site 43
Monitoring your site 44
Using the web server access and error logs 44
Using the Web Publishing Engine application log 44
Using the Web Server Module error log 45
Using the Web Publishing Core internal access log 45
Troubleshooting your site 45
Appendix A
Error codes for Custom Web Publishing with PHP 47
Error code numbers for FileMaker databases 47
Error code numbers for PHP components 53
Index 55
6 FileMaker Server Custom Web Publishing with PHP
Preface
About this guide
This guide assumes you are experienced with PHP, developing websites, and using FileMaker® Pro to create
databases. You should understand the basics of FileMaker Pro database design, and should understand the
concepts of fields, relationships, layouts, portals, and containers. For information about FileMaker Pro, see
FileMaker Pro Help.
This guide provides the following information about Custom Web Publishing with PHP on FileMaker Server:
1 what is required to develop a Custom Web Publishing solution using PHP
1 how to publish your databases using PHP
1 what web users need in order to access a Custom Web Publishing solution
1 how to use the FileMaker API for PHP to obtain data from databases hosted by FileMaker Server
Important You can download PDFs of FileMaker documentation from www.filemaker.com/downloads.
Any updates to this document are also available from the website.
Using the FileMaker Server Web Publishing Engine for Custom Web Publishing
Products.fp7 Customers.fp7
1 2 3
Web Server Web Publishing
Engine
Web Web Server Module
Browser Database
Server
FM API and PHP code XSLT Stylesheets
6 5 4
Important Security is important when you publish data on the web. Review the security guidelines in
FileMaker Pro User’s Guide, available as a PDF file from www.filemaker.com/downloads.
Chapter 1 | Introducing Custom Web Publishing 11
Note For more information about Custom Web Publishing with XSLT, see FileMaker Server Custom Web
Publishing with XSLT.
Chapter 2
About Custom Web Publishing with PHP
Custom Web Publishing with PHP lets you use the PHP scripting language to integrate data from FileMaker
databases with your customized web page layouts. Custom Web Publishing with PHP provides the
FileMaker API for PHP, which is a PHP class created by FileMaker that accesses databases hosted by
FileMaker Server. This PHP class connects to the FileMaker Server Web Publishing Engine and makes data
available to your web server’s PHP engine.
1 PHP installed on the web server. FileMaker Server can install the supported version of PHP 5, or you can
use your own version. The minimum required version of PHP is 4.3. For information about PHP, see
http://php.net. The version of PHP installed on the web server must support cURL (client URL library)
functions. For information about cURL, see http://php.net/curl.
Important When you install the FileMaker Server supported version of PHP 5, it does not show up in the
Mac OS X Server Admin tool; it is not supposed to be listed. If you use the Mac OS X Server Admin tool
to turn on PHP, you disable the FileMaker Server supported version of PHP 5, and enable your own
version of PHP.
1 one or more FileMaker Pro databases hosted by FileMaker Server.
1 the IP address or domain name of the host running the web server
1 a web browser and access to the web server to develop and test your Custom Web Publishing solution
For more information, see FileMaker Server Getting Started Guide.
To make the FileMaker API for PHP accessible to your PHP scripts
When you installed FileMaker Server, the FileMaker API for PHP package was included as a .zip file in the
following location:
1 For IIS (Windows):
<drive>:\Program Files\FileMaker\FileMaker Server\Web Publishing\FM_API_for_PHP_Standalone.zip
where <drive> is the drive on which the web server component of your FileMaker server deployment
resides.
1 For Apache (Mac OS): /Library/FileMaker Server/Web Publishing/FM_API_for_PHP_Standalone.zip
The FM_API_for_PHP_Standalone.zip file contains a file called FileMaker.php and a folder called
FileMaker. Unzip the file and copy the FileMaker.php file and the FileMaker folder to either of these
locations:
1 the web server root folder where your PHP scripts reside.
1 For IIS (Windows): <drive>:\Inetpub\wwwroot where <drive> is the drive on which the Web Publishing
Engine component of your FileMaker server deployment resides.
1 For Apache (Mac OS): /Library/WebServer/Documents
1 one of the include_path directories in your PHP installation. The default location for Mac OS X is
/usr/lib/php.
Important When defining account names and passwords for Custom Web Publishing solutions, use
printable ASCII characters; for example, a-z, A-Z, and 0-9. For more secure account names and
passwords, include certain non-alphanumeric characters such as an exclamation point (!) or percent sign
(%). Colons (:) are not allowed. For details on setting up accounts, see FileMaker Pro Help.
4. Using the FileMaker Server Admin Console, verify that hosting is properly configured for the database,
and that it is accessible to the FileMaker Server. See FileMaker Server Help for instructions.
Note Because Custom Web Publishing with PHP does not use persistent database sessions, references to
an external ODBC data source in the FileMaker Pro relationships graph may limit the functionality available
to your PHP solution. If your database accesses data from an external SQL data source, you may not be able
to update the external table’s record data.
For more information on securing your database, see the FileMaker Pro User’s Guide, available as a PDF
file from www.filemaker.com/downloads.
The following list summarizes the process that occurs when using Custom Web Publishing to access a database:
1 If no password has been assigned for a Custom Web Publishing-enabled account, the PHP solution needs
to provide the account name only.
1 If the Guest account is disabled, then the PHP solution needs to provide an account name and password.
The PHP solution can either prompt the web user for the account name and password, or it can store the
account name and password in the PHP code. The account must have the extended privilege fmphp enabled.
Chapter 3 | Preparing databases for Custom Web Publishing 19
1 If the Guest account is enabled and has the fmphp extended privilege enabled:
1 The PHP solution does not need to prompt web users for an account name and password when opening
a file. All web users are automatically logged in with the Guest account and assume the Guest account
privileges.
1 The default privilege set for Guest accounts provides “read-only” access. You can change the default
privileges, including Extended Privileges, for this account. See FileMaker Pro Help.
1 The PHP solution can use the Re-Login script step to allow users to log in using a different account (for
example, to switch from the Guest account to an account with more privileges). See FileMaker Pro Help.
However, because PHP connections do not use persistent database sessions, the PHP solution must store
the account name and password to use them for each subsequent request.
Note By default, web users cannot change their account passwords from a web browser. You can enable
this feature for a database using the Change Password script step, which allows web users to change their
passwords from browser. See FileMaker Pro Help.
The FileMaker API for PHP tutorial includes additional examples showing how to use container fields. See
“FileMaker API for PHP Tutorial” on page 27.
In addition, if a container field stores a file reference, then you must follow these steps to publish the
referenced files using the Web Publishing Engine:
1. Store the container object files in the Web folder inside the FileMaker Pro folder.
2. In FileMaker Pro, insert the objects into the container field and select the Store only a reference to the file
option.
3. Copy or move the referenced object files in the Web folder to the same relative path location in the root
folder of the web server software.
1 For IIS (Windows): <drive>:\Inetpub\wwwroot where <drive> is the drive on which the Web Publishing
Engine component of your FileMaker server deployment resides.
1 For Apache (Mac OS): /Library/WebServer/Documents
20 FileMaker Server Custom Web Publishing with PHP
Notes
1 For container objects stored as file references, your web server must be configured to support the MIME
(Multipurpose Internet Mail Extensions) types for the kinds of files you want to serve, such as movies.
Your web server determines the support for the current MIME types registered for the Internet. The Web
Publishing Engine does not change a web server’s support for MIME. For more information, see the
documentation for your web server.
1 All QuickTime movies stored in a container field are stored by reference.
1 The Web Publishing Engine does not support movie file streaming. Web users must download the entire
movie file in order to view the movie.
1 In the ScriptMaker Edit Script window, select Run script with full access privileges to allow scripts to
perform tasks for which you would not grant access by an individual. For example, you can prevent users
from deleting records by restricting their accounts and privileges, but still allow users to run a script that
would delete certain types of records under conditions predefined within the script.
1 If your scripts contain steps that are unsupported—for example, steps that are not web-compatible—use
the Allow User Abort script step to determine how subsequent steps are handled:
1 If the Allow User Abort script step option is enabled (on), unsupported script steps stop the script from
continuing.
1 If Allow User Abort is off, unsupported script steps are skipped and the script continues to execute.
1 If this script step is not included, scripts are executed as if the feature is enabled, so unsupported script
steps stop scripts.
1 Some scripts that work with one step from a FileMaker Pro client may require an additional Commit
Record/Request step to save the data to the host. Because web users don’t have a direct connection to the
host, they aren’t notified when data changes. For example, features like conditional value lists aren’t as
responsive for web users because the data must be saved to the host before the effects are seen in the
value list field.
1 Any script that modifies data should include the Commit Record/Request step, because data changes
won’t be visible in the browser until the data is saved or “submitted” to the server. This includes several
script steps like Cut, Copy, Paste, and so on. Many single-step actions should be converted into scripts
to include the Commit Record/Request step. When designing scripts that will be executed from a web
browser, include the Commit Record/Request step at the end of a script to make sure all changes are
saved.
1 To create conditional scripts based on the type of client, use the Get(ApplicationVersion) function. If the
value returned includes “Web Publishing Engine” you know that the current user is accessing your
database with Custom Web Publishing. For more information on functions, see FileMaker Pro Help.
1 After converting your files, open each script that web users might run and select Indicate web compatibility
to ensure that the script will execute properly with Custom Web Publishing.
22 FileMaker Server Custom Web Publishing with PHP
4. Copy or move your site directory structure and files to the web server root folder.
1 For IIS (Windows): <drive>:\Inetpub\wwwroot where <drive> is the drive on which the Web Publishing
Engine component of your FileMaker server deployment resides.
1 For Apache (Mac OS): /Library/WebServer/Documents
5. If a database container field stores a file reference instead of an actual file, the referenced container object
must be stored in the FileMaker Pro Web folder when the record is created or edited. You must copy or
move the object to a folder with the same relative location in the root folder of the web server software.
See “Publishing the contents of container fields on the web” on page 19.
6. Make sure that security mechanisms for your site or program are in place.
7. Test your site using the same accounts and privileges defined for web users.
8. Make the site available and known to users. The URL that the web user enters follows this format:
http://<server>/<site_path>
Note PHP 4 and PHP 5 use Latin-1 (ISO-8859-1) encoding. FileMaker Server returns Unicode (UTF-8)
data. Use the FileMaker Server Admin Console to specify the default character encoding for your site. For
PHP sites, you can specify either UTF-8 or ISO-8859-1. Specify the same setting for the charset attribute in
the <HEAD> section of your site PHP files.
1 delete records
1 view a summary report
You also generate a home page that is linked with the other generated website pages. For detailed
information, see PHP Site Assistant Help.
Notes
1 To start PHP Site Assistant from FileMaker Pro Advanced, you must host the active database file on
FileMaker Server, and enable the fmphp extended privilege in a privilege set for at least one account.
1 If the IP address of the master machine in your FileMaker Server deployment changes, the shortcuts no
longer work. Follow the procedure above to create new shortcuts.
Note The hostspec property defaults to the value http://localhost. If the PHP engine is running on the same
machine as the web server component of the FileMaker Server deployment, there is no need to specify the
hostspec property. If the PHP engine is on a different machine, use the hostspec property to specify the
location of the web server component of the FileMaker Server deployment.
Creating a record
There are two ways to create a record:
1 Use the createRecord() method, specifying a layout name, and optionally specifying an array of field
values. You can also set values individually in the new record object.
The createRecord() method does not save the new record to the database. To save the record to the
database, call the commit() method.
For example:
$rec =& $fm->createRecord('Form View', $values);
$result = $rec->commit();
30 FileMaker Server Custom Web Publishing with PHP
1 Use the Add command. Use the newAddCommand() method to create a FileMaker_Command_Add
object, specifying the layout name and an array with the record data. To save the record to the database,
call the execute() method.
For example:
$newAdd =& $fm->newAddCommand('Respondent', $respondent_data);
$result = $newAdd->execute();
Duplicating a record
Duplicate an existing record using the Duplicate command. Use the newDuplicateCommand() method to
create a FileMaker_Command_Duplicate object, specifying the layout name and the record ID of the record
that you want to duplicate. Then, duplicate the record by calling the execute() method.
Example
$newDuplicate =& $fm->newDuplicateCommand('Respondent', $rec_ID);
$result = $newDuplicate->execute();
Editing a record
There are two ways to edit a record:
1 Using the Edit command. Use the newEditCommand() method to create a FileMaker_Command_Edit
object, specifying the layout name, the record ID of the record you want to edit, and an array of values
that you want to update. Then, edit the record by calling the execute() method.
For example:
$newEdit =& $fm->newEditCommand('Respondent', $rec_ID, $respondent_data);
$result = $newEdit->execute();
1 Using a record object. Retrieve a record from the database, change field values, and then edit the record
by calling the commit() method.
For example:
$rec = $fm->getRecordById('Form View', $rec_ID);
$rec->setField('Name', $nameEntered);
$result = $rec->commit();
Deleting a record
There are two ways to delete a record:
1 Retrieve a record from the database, and then call the delete() method.
For example:
$rec = $fm->getRecordById('Form View', $rec_ID);
$rec->delete();
1 Delete an existing record using the Delete command. Use the newDeleteCommand() method to create a
FileMaker_Command_Delete object, specifying the layout name and the record ID of the record you
want to delete. Then, delete the record by calling the execute() method.
For example:
$newDelete =& $fm->newDeleteCommand('Respondent', $rec_ID);
$result = $newDelete->execute();
Chapter 5 | Using the FileMaker API for PHP 31
If one of the above steps generates an error code, the command execution stops; any steps that follow are
not executed. However, any prior steps in the request are still executed.
For example, consider a command that deletes the current record, sorts the records, and then executes a
script. If the addSortRule() method specifies a non-existent field, the request deletes the current record and
returns error code 102 (“Field is missing”), but does not execute the script.
Using portals
A portal is table that displays rows of data from one or more related records. The FileMaker class defines a
related set object and several methods that allow you to work with portals defined in a FileMaker Pro database.
A related set object is an array of record objects from the related portal; each record object represents one
row of data in the portal.
$result = $new_row->commit();
$nameField = $nextRow->getField('customer::name')
if ($nameField == $badName ) {
$result = $newRow->delete();
}
For a specific field object, use the getValueList() method to get an array of choices defined for the named value
list. If the field is not a value list field, then this method returns a null value.
Example
$valuesArray = $currentField->getValueList('style');
If the database uses a value list that displays only related values (values are dynamically set based on the
values in a second field), include a record ID value in the getValueList() method to retrieve the value list data
for a specific record.
Example
$value = $layout->getValueList('style', $record->getRecordId());
The FileMaker class also defines several methods that can be used for all four types of Find commands:
1 Use the addSortRule() method to add a rule defining how the result set is sorted. Use the clearSortRules()
method to clear all sort rules that have been defined.
1 Use the setLogicalOperator() method to change between logical AND searches and logical OR searches.
1 Use the setRange() method to request only part of the result set. Use the getRange() method to retrieve the
current range definition.
Using the setRange() method can improve the performance of your solution by reducing the number
records that are returned by the Find request. For example, if a Find request returns 100 records, you can
split the result set into five groups of 20 records each rather than processing all 100 records at once.
1 You can execute ScriptMaker scripts in conjunction with Find commands.
1 To run a script before executing the Find command, use the setPreCommandScript() method.
1 To run a script before sorting the result set, use the setPreSortScript() method.
1 To run a script after a result set is generated, but before the result set is sorted, use the setScript() method.
36 FileMaker Server Custom Web Publishing with PHP
The PHP engine pre-validation supports implicit checking of field data based on the type of the field:
1 date
A field defined as a date field is validated according to the rules of “four-digit year” validation, except
the year value can contain 0-4 digits (the year value can be empty). For example, 1/30 is a valid date even
though it has no year specified.
1 time
A field defined as a time field is validated according to the rules of “time of day” validation, except the
hour component (H) can be a number between 1 and 24 inclusive to support 24-hour time values.
1 timestamp
A field defined as a timestamp field is validated according to the rules of “time” validation for the time
component and according to the rules of “date” validation for the date component.
40 FileMaker Server Custom Web Publishing with PHP
The FileMaker class cannot pre-validate all of the field validation options that are available in FileMaker
Pro. The following validation options cannot be pre-validated because they are dependent on the state of all
the data in the database at the time that the data is committed:
1 unique value
1 existing value
1 in range
1 member of value list
1 validate by calculation
Pre-validating records
For a record object, use the validate() method to validate one field or all the fields in the record against the
pre-validation rules enforceable by the PHP engine. If you pass the optional field name argument, only that
field is pre-validated.
If the pre-validation passes, then the validate() method returns TRUE. If the pre-validation fails, then the
validate() method returns a FileMaker_Error_Validation object containing details about what failed to validate.
Pre-validating fields
For a field object, use the validate() method to determine whether a given value is valid for a field.
If the pre-validation passes, then the validate() method returns TRUE. If the pre-validation fails, then the
validate() method returns a FileMaker_Error_Validation object containing details about what failed to validate.
You can also use the following methods with a FileMaker_Error_Validation object:
1 Use the isValidationError() method to test whether the error is a validation error.
1 Use the numErrors() method to get the number of validation rules that failed.
Example
//Create an Add request
$addrequest =& $fm->newAddCommand('test', array('join' => 'added', 'maxchars' => 'abcx', 'field' => 'something' ,
'numericonly' => 'abc'));
//If the validate() method returned any errors, print the name of the field, the error number, and the value that failed.
if(FileMaker::isError($result)){
echo 'Validation failed:'. "\n";
$validationErrors= $result->getErrors();
foreach ($validationErrors as $error) {
$field = $error[0];
echo 'Field Name: ' . $field->getName(). "\n";
echo 'Error Code: ' . $error[1] . "\n";
echo 'Value: ' . $error[2] . "\n";
}
}
Output
Validation failed:
Field Name: numericonly
Error Code: 2
Value: abc
Field Name: maxchars
Error Code: 3
Value: abcx
42 FileMaker Server Custom Web Publishing with PHP
Handling errors
The FileMaker class defines the FileMaker_Error object to help you handle errors that occur in a PHP solution.
An error can occur when a command runs. If an error does occur, the command returns a FileMaker_Error
object. It is a good practice to check the error that is returned when a command runs.
Use the following methods to learn more about the error indicated in the FileMaker_Error object.
1 Test for whether a variable is a FileMaker Error object by calling the isError() method.
1 Get the number of errors that occurred by calling the numErrors() method.
1 Retrieve an array of arrays describing the errors that occurred by calling the getErrors() method.
1 Display an error message by calling the getMessage() method.
Example
$result = $findCommand->execute();
if (FileMaker::isError($result)) {
echo "<p>Error: " . $result->getMessage() . "</p>";
exit;
}
For information about the error codes returned with the FileMaker Error object, see appendix A, “Error
codes for Custom Web Publishing with PHP.”
Chapter 6
Staging, testing, and monitoring a site
This chapter provides instructions for staging and testing a Custom Web Publishing site before deploying it
in a production environment. Instructions are also provided for using log files to monitor the site during
testing or after deployment.
Note If you have installed the web server, Web Publishing Engine, and the Database Server in a
single-machine deployment, you can view and test your site without using a network connection. Move your
site files to the appropriate directory on that machine, and enter one of the following URLs in your browser:
1 http://localhost/<site_path>
1 http://127.0.0.1/<site_path>
Where <site_path> is the relative path to the home page for your site.
Note For information on the W3C Common Logfile Format and the W3C Extended Log File Format, see
the World Wide Web Consortium website at www.w3.org.
The pe_application_log.txt file is generated if any of the following Logging options are enabled in the Web
Publishing Engine.
Logging option enabled Information recorded in pe_application_log.txt
Error Logging Any unusual Web Publishing Engine errors that have occurred. Common errors
reported to the web user, such as “Database not open,” are not recorded.
Script Logging Any errors generated when web users execute scripts. For example, it lists script steps
that are skipped if they’re not web-compatible.
Both of these Logging options are enabled by default. For information on setting these options using the
Admin Console, see FileMaker Server Getting Started Guide.
Note The entries in the application log are not automatically deleted, and over time the file may become
very large. To save hard disk space on your host computer, archive the application log file on a regular basis.
For more information, see the FileMaker Server Getting Started Guide and FileMaker Server Help.
Appendix A
Error codes for Custom Web Publishing with PHP
The Web Publishing Engine supports two types of error codes that can occur for Custom Web Publishing:
1 Database and data request errors. The Web Publishing Engine generates an error code whenever data is
requested from published database. The FileMaker API for PHP returns this error code as a
FileMaker_Error object. See the next section, “Error code numbers for FileMaker databases.”
1 PHP errors. These errors are generated and returned by PHP components, including the cURL module.
See “Error code numbers for PHP components” on page 53.
time 39
time of day 39
timestamp 39
value lists 34
W
web browsers
receiving output 10
Web folder, copying container field objects 19
Web Publishing Core
internal access logs 45
Web Publishing Engine
application log 44
described 10
generated error codes 47
request processing 10
web server
log files 44
web users
using container field data 20
web_server_module_log.txt log file 45
websites
FileMaker support pages 7
monitoring 44
staging 43
testing 43
troubleshooting 45
wpc_access_log.txt file 45
X
XML and XSLT advantages 12
XML custom web publishing 11
XSLT custom web publishing 11
60 FileMaker Server Custom Web Publishing with PHP