mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
update php tutorial
This commit is contained in:
2
examples/tutorial/hello/php/.gitignore
vendored
2
examples/tutorial/hello/php/.gitignore
vendored
@@ -1 +1,3 @@
|
||||
vendor/
|
||||
func.yaml
|
||||
composer.lock
|
||||
|
||||
@@ -37,14 +37,21 @@ That's it!
|
||||
|
||||
### Note on Dependencies
|
||||
|
||||
```yml
|
||||
name: USERNAME/hello
|
||||
version: 0.0.1
|
||||
path: /hello
|
||||
build:
|
||||
- docker run --rm -v "$PWD":/worker -w /worker funcy/php:dev composer install
|
||||
In PHP, you can create a [composer](https://getcomposer.org/) file in your function directory, then run:
|
||||
|
||||
```sh
|
||||
fn build
|
||||
```
|
||||
|
||||
This will rebuild your gems and vendor them. PHP doesn't pick them up automatically, so you'll have to add this to the top of your `func.php` file:
|
||||
|
||||
```php
|
||||
require 'vendor/autoload.php';
|
||||
```
|
||||
|
||||
Open `func.php` to see it in action.
|
||||
|
||||
|
||||
### 3. Queue jobs for your function
|
||||
|
||||
Now you can start jobs on your function. Let's quickly queue up a job to try it out.
|
||||
@@ -63,18 +70,23 @@ cat hello.payload.json | fn call phpapp /hello
|
||||
```
|
||||
|
||||
2. We received our function input through **stdin**
|
||||
```node
|
||||
obj = JSON.parse(fs.readFileSync('/dev/stdin').toString())
|
||||
```php
|
||||
$payload = json_decode(file_get_contents("php://stdin"), true);
|
||||
```
|
||||
|
||||
3. We wrote our output to **stdout**
|
||||
```node
|
||||
console.log
|
||||
```php
|
||||
echo "Hello World!\n";
|
||||
```
|
||||
|
||||
4. We sent **stderr** to the server logs
|
||||
```node
|
||||
console.error
|
||||
```php
|
||||
fwrite(STDERR, "--> this will go to stderr (server logs)\n");
|
||||
```
|
||||
|
||||
5. We added PHP dependencies and enabled them using:
|
||||
```php
|
||||
require 'vendor/autoload.php';
|
||||
```
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
{
|
||||
"require": {
|
||||
"monolog/monolog": "1.0.*"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
fwrite(STDERR, "--> this will go to stderr (server logs)\n");
|
||||
stream_set_blocking(STDIN, 0);
|
||||
$payload = json_decode(file_get_contents("php://stdin"), true);
|
||||
if (isset($payload['name'])) {
|
||||
echo "Hello ", $payload['name'],"!\n\n";
|
||||
echo "Hello ", $payload['name'],"!\n";
|
||||
} else {
|
||||
echo "Hello World!\n\n";
|
||||
echo "Hello World!\n";
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
name: carimura2/hello
|
||||
version: 0.0.1
|
||||
version: 0.0.2
|
||||
runtime: php
|
||||
entrypoint: php func.php
|
||||
path: /hello
|
||||
|
||||
Reference in New Issue
Block a user