Slide 1

Slide 1 text

CLI,%the%other%SAPI Thijs%Feryn Evangelist +32%(0)9%218%79%06 [email protected] php

Slide 2

Slide 2 text

About%me I’m%an%Evangelist%at%Combell

Slide 3

Slide 3 text

About%me I’m%a%board%member%at%PHPBenelux

Slide 4

Slide 4 text

Follow%me%on%Twi+er:%@ThijsFeryn Give%me%feedback:%h+p://joind.in/6393

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

SAPI? The%way%you%interact%with%PHP

Slide 7

Slide 7 text

Common%SAPIs

Slide 8

Slide 8 text

Common%SAPIs • Apache/Apache%2 • FPM • FastCGI • ISAPI • CLI • GTK

Slide 9

Slide 9 text

The%CLI%SAPI PHP%script%execuWon%via%the%command%line%interface

Slide 10

Slide 10 text

When%to%use

Slide 11

Slide 11 text

When%to%use • In%crons • For%batch%tasks • For%worker%processes • Daemons • Process%control • InteracWon%with%other%binaries

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

CLI 101

Slide 14

Slide 14 text

CLI 101 The PHP binary Passing arguments Reading from STDIN I/O with pipes

Slide 15

Slide 15 text

CLI 101 Invoking a script with the PHP binary php#file.php

Slide 16

Slide 16 text

CLI 101 Passing arguments php#file.php#arg1#arg2

Slide 17

Slide 17 text

CLI 101 interpreting arguments $argument){ echo "Argument # {$key}: {$argument}\n"; }

Slide 18

Slide 18 text

CLI 101 interpreting arguments $argument){ echo "Argument # {$key}: {$argument}\n"; } Argument% count Argument% array

Slide 19

Slide 19 text

CLI 101 interpreting arguments $"php"args.php"arg1"arg2 Number"of"arguments"3 Argument"#"0:"args.php Argument"#"1:"arg1 Argument"#"2:"arg2 $ The% PHP%file%is%an% argument%too

Slide 20

Slide 20 text

CLI 101 interpreting arguments $argc $argv $_SERVER[‘argc’] $_SERVER[‘argv’] !!! register_argc_argv !!!

Slide 21

Slide 21 text

CLI 101 getopt

Slide 22

Slide 22 text

CLI 101 getopt

Slide 23

Slide 23 text

CLI 101 php"getopt.php":a":b"2":c3 array(3)"{ ""["a"]=> ""bool(false) ""["b"]=> ""string(1)""2" ""["c"]=> ""string(1)""3" } No% spacing%for% opWonal% arguments

Slide 24

Slide 24 text

CLI 101 getopt: longopts

Slide 25

Slide 25 text

CLI 101 php"getopt2.php"::arg1"::arg2"123"::arg3=x array(3)"{ ""["arg1"]=> ""bool(false) ""["arg2"]=> ""string(3)""123" ""["arg3"]=> ""string(1)""x" } Mind% the%“=”%sign

Slide 26

Slide 26 text

CLI 101 REading From STDIN 0){ echo strrev($line).PHP_EOL; } } fclose($handle);

Slide 27

Slide 27 text

CLI 101 $"cat"test.txt"|"php"stdin.php" enO owT eerhT $

Slide 28

Slide 28 text

CLI 101 $"cat"test.txt"|"php"stdin.php" enO owT eerhT $ Output% file Convert% output%to% input%with% pipes

Slide 29

Slide 29 text

Comparing%the%Apache%&%CLI%SAPI

Slide 30

Slide 30 text

Comparing%the%Apache%&%CLI%SAPI Web%based%SAPI’s • HTTP%is%a%stateless%protocol • Request/response%based • Limited%interacWon • Sessions%&%cookies%as%workaround • ExecuWon%Wmeouts • Limited%request/response%size

Slide 31

Slide 31 text

Comparing%the%Apache%&%CLI%SAPI CLI%SAPI • Controlable%state • Controlable%script%execuWon • ConWnuous%interacWon • No%need%for%sessions • No%execuWon%Wmeouts

Slide 32

Slide 32 text

The%PHP%binary php

Slide 33

Slide 33 text

The%PHP%binary Usage:"php"[options]"[:f]""[::]"[args...] """""""php"[options]":r""[::]"[args...] """""""php"[options]"[:B"]":R"" [:E"]"[::]"[args...] """""""php"[options]"[:B"]":F"" [:E"]"[::]"[args...] """""""php"[options]"::"[args...] """""""php"[options]":a

Slide 34

Slide 34 text

