Thijs Feryn gave a presentation on the CLI (command line interface) SAPI for PHP. The CLI SAPI allows PHP scripts to be run from the command line/terminal instead of through a web server. Some key points of the CLI SAPI include reading arguments from $argv, reading input from STDIN, writing output to STDOUT, and having full control over script execution without timeouts. The CLI SAPI is useful for tasks like cronjobs, batch processing, and applications without a web frontend.
4. I
live
in
the
wonderful
city
of
Bruges
MPBecker
-‐
Bruges
by
Night
hWp://www.flickr.com/photos/galverson2/3715965933
5. Follow
me
on
Twi+er:
@ThijsFeryn
Give
me
feedback:
h+p://joind.in/2852
Read
my
blog:
h+p://blog.feryn.eu
6.
7. SAPI
according
to
Wikipedia
“The
Server
Applica^on
Programming
Interface
(SAPI)
is
the
generic
term
used
to
designate
direct
module
interfaces
to
web
server
applica^ons”
10. The
CLI
SAPI
according
to
php.net
As
of
version
4.3.0,
PHP
supports
a
new
SAPI
type
(Server
Applica^on
Programming
Interface)
named
CLI
which
means
Command
Line
Interface.
As
the
name
implies,
this
SAPI
type
main
focus
is
on
developing
shell
(or
desktop
as
well)
applica^ons
with
PHP
11. The
CLI
SAPI
according
to
php.net
PHP
script
execu^on
via
the
command
line
interface
12. When
to
use
• In
crons
• For
batch
tasks
• For
worker
processes
• Daemons
• Process
control
• Interac^on
with
other
binaries
30. Comparing
the
Apache
&
CLI
SAPI
Web
based
SAPI’s
• HTTP
is
a
stateless
protocol
• Request/response
based
• Limited
interac^on
• Sessions
&
cookies
as
workaround
• Execu^on
^meouts
• Limited
request/response
size
31. Comparing
the
Apache
&
CLI
SAPI
CLI
SAPI
• Controlable
state
• Controlable
script
execu^on
• Con^nuous
interac^on
• No
need
for
sessions
• No
execu^on
^meouts
48. Get
INI
informaOon
(-‐i)
Filtering
items
$
php
-‐i
|
grep
“log_”
define_syslog_variables
=>
Off
=>
Off
log_errors
=>
On
=>
On
log_errors_max_len
=>
1024
=>
1024
$
49. Syntax/lint
check
(-‐l)
Only
checks
parse
errors
$
php
-‐l
myFile.php
No
syntax
errors
detected
in
myFile.php
$
50. Module
list
(-‐m)
$
php
-‐m
[PHP
Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
dba
$
63. STDIN
Stream
that
is
opened
<?php by
default
while(!feof(STDIN)){
$line = trim(fgets(STDIN));
if(strlen($line) > 0){
echo strrev($line).PHP_EOL;
}
}
64. STDIN
The
Stream
that
is
opened
proof
! by
default
$
php
-‐r
"var_dump(STDIN);"
resource(1)
of
type
(stream)
$
81. PCNTL
according
to
php.net
Process
Control
support
in
PHP
implements
the
Unix
style
of
process
crea^on,
program
execu^on,
signal
handling
and
process
termina^on.
82. Process
Control
should
not
be
enabled
within
a
web
server
environment
and
unexpected
results
may
happen
if
any
Process
Control
func^ons
are
used
within
a
web
server
environment.
83. Forking
according
to
Wikipedia
In
compu^ng,
when
a
process
forks,
it
creates
a
copy
of
itself
84. Forking PID
value
determines
context Copy
<?php
program
$pid = pcntl_fork();
if ($pid == -1) {
execu^on
//Forking failed
} else if ($pid) {
//Parent logic
} else { PID
of
child
//Child logic
} process
87. Signals
A
signal
is
a
limited
form
of
inter-‐process
communica^on
used
in
Unix,
Unix-‐like,
and
other
POSIX-‐compliant
opera^ng
systems.
Essen^ally
it
is
an
asynchronous
no^fica^on
sent
to
a
process
in
order
to
no^fy
it
of
an
event
that
occurred.
92. POSIX
process
control
funcOons
<?php
$pid=pcntl_fork();
if ($pid == -1) {
die("could not fork");
} else if ($pid) {
$exists = posix_kill($pid,0)?'still':'no longer';
echo "[parent] Child process $pid $exists exists".PHP_EOL;
echo "[parent] Killing child process $pid".PHP_EOL;
posix_kill($pid,SIGTERM);
echo "[parent] Child process $pid killed".PHP_EOL;
pcntl_wait($status);
$exists = posix_kill($pid,0)?'still':'no longer';
echo "[parent] Child process $pid $exists exists".PHP_EOL;
} else {
while(true){
sleep(100);
}
exit;
}
93. POSIX
process
control
funcOons
“KILL
0”
checks
<?php
$pid=pcntl_fork(); existence
if ($pid == -1) {
die("could not fork");
} else if ($pid) {
Send
SIGTERM
$exists = posix_kill($pid,0)?'still':'no longer';
echo "[parent] Child process $pid $exists exists".PHP_EOL;
signal
echo "[parent] Killing child process $pid".PHP_EOL;
posix_kill($pid,SIGTERM);
echo "[parent] Child process $pid killed".PHP_EOL;
pcntl_wait($status);
$exists = posix_kill($pid,0)?'still':'no longer';
echo "[parent] Child process $pid $exists exists".PHP_EOL;
} else {
while(true){
Child
process
sleep(100);
}
is
dead
exit;
}
94. Some
PCNTL
guidelines
✓Detect
your
CWD
✓Use
PID
files
✓Control
you
file
privileges
✓Detach
you
session
✓Avoid
rogue
child
processes
✓Cleanup
your
garbage
95. Talk
dedicated
to
process
control
Check
this
guy
out
! in
PHP
Jeroen
Keppens:
@jkeppens
http://www.slideshare.net/jkeppens/php-‐in-‐the-‐dark