-
-
Notifications
You must be signed in to change notification settings - Fork 145
/
59-server-json-api.php
41 lines (32 loc) · 1.43 KB
/
59-server-json-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
// Simple JSON-based HTTP API example as a base to build RESTful/RESTish APIs
// Launch demo and use your favorite CLI tool to test API requests
//
// $ php examples/59-server-json-api.php 8080
// $ curl -v http://localhost:8080/ -H 'Content-Type: application/json' -d '{"name":"Alice"}'
use React\Http\Message\Response;
require __DIR__ . '/../vendor/autoload.php';
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
if ($request->getHeaderLine('Content-Type') !== 'application/json') {
return Response::json([
'error' => 'Only supports application/json'
])->withStatus(Response::STATUS_UNSUPPORTED_MEDIA_TYPE);
}
$input = json_decode($request->getBody()->getContents());
if (json_last_error() !== JSON_ERROR_NONE) {
return Response::json([
'error' => 'Invalid JSON data given'
])->withStatus(Response::STATUS_BAD_REQUEST);
}
if (!isset($input->name) || !is_string($input->name)) {
return Response::json([
'error' => 'JSON data does not contain a string "name" property'
])->withStatus(Response::STATUS_UNPROCESSABLE_ENTITY);
}
return Response::json([
'message' => 'Hello ' . $input->name
]);
});
$socket = new React\Socket\SocketServer($argv[1] ?? '0.0.0.0:0');
$http->listen($socket);
echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()) . PHP_EOL;