SlideShare a Scribd company logo
PHP for Android
Prototyping Android apps in php
Cesare D'Amico
cesare@wyrd.it
Cesare D'Amico
freelance php dev
Hi, nice to meet you!
cesare@wyrd.it
Twitter: __ce (double underscore)
http://cesaredami.co/
What happens adding...
+
Genetically modified php :)
We're going to talk about...
+ PFAf
SL4A
Android
Android
• built on top of a linux
kernel
• native libc (Bionic):
0.5 * sizeof(GNU libC)
• runtime (Dalvik VM) +
system libs
• application framework
• Java!
How can we make php run
on Android?
1. static compilation with Glibc (over
7MB)... fat!
2. linked to Bionic: smaller (~2MB), but
tweaks needed
How can we make php run
on Android?
3. SL4A to the rescue!
SL4A – scripting layer for
Android
• Google project, led by
Damon Kohler
• Open Source
• exposes Android API
• quite active
SL4A: installation
As easy as:
1) enable “Unknown
sources” under Applications
settings
2)
PHP for Android project
• led by Iván Mosquera,
software engineer at Irontec
(the people behind IronPython
and other OS projects)
• Open Source
• Started in 2009
• php-for-android on Google
Groups
PFA: installation
The PFA apk just installs
other components:
• php_rX.zip:
– php binary
• php_scripts_rX.zip
– scripts and PFAf
• php_extras_rX.zip
• Android.php + php.ini
Everything's installed!
SL4A: how does it work?
Provides Android facades
Makes the Android API
available through JSON
RPC calls
PFA: let's try it
PFA: let's try it
<?php
require_once('Android.php');
$droid = new Android();
$droid­>vibrate();
PFA: let's try it
SOOOO easy! :-)
PFA: let's try it
Let's try
something
else:
PFA: let's try it
<?php
require_once('Android.php');
$droid = new Android();
$name = $droid­>getInput('Hi!',
  'What is your name?');
$droid­>makeToast('Hello '.
  $name['result']);
