在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:phly/http开源软件地址:https://github.com/phly/http开源编程语言:PHP 100.0%开源软件介绍:phly/http
This package exists:
Installation and RequirementsInstall this library using composer: $ composer require phly/http
UsageUsage will differ based on whether you are writing an HTTP client, or a server-side application. For HTTP client purposes, you will create and populate a For server-side applications, you will create a HTTP ClientsA client will send a request, and return a response. As a developer, you will create and populate the request, and then introspect the response. Both requests and responses are immutable; if you make changes -- e.g., by calling setter methods -- you must capture the return value, as it is a new instance. // 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()); (Note: phly/http does NOT ship with a client implementation; the above is just an illustration of a possible implementation.) Server-Side ApplicationsServer-side applications will need to marshal the incoming request based on superglobals, and will then populate and send a response. Marshaling an incoming requestPHP contains a plethora of information about the incoming request, and keeps that information in a variety of locations. You can call the factory method with or without the following arguments, in the following order:
The method will then return a $request = Phly\Http\ServerRequestFactory::fromGlobals(
$_SERVER,
$_GET,
$_POST,
$_COOKIE,
$_FILES
); Manipulating the responseUse the response object to add headers and provide content for the response. Writing to the body does not create a state change in the response, so it can be done without capturing the return value. Manipulating headers does, however. $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'); "Serving" an application
You can create a server in one of three ways: // 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 callbacks can expect up to three arguments, in the following order:
Once you have your server instance, you must instruct it to listen: $server->listen(); At this time, you can optionally provide a callback to $server->listen(function ($request, $response, $error = null) {
if (! $error) {
return;
}
// do something with the error...
}); Typically, the APIRequest Message
class Request
{
public function __construct(
$uri = null,
$method = null,
$body = 'php://memory',
array $headers = []
);
// See psr/http-message's RequestInterface for other methods
} Requests are immutable. Any methods that would change state -- those prefixed with ServerRequest MessageFor server-side applications, 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.
} The Response Message
class Response
{
public function __construct(
$body = 'php://memory',
$statusCode = 200,
array $headers = []
);
// See psr/http-message's ResponseInterface for other methods
} Like the ServerRequestFactoryThis static class can be used to marshal a // 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
); URI
class Uri
{
public function __construct($uri = '');
// See psr/http-message's UriInterface for other methods.
} Like the various message objects, URIs are immutable. Any methods that would change state -- those prefixed with Stream
If a stream identifier is provided, an optional second parameter may be provided, the file mode by which to
In most cases, you will not interact with the Stream object directly. UploadedFile
In most cases, you will only use the methods defined in the Server
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);
} You can create an instance of the
Emitting responsesIf you are using a non-SAPI PHP implementation and wish to use the SerializationAt times, it's useful to either create a string representation of a message (serialization), or to cast a string or stream message to an object (deserialization). This package provides features for this in
The deserialization methods ( |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论