cakephp / http
CakePHP HTTP客户端和PSR7/15中间件库
5.1.0
2024-09-13 16:05 UTC
Requires
- php: >=8.1
- cakephp/core: ^5.1
- cakephp/event: ^5.1
- cakephp/utility: ^5.1
- composer/ca-bundle: ^1.5
- laminas/laminas-diactoros: ^3.3
- laminas/laminas-httphandlerrunner: ^2.6
- psr/http-client: ^1.0.2
- psr/http-factory: ^1.1
- psr/http-message: ^1.1 || ^2.0
- psr/http-server-handler: ^1.0.2
- psr/http-server-middleware: ^1.0.2
Requires (Dev)
- cakephp/cache: ^5.1
- cakephp/console: ^5.1
- cakephp/i18n: ^5.1
- cakephp/orm: ^5.1
- paragonie/csp-builder: ^3.0
Suggests
- cakephp/cache: To use cache session storage
- cakephp/orm: To use database session storage
- paragonie/csp-builder: To use CspMiddleware
- 5.x-dev
- 5.1.0
- 5.1.0-RC2
- 5.1.0-RC1
- 5.0.11
- 5.0.10
- 5.0.9
- 5.0.8
- 5.0.7
- 5.0.6
- 5.0.5
- 5.0.4
- 5.0.3
- 5.0.2
- 5.0.0
- 5.0.0-RC2
- 5.0.0-RC1
- 5.0.0-beta2
- 5.0.0-beta1
- 4.x-dev
- 4.5.7
- 4.5.6
- 4.5.5
- 4.5.4
- 4.5.3
- 4.5.2
- 4.5.1
- 4.5.0
- 4.5.0-RC1
- 4.4.17
- 4.4.16
- 4.4.15
- 4.4.14
- 4.4.13
- 4.4.12
- 4.4.11
- 4.4.10
- 4.4.9
- 4.4.8
- 4.4.7
- 4.4.6
- 4.4.5
- 4.4.4
- 4.4.3
- 4.4.2
- 4.4.1
- 4.4.0
- 4.4.0-RC2
- 4.4.0-RC1
- 4.3.x-dev
- 4.3.11
- 4.3.10
- 4.3.9
- 4.3.8
- 4.3.6
- 4.3.5
- 4.3.4
- 4.3.3
- 4.3.2
- 4.3.1
- 4.3.0
- 4.3.0-RC4
- 4.3.0-RC3
- 4.3.0-RC2
- 4.3.0-RC1
- 4.2.x-dev
- 4.2.12
- 4.2.11
- 4.2.10
- 4.2.9
- 4.2.8
- 4.2.7
- 4.2.6
- 4.2.5
- 4.2.4
- 4.2.3
- 4.2.2
- 4.2.1
- 4.2.0
- 4.2.0-RC1
- 4.2.0-beta1
- 4.1.7
- 4.1.6
- 4.1.5
- 4.1.4
- 4.1.3
- 4.1.2
- 4.1.1
- 4.1.0
- 4.1.0-RC2
- 4.1.0-RC1
- 4.1.0-beta1
- 4.0.x-dev
- 4.0.9
- 4.0.8
- 4.0.7
- 4.0.6
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- 4.0.0-RC2
- 4.0.0-RC1
- dev-5.next
- dev-4.next
- dev-master
This package is auto-updated.
Last update: 2024-09-21 03:06:00 UTC
README
CakePHP Http库
此库提供PSR-15 Http中间件服务器,PSR-7请求和响应对象,以及PSR-18 Http客户端。这些类共同允许您处理传入的服务器请求和发送出站HTTP请求。
使用HttpClient
发送请求很简单。进行GET请求看起来像
use Cake\Http\Client; $http = new Client(); // Simple get $response = $http->get('http://example.com/test.html'); // Simple get with querystring $response = $http->get('http://example.com/search', ['q' => 'widget']); // Simple get with querystring & additional headers $response = $http->get('http://example.com/search', ['q' => 'widget'], [ 'headers' => ['X-Requested-With' => 'XMLHttpRequest'], ]);
要了解更多信息,请阅读HttpClient文档。
使用Http服务器
Http服务器允许HttpApplicationInterface
处理请求并发出响应。要开始,首先实现Cake\Http\HttpApplicationInterface
。一个最小示例可能如下所示
namespace App; use Cake\Core\HttpApplicationInterface; use Cake\Http\MiddlewareQueue; use Cake\Http\Response; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; class Application implements HttpApplicationInterface { /** * Load all the application configuration and bootstrap logic. * * @return void */ public function bootstrap(): void { // Load configuration here. This is the first // method Cake\Http\Server will call on your application. } /** * Define the HTTP middleware layers for an application. * * @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to set in your App Class * @return \Cake\Http\MiddlewareQueue */ public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue { // Add middleware for your application. return $middlewareQueue; } /** * Handle incoming server request and return a response. * * @param \Psr\Http\Message\ServerRequestInterface $request The request * @return \Psr\Http\Message\ResponseInterface */ public function handle(ServerRequestInterface $request): ResponseInterface { return new Response(['body'=>'Hello World!']); } }
一旦您有一个带有中间件的应用程序,您就可以开始接收请求。在应用程序的webroot中,您可以添加一个index.php
来处理请求
<?php // in webroot/index.php require dirname(__DIR__) . '/vendor/autoload.php'; use App\Application; use Cake\Http\Server; // Bind your application to the server. $server = new Server(new Application()); // Run the request/response through the application and emit the response. $server->emit($server->run());
然后您可以使用PHP的内置Web服务器来运行您的应用程序
php -S localhost:8765 -t ./webroot ./webroot/index.php
有关中间件的更多信息,请查阅文档