PFA: let's try it
PFA: access phone features
PFA: access phone features
<?php
require_once('Android.php');
$droid = new Android();
$droid­>viewContacts();
PFA: access phone features
Many more features
available; API full list:
http://j.mp/9btqUG
Examples: smsSend, sendEmail,
cameraCapturePicture, viewHTML,
bluetoothConnect, contactsGet,
viewMap... (perhaps not *everything*
available yet)
PFA: writing scripts
Directly on the phone!
PFA: writing scripts
SOOOOO
slow and
unusable!
:-(
PFA: writing scripts
Including local files:
<?php
include('/sdcard/proj/...');
?>
PFA: writing scripts
allow_url_fopen
+
allow_url_include
FTW!
PFA: including remote code
<?php
require_once('Android.php');
$droid = new Android();
include('http://www.wyrd.it/
droid.php.txt');
<?php
$droid­>makeToast("There's a 
starman waiting in the sky!");
remote.php
droid.php.txt
PFA: including remote code
PFA: including remote code
include('http://www.wyrd.it/
droid.php.txt');
Secure
Handy
?
This is here for testing purposes ONLY!
PFA: including remote code
Don't forget that PFA,
although yet useable in
many respects, is still
alpha software.
In the future we'll be
able to distribute scripts
in apk format, without
using allow_url_fopen
PFA: internals
Android.php:
<?php
public function __call($name, $args){
  return $this­>rpc($name, $args);
}
public function rpc($method, $args){
  $data = array('id'=>$this­>_id, 'method'=>$method,
                'params'=>$args);
  $request = json_encode($data)."n";
  $sent = socket_write($this­>_socket,$request,
                       strlen($request));
PFA: internals
<?php
public function rpc($method, $args){
  $data = array('id'=>$this­>_id, 'method'=>$method,
                'params'=>$args);
  $request = json_encode($data)."n";
  $sent = socket_write($this­>_socket, $request,
                      strlen($request));
  $response = socket_read($this­>_socket, 1024,
    PHP_NORMAL_READ)or die("Could not read inputn");
  $this­>_id++;
  $result = json_decode($response);
  return array(
    'id'=>$result['id'], 'result'=>$result['result'],
    'error'=>$result['error']
  );
}
PFAf: PFA framework
Base php class: ScriptAbstract; apps
extend this class and implement init()
States-based: put on a stack the next
action you want to execute
function currentAction(){
  ...
  $this­>setNextAction('next');
}
function nextAction() { ... }
Thanks everyone!
?
PFA wants you!
You can help by writing
scripts and sharing
them:
http://www.phpforandroid.net/scripts
...and there are good examples there!
Take a look at the wi-fi scanner and the
web server ;)
12-14 Maggio 2011
http://www.phpday.it/
Please, rate this talk!
Feel like bashing?
Urge saying you fell in love
with this presentation?
Now you can!
http://joind.in/2152

More Related Content

PDF
Phpforandroid en
PPTX
Resful Trinity Code One - San Francisco
PDF
really really really awesome php application with bdd behat and iterfaces
PDF
Chasing the RESTful Trinity - Client CLI and Documentation
PDF
GraalVM and MicroProfile - A Polyglot Microservices Solution
PDF
Getting started with Go - Florin Patan - Codemotion Rome 2017
PDF
Як РНР розробник пише код на Kotlin
PDF
Dev + DevOps для PHP розробника
Phpforandroid en
Resful Trinity Code One - San Francisco
really really really awesome php application with bdd behat and iterfaces
Chasing the RESTful Trinity - Client CLI and Documentation
GraalVM and MicroProfile - A Polyglot Microservices Solution
Getting started with Go - Florin Patan - Codemotion Rome 2017
Як РНР розробник пише код на Kotlin
Dev + DevOps для PHP розробника

What's hot (20)

PDF
Pavimentando el Camino con Jakarta EE 9 y Apache TomEE 9.0.0
PDF
JavaScript for PHP Developers
PDF
Flutter beers and pizza
PDF
Spring-batch Groovy y Gradle
PDF
RxNetty
PDF
7 recomendaciones para migrar tus aplicaciones a Jakarta EE utilizando Apache...
PDF
DBI for Parrot and Perl 6 Lightning Talk 2007
PDF
Paving the road with Jakarta EE and Apache TomEE - JCON 2021
PDF
PSR7 - interoperabilità HTTP
PPTX
C/C++ Remote Development Overview
PDF
Building Command Line Tools with Golang
PDF
plone.api
PDF
Beyond QA
PDF
Let the contribution begin
PDF
Best practices for joomla extensions developers
PDF
用 Go 語言實戰 Push Notification 服務
PDF
Code analysis for a better future
PDF
Creating an api from design to security.
PDF
Pavimentando el camino con Jakarta EE 9 y Apache TomEE
PDF
[INNOVATUBE] Tech Talk #3: Golang - Takaaki Mizuno
Pavimentando el Camino con Jakarta EE 9 y Apache TomEE 9.0.0
JavaScript for PHP Developers
Flutter beers and pizza
Spring-batch Groovy y Gradle
RxNetty
7 recomendaciones para migrar tus aplicaciones a Jakarta EE utilizando Apache...
DBI for Parrot and Perl 6 Lightning Talk 2007
Paving the road with Jakarta EE and Apache TomEE - JCON 2021
PSR7 - interoperabilità HTTP
C/C++ Remote Development Overview
Building Command Line Tools with Golang
plone.api
Beyond QA
Let the contribution begin
Best practices for joomla extensions developers
用 Go 語言實戰 Push Notification 服務
Code analysis for a better future
Creating an api from design to security.
Pavimentando el camino con Jakarta EE 9 y Apache TomEE
[INNOVATUBE] Tech Talk #3: Golang - Takaaki Mizuno
Ad

Viewers also liked (20)

PPSX
Php and MySQL
PPT
PurpleSearch screenshots
PDF
OE 35 opening of central institution of sanatanis at sanskar dham, Desalpar
PDF
Series 39 1935 Bombay high court case on Satpanth Issue
PDF
Series 30 -Umiya_mataji_unjha_resolution_dt._16-nov-1978
PDF
Series 28 How to counter the threat posed by mavals
PDF
C b-macpherson-the-life-and-times-of-liberal-democracy
PPSX
Series 33 - E - History of Pirana Satpanth Part 2 of 3
PDF
My strategicplan.strategyhuddle.042810
PPTX
Likes health project
PDF
GE1 important information -on satpanth and samaj d
PPSX
Series 33 - G - PPT - History of Pirana satpanth Part 1 of 3
PPT
Civil society initiatives on engaging peace processes
PDF
Series 10 pirana satpanth kaka appointment and admin rights agreement -d
PDF
Series 32 1945 -Umiya Mataji Vandhay Mandir -Inauguration
PPT
Effective googling
PDF
Series 9 attachment -photos of original hindu atharv ved pages
PDF
Anag
PPT
Abecedario para niños
Php and MySQL
PurpleSearch screenshots
OE 35 opening of central institution of sanatanis at sanskar dham, Desalpar
Series 39 1935 Bombay high court case on Satpanth Issue
Series 30 -Umiya_mataji_unjha_resolution_dt._16-nov-1978
Series 28 How to counter the threat posed by mavals
C b-macpherson-the-life-and-times-of-liberal-democracy
Series 33 - E - History of Pirana Satpanth Part 2 of 3
My strategicplan.strategyhuddle.042810
Likes health project
GE1 important information -on satpanth and samaj d
Series 33 - G - PPT - History of Pirana satpanth Part 1 of 3
Civil society initiatives on engaging peace processes
Series 10 pirana satpanth kaka appointment and admin rights agreement -d
Series 32 1945 -Umiya Mataji Vandhay Mandir -Inauguration
Effective googling
Series 9 attachment -photos of original hindu atharv ved pages
Anag
Abecedario para niños
Ad

Similar to PHP for Android: prototyping Android apps in php (20)

PPT
Building PHP Powered Android Applications
ODP
When Perl Met Android (YAPC::EU 2010)
PDF
PHP in a mobile ecosystem
PDF
PDF
Integrating PHP With System-i using Web Services
ODP
Android Overview
KEY
Android Scripting
PDF
PDF
PDF
Manual oficial de php5
PDF
Porting your favourite cmdline tool to Android
PPT
Portinig Application, Drivers And Os
PDF
(phpconftw2012) PHP as a Middleware in Embedded Systems
PPTX
Racing with Droids
PDF
Lecture8
PDF
Summer training report priyanka
PDF
Android Patching & Client-Side CyberSecurity
Building PHP Powered Android Applications
When Perl Met Android (YAPC::EU 2010)
PHP in a mobile ecosystem
Integrating PHP With System-i using Web Services
Android Overview
Android Scripting
Manual oficial de php5
Porting your favourite cmdline tool to Android
Portinig Application, Drivers And Os
(phpconftw2012) PHP as a Middleware in Embedded Systems
Racing with Droids
Lecture8
Summer training report priyanka
Android Patching & Client-Side CyberSecurity

PHP for Android: prototyping Android apps in php