1. Go to this page and download the library: Download phly/http library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
phly / http example snippets
// Create a request
$request = (new Phly\Http\Request())
->withUri(new Phly\Http\Uri('http://example.com'))
->withMethod('PATCH')
->withAddedHeader('Authorization', 'Bearer ' . $token)
->withAddedHeader('Content-Type', 'application/json');
// OR:
$request = new Phly\Http\Request(
'http://example.com',
'PATCH',
'php://memory',
[
'Authorization' => 'Bearer ' . $token,
'Content-Type' => 'application/json',
]
);
// If you want to set a non-origin-form request target, set the
// request-target explicitly:
$request = $request->withRequestTarget((string) $uri)); // absolute-form
$request = $request->withRequestTarget($uri->getAuthority()); // authority-form
$request = $request->withRequestTarget('*'); // asterisk-form
// Once you have the instance:
$request->getBody()->write(json_encode($data));
$response = $client->send($request);
printf("Response status: %d (%s)\n", $response->getStatusCode(), $response->getReasonPhrase());
printf("Headers:\n");
foreach ($response->getHeaders() as $header => $values) {
printf("%s: %s\n", $header, implode(', ', $values));
}
printf("Message:\n%s\n", $response->getBody());
$response = new Phly\Http\Response();
// Write to the response body:
$response->getBody()->write("some content\n");
// Multiple calls to write() append:
$response->getBody()->write("more content\n"); // now "some content\nmore content\n"
// Add headers
// Note: headers do not need to be added before data is written to the body!
$response = $response
->withHeader('Content-Type', 'text/plain')
->withAddedHeader('X-Show-Something', 'something');
// Direct instantiation, with a callback handler, request, and response
$server = new Phly\Http\Server(
function ($request, $response, $done) {
$response->getBody()->write("Hello world!");
},
$request,
$response
);
// Using the createServer factory, providing it with the various superglobals:
$server = Phly\Http\Server::createServer(
function ($request, $response, $done) {
$response->getBody()->write("Hello world!");
},
$_SERVER,
$_GET,
$_POST,
$_COOKIE,
$_FILES
);
// Using the createServerFromRequest factory, and providing it a request:
$server = Phly\Http\Server::createServerfromRequest(
function ($request, $response, $done) {
$response->getBody()->write("Hello world!");
},
$request
);
$server->listen();
$server->listen(function ($request, $response, $error = null) {
if (! $error) {
return;
}
// do something with the error...
});
class Request
{
public function __construct(
$uri = null,
$method = null,
$body = 'php://memory',
array $headers = []
);
// See psr/http-message's RequestInterface for other methods
}
class ServerRequest
{
public function __construct(
array $serverParams = [],
array $fileParams = [],
$uri = null,
$method = null,
$body = 'php://input',
array $headers = []
);
// See psr/http-message's ServerRequestInterface for other methods.
}
class Response
{
public function __construct(
$body = 'php://memory',
$statusCode = 200,
array $headers = []
);
// See psr/http-message's ResponseInterface for other methods
}
// Returns new ServerRequest instance, using values from superglobals:
$request = ServerRequestFactory::fromGlobals();
// or
// Returns new ServerRequest instance, using values provided (in this
// case, equivalent to the previous!)
$request = RequestFactory::fromGlobals(
$_SERVER,
$_GET,
$_POST,
$_COOKIE,
$_FILES
);
class Uri
{
public function __construct($uri = '');
// See psr/http-message's UriInterface for other methods.
}
class Server
{
public function __construct(
callable $callback,
Psr\Http\Message\ServerRequestInterface $request,
Psr\Http\Message\ResponseInterface $response
);
public static function createServer(
callable $callback,
array $server, // usually $_SERVER
array $query, // usually $_GET
array $body, // usually $_POST
array $cookies, // usually $_COOKIE
array $files // usually $_FILES
);
public static function createServerFromRequest(
callable $callback,
Psr\Http\Message\ServerRequestInterface $request,
Psr\Http\Message\ResponseInterface $response = null
);
public function setEmitter(Response\EmitterInterface $emitter);
public function listen(callable $finalHandler = null);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.