Embedding PHP
Sara Golemon
University of California - Berkeley
pollita@php.net
$embed->empower->enrich();
Orlando, Florida
April 25-28, 2006
2
Introduction
• About This Talk
– Embedding the PHP interpreter into 3rd party
applications.
• About You
– You have an application which could benefit from
scriptibility.
– You have some familiarity with the PHP API.
– You like asking questions and aren’t shy about it.
• About Me
– PHP Internals & PECL Developer
– It’s my first time speaking, BE NICE!
Orlando, Florida
April 25-28, 2006
3
PHP Architecture
PHP Core
Zend Engine
SAPI
Extensions
MySQL
standard
PDO
session
• Apache / APXS
• CLI
• CGI
• ISAPI
• Other Web Servers…
• Embed
Orlando, Florida
April 25-28, 2006
4
How this division looks in Windows
PHP Core
Zend Engine
SAPI
Orlando, Florida
April 25-28, 2006
5
Taking the role of the SAPI
A. Make system calls to PHP executables
•
Slow: Every syscall requires PHP to be loaded, initialized,
activated, deactivated, shutdown, and unloaded.
•
Nonpersistent: Each call to PHP is a new request with a fresh
symbol table and no state information.
•
Noninteractive: No access to symbol tables, limited error
handling, no ability to expose host application to PHP.
B. Write your own SAPI implementation
•
Most powerful option for integrating an application with PHP.
•
Several easy mistakes to make.
•
More work than necessary for most applications.
C. Link against the Embed SAPI.
•
Requires minimal programming to “get started”.
•
Can be built on gradually to work towards a full-featured SAPI.
•
Hides the messy ZTS bits from applications which don’t care
about threading.
Orlando, Florida
April 25-28, 2006
6
Installing SAPI/embed
• On Windows
– Already built (php5embed.lib) but needs
source code (for headers) as well.
• On Linux/BSD/*nix
– ./configure --other-options \
--enable-embed=shared
or static
– make all install
– /usr/local/lib/libphp5.so
– /usr/local/include/php/*
Orlando, Florida
April 25-28, 2006
7
Simplest Usage
#include <php_embed.h>
int main (int argc, char *argv[])
{
P