A simple web service wrapper around the PHP version of Faker.
- PHP 5.4 or later
- PHP Internationalisation Module (PECL intl) installed
- Expand code into folder of your choice.
- Use Composer to install the prerequisites (just Faker & Mustache):
composer install
- There is no step 3 ๐
Once installed the fake data can be accessed in three formats via index.php
; plain text, JSON, and pretty-printed JSON. The output format is controlled via the type
parameter (described below).
By default, plain text is returned when a single value is requested, and JSON when multiple values are requested.
The script reads parameters from PHP's $_REQUEST
super-global variable, so depending on your PHP config, it can accept parameters via the query string, post data, and cookies. See the PHP documentation for $_REQUEST
for details.
arg1
โฆargn
โ separate numbered parameters to specify arguments to be passed to the formatter. E.g.formatter=password&arg1=8&arg2=16
to pass8
and16
as the first and second arguments to thepassword
formatter. Processing will stop at the first missing argument, so if you passarg1
andarg3
thenarg3
will be ignored because processing will have stopped at the missingarg2
.args
โ arguments to be passed to the formatter as a JSON string representing an array. Because special characters need to be URL-escaped in the query string this parameter is best passed as post data.default
(aliased byd
&def
) โ the value to use as the default value when theoptional
parameter has a truthy value. The default value is the empty string.formatter
(aliased byf
, required forindex.php
) โ the name of the Faker Formatter to generate the data with, e.g.name
. You can see the fromatters available viainfo.php
.locale
(aliased byl
&loc
) โ the locale to use when constructing the Faker Factory object that will be used to generate the data. The locale can be in any format that is understood by PHP'slocale_get_primary_language()
andlocale_get_region()
functions. If this parameter is not passed then theAccept-Language
HTTP Request header tried, and if that's not present then Faker's default locale (en_US
) is used. You can test your locale viainfo.php
โ it will process the locale in the same wayindex.php
does and display the result.n
โ the number of pieces of data to generate. The value must be a positive integer or it will be ignored. The default is1
.optional
(aliased byo
&opt
) โ a truthy value for this parameter will enable Faker's.optional()
modifier. If enabled the modifier will be called with values from theweight
anddefault
parameters.recordSeparator
(aliased byrs
&recsep
,records.php
only) โ the separarator to use between records when outputting in text mode. The default is two newline characters.separatpr
(aliased bys
&sep
) โ the separator to use when separating multiple values whentype=text
. The default separator is the new-line character.type
(aliased byt
&want
) โ the requested output format, must be one oftext
,json
, orjsonText
. The default istext
forn=1
andjson
whenn
is greater than 1.json
โ the data is returned as a JSON string represening an array of values. The response will have the MIME-Typeapplication/json
.jsonText
โ the data is returned as a pretty-printed JSON string representing an array of values. The response will have the MIME-Typetext/plain
.text
โ the data is returned as a single string separated byseparator
. The response will have the MIME-Typetext/plain
.
unique
(aliased byu
) โ a truthy value for this parameter will enable Faker's.unique()
modifier.valueSeparator
(aliased byvs
&valsep
,records.php
only) โ the separarator to use between the name and value of a field within a record when outputting in text mode. The default is': '
.weight
(aliased byw
) โ the weigting to use whenoptional
has a truthy value. Defaults to0.5
.
Nicer URLs can be obtained using an NGINX config something like the following:
# set up fakerWS
location /fakerWS/ {
try_files $uri $uri/;
rewrite ^/fakerWS/info/?$ /fakerWS/info.php?$args last;
rewrite ^/fakerWS/info/(json|text|jsonText)/?$ /fakerWS/info.php?type=$1&$args last;
rewrite ^/fakerWS/(\w+)/?$ /fakerWS/index.php?formatter=$1&$args last;
rewrite ^/fakerWS/(\w+)/(\d+)/?$ /fakerWS/index.php?formatter=$1&n=$2&$args last;
rewrite ^/fakerWS/(\w+)/(\d+)/(json|text|jsonText)/?$ /fakerWS/index.php?formatter=$1&n=$2&type=$3&$args last;
rewrite ^/fakerWS/records/?$ /fakerWS/records.php?$args last;
rewrite ^/fakerWS/records/(\d+)/?$ /fakerWS/records.php?n=$1&$args last;
rewrite ^/fakerWS/records/(\d+)/(json|text|jsonText)/?$ /fakerWS/records.php?n=$1&type=$2&$args last;
}
The re-write lines allow URLs of the forms:
http://DOMAIN.TLD/fakerWS/info
, e.g.http://mydomain.tld/fakerWS/info
to get the info page in the default text format.http://DOMAIN.TLD/fakerWS/info/TYPE
, e.g.http://mydomain.tld/fakerWS/info/json
to get the info page in json format.http://DOMAIN.TLD/fakerWS/FORMATTER
, e.g.http://mydomain.tld/fakerWS/name
to get a single value from thename
formatter.http://DOMAIN.TLD/fakerWS/FORMATTER/N
, e.g.http://mydomain.tld/fakerWS/name/5
to get 5 values from thename
formatter.http://DOMAIN.TLD/fakerWS/FORMATTER/N/TYPE
, e.g.http://mydomain.tld/fakerWS/name/5/text
to get 5 values from thename
formatter in plain text.http://DOMAIN.TLD/fakerWS/records?PARAMETERS
, e.g.http://mydomain.tld/fakerWS/records?f1=name&f2=email
to get one record consisting of a name and an email address in the default (for single values) text format.http://DOMAIN.TLD/fakerWS/records/N?PARAMETERS
, e.g.http://mydomain.tld/fakerWS/records/5?f1=name&f2=email
to get five records each consistong of a name and an email address in the default (for multiple values) JSON format.http://DOMAIN.TLD/fakerWS/records/N/TYPE?PARAMETERS
, e.g.http://mydomain.tld/fakerWS/records/5/text?f1=name&f2=email
to get five records each consistong of a name and an email address in text format.