InteracLve%mode%("a) $"php":a Interactive"shell php">"echo"5+8; 13 php">"function"addTwo($n) php">"{ php"{"return"$n"+"2; php"{"} php">"var_dump(addtwo(2)); int(4) php">

Slide 35

Slide 35 text

InteracLve%mode%("a) $"php":a Interactive"shell php">"stri[TAB][TAB] strip_tags"""""stripcslashes"" stripslashes"""stristr"""""""" stripos"""""""" php">"stri Tab% compleWon

Slide 36

Slide 36 text

Run%code%("r) $"php":r""echo"date('Y:m:d"H:i:s');" 2011:03:02"22:04:45 $

Slide 37

Slide 37 text

Config%directory%("c) $"php":c"/custom/dir/php.ini"script.php

Slide 38

Slide 38 text

Define%custom%INI%seRng%("d) $"php":d"max_execution_time=20":r"'$foo"=" ini_get("max_execution_time");" var_dump($foo);' string(2)""20" $

Slide 39

Slide 39 text

Get%INI%informaLon%("i) $"php":i"|"grep"“log_” define_syslog_variables"=>"Off"=>"Off log_errors"=>"On"=>"On log_errors_max_len"=>"1024"=>"1024 $ Filtering% items

Slide 40

Slide 40 text

Syntax/lint%check%("l) $"php":l"myFile.php No"syntax"errors"detected"in"myFile.php $ Only% checks%parse% errors

Slide 41

Slide 41 text

Module%list%("m) $"php":m [PHP"Modules] bcmath bz2 calendar Core ctype curl date dba $

Slide 42

Slide 42 text

Syntax%highlighLng%("s) $"php":s"helloworld.php">"helloworld.html $

Slide 43

Slide 43 text

Syntax%highlighLng%("s) <?php
echo "Hello world";

Slide 44

Slide 44 text

Version%info%("v) $"php":v PHP"5.3.3:1ubuntu9.3"with"Suhosin:Patch" (cli)"(built:"Jan"12"2011"16:07:38)" Copyright"(c)"1997:2009"The"PHP"Group Zend"Engine"v2.3.0,"Copyright"(c)"1998:2010" Zend"Technologies $

Slide 45

Slide 45 text

FuncLon%reflecLon%(""rf) $"php"::rf"json_encode Function"[""function" json_encode"]"{ "":"Parameters"[2]"{ """"Parameter"#0"[""$value"] """"Parameter"#1"[""$options"] ""} } $

Slide 46

Slide 46 text

Class%reflecLon%(""rc) $"php"::rc"stdclass Class"[""class"stdClass"]"{ "":"Constants"[0]"{ ""} "":"Static"properties"[0]"{ ""} "":"Static"methods"[0]"{ ""} "":"Properties"[0]"{ ""} "":"Methods"[0]"{ ""} } $

Slide 47

Slide 47 text

Extension%reflecLon%(""re) $"php"::re"json Extension"[""extension"#20"json"version"1.2.1"]"{ ... "":"Functions"{ """"Function"[""function"json_encode"]"{ """""":"Parameters"[2]"{ """"""""Parameter"#0"[""$value"] """"""""Parameter"#1"[""$options"] """"""} """"} ... }

Slide 48

Slide 48 text

Extension%INI%informaLon%(""ri) $"php"::ri"pdo PDO PDO"support"=>"enabled PDO"drivers"=>"mysql,"sqlite,"sqlite2 $

Slide 49

Slide 49 text

BuiltYin%webserver%("S) $"php":S"localhost:1234 PHP"5.4.0"Development"Server"started"at"Mon"May" 14"09:43:28"2012 Listening"on"localhost:1234 Document"root"is"/var/www/cli.dev Press"Ctrl:C"to"quit. $ PHP% 5.4

Slide 50

Slide 50 text

BuiltYin%webserver%("S) [Mon"May"14"09:44:42"2012]"192.168.72.1:53147" [200]:"/ [Mon"May"14"09:44:42"2012]"192.168.72.1:53148" [200]:"/?=PHPE9568F34:D428:11d2:A769:00AA001ACF42 [Mon"May"14"09:44:42"2012]"192.168.72.1:53149" [200]:"/?=PHPE9568F35:D428:11d2:A769:00AA001ACF42

Slide 51

Slide 51 text

BuiltYin%webserver%("S) $"php":S"localhost:1234"router.php PHP"5.4.0"Development"Server"started"at"Mon"May" 14"09:43:28"2012 Listening"on"localhost:1234 Document"root"is"/path/to/docroot Press"Ctrl:C"to"quit. $ Router

Slide 52

Slide 52 text

BuiltYin%webserver%("t) $"php":S"localhost:1234":t"/path/to/docroot PHP"5.4.0"Development"Server"started"at"Mon"May" 14"09:43:28"2012 Listening"on"localhost:1234 Document"root"is"/path/to/docroot Press"Ctrl:C"to"quit. $ Custom% docroot

Slide 53

Slide 53 text

Back%on%track

Slide 54

Slide 54 text

Back%to%I/O

Slide 55

Slide 55 text

Input%&%output Web • $_SERVER • $_GET • $_POST • $_COOKIE • $_SESSION • $_ENV CLI • $_SERVER • $argc/$argv • $_ENV • getopt() • STDIN/STDOUT/ STDERR

Slide 56

Slide 56 text

Change%your%mindset

Slide 57

Slide 57 text

Change%your%mindset Don’t%use%sessions%&%cookies Just%use%local%variables

Slide 58

Slide 58 text

Change%your%mindset If%you%don’t%need%HTTP,%use%CLI Avoid%overhead E.g.% cronjobs

Slide 59

Slide 59 text

Change%your%mindset Current%directory%!=%webroot ➡Use%dirname(__FILE__) ➡Use%chdir() ➡Use%getcwd() CLI% scripts%are% executable% everywhere

Slide 60

Slide 60 text

STDIN 0){ echo strrev($line).PHP_EOL; } } fclose($handle);

Slide 61

Slide 61 text

STDIN 0){ echo strrev($line).PHP_EOL; } } fclose($handle);

Slide 62

Slide 62 text

STDIN 0){ echo strrev($line).PHP_EOL; } }

Slide 63

Slide 63 text

STDIN 0){ echo strrev($line).PHP_EOL; } } Stream% that%is%opened% by%default

Slide 64

Slide 64 text

STDIN $"php":r""var_dump(STDIN);" resource(1)"of"type"(stream) $ The% proof%! Stream% that%is%opened% by%default

Slide 65

Slide 65 text

Wordcount%example 0){ foreach(preg_split('/[\s]+/',$line) as $word){ if(!array_key_exists($word,$wordArray)){ $wordArray[$word] = 0; } $wordArray[$word]++; } } } ksort($wordArray); foreach($wordArray as $word=>$count){ echo "$word: $count".PHP_EOL; }

Slide 66

Slide 66 text

Wordcount%example $"cat"wordcount.txt" Italy"Thijs Thijs Italy Thijs"PHPDAY Thijs Italy $"cat"wordcount.txt""|"php"wordcount.php" PHPDAY:"1 Thijs:"4 Italy:"3 $

Slide 67

Slide 67 text

STDOUT

Slide 68

Slide 68 text

STDOUT

Slide 69

Slide 69 text

STDERR

Slide 70

Slide 70 text

STDERR

Slide 71

Slide 71 text

Mixing%STDOUT%&%STDERR

Slide 72

Slide 72 text

Mixing%STDOUT%&%STDERR

Slide 73

Slide 73 text

Mixing%STDOUT%&%STDERR $"php"stdmix.php">"/dev/null" STDERR"output $ $"php"stdmix.php"&>""/dev/null $

Slide 74

Slide 74 text

Mixing%STDOUT%&%STDERR $"php"stdmix.php">"/dev/null" STDERR"output $ $"php"stdmix.php"&>""/dev/null $ STDOUT% is%caught STDOUT% &%STDERR%are% caught

Slide 75

Slide 75 text

AlternaLve%output

Slide 76

Slide 76 text

AlternaLve%output

Slide 77

Slide 77 text

Piping $"php":r"'for($i=0;$i<10;$i++)"echo"$i.PHP_EOL;' 0 1 2 3 4 5 6 7 8 9 $"php":r"'for($i=0;$i<10;$i++)"echo"$i.PHP_EOL;'"|"wc":l """"""10 $

Slide 78

Slide 78 text

Readline

Slide 79

Slide 79 text

Shebang%!

Slide 80

Slide 80 text

Shebang%! #!/usr/bin/php

Slide 81

Slide 81 text

Encore

Slide 82

Slide 82 text

Process%Control%should%not%be%enabled% within%a%web%server%environment%and% unexpected%results%may%happen%if%any% Process%Control%funcWons%are%used%within% a%web%server%environment.

Slide 83

Slide 83 text

Forking

Slide 84

Slide 84 text

Forking

Slide 85

Slide 85 text

Forking

Slide 86

Slide 86 text

Signals

Slide 87

Slide 87 text

Signals

Slide 88

Slide 88 text

POSIX%process%control%funcLons

Slide 89

Slide 89 text

POSIX%process%control%funcLons

Slide 90

Slide 90 text

POSIX%process%control%funcLons

Slide 91

Slide 91 text

POSIX%process%control%funcLons

Slide 92

Slide 92 text

Jeroen%Keppens:%@jkeppens http://www.slideshare.net/jkeppens/phpYinYtheYdark Talk% dedicated%to% process%control% in%PHP Check%this% guy%out%!

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

Q&A

Slide 95

Slide 95 text

Thanks%!