Skip to content

Instantly share code, notes, and snippets.

@pixelbrackets
Last active September 2, 2020 12:46
Show Gist options
  • Save pixelbrackets/8d63ea6259da5576a269026dfc8ccfcf to your computer and use it in GitHub Desktop.
Save pixelbrackets/8d63ea6259da5576a269026dfc8ccfcf to your computer and use it in GitHub Desktop.
waterpipe-example-api
<?php
require __DIR__ . '/../vendor/autoload.php';
/**
* Waterpipe: https://packagist.org/packages/elementaryframework/water-pipe
*
* Examples calls:
* http 127.0.0.1:8080/ # GET homepage
* http 127.0.0.1:8080/build/ token=foo # POST JSON Build endpoint
**/
use ElementaryFramework\WaterPipe\WaterPipe;
use ElementaryFramework\WaterPipe\HTTP\Request\Request;
use ElementaryFramework\WaterPipe\HTTP\Response\Response;
use ElementaryFramework\WaterPipe\HTTP\Response\ResponseStatus;
use ElementaryFramework\WaterPipe\HTTP\Response\ResponseHeader;
$app = new WaterPipe;
// homepage
$app->get('/', static function (Request $request, Response $response) {
$response->sendHtml('Endpoints: <ul><li>/build</li></ul>');
});
// build endpoint
$app->post('/build', static function (Request $request, Response $response) {
$data = $request->getBody()->toArray();
$response->sendJson([
'success' => true,
'token' => $data['token']
]);
});
$app->run();
@pixelbrackets
Copy link
Author

Note: This requires redirecting all request to index.php → https://gist.github.com/pixelbrackets/8ba866dcb207b461f0ed0bc5af45927b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment