weew / http
基本 HTTP 层。
v1.13.0
2016-11-03 17:04 UTC
Requires
- weew/contracts: ^1.1
- weew/helpers-array: ^1.0
- weew/helpers-string: ^1.0
- weew/json-encoder: ^1.0
- weew/url: ^2.0
Requires (Dev)
- phpunit/phpunit: ^4.7
- satooshi/php-coveralls: ^0.6.1
- dev-master
- v1.13.0
- v1.12.0
- v1.11.0
- v1.10.1
- v1.10.0
- v1.9.1
- v1.9.0
- v1.8.0
- v1.7.0
- v1.6.0
- v1.5.1
- v1.5.0
- v1.4.0
- v1.3.0
- v1.2.1
- v1.2.0
- v1.1.0
- v1.0.0
- v0.1.19
- v0.1.18
- v0.1.17
- v0.1.16
- v0.1.15
- v0.1.14
- v0.1.13
- v0.1.12
- v0.1.11
- v0.1.10
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- v0.0.11
- v0.0.10
- v0.0.9
- v0.0.8
- v0.0.7
- v0.0.6
- v0.0.5
- v0.0.4
- v0.0.3
- v0.0.2
- v0.0.1
This package is not auto-updated.
Last update: 2024-09-14 18:47:45 UTC
README
目录
安装
composer require weew/http
响应
基本响应
$response = new HttpResponse(); $response->send();
HTTP/1.1 200 OK
Host: localhost
Connection: close
内容
$response = new HttpResponse(); $response->setContent('<h1>Hello World!</h1>'); $response->send();
HTTP/1.1 200 OK
Host: localhost
Connection: close
content-type: text/html
<h1>Hello World!</h1>
状态码
$response = new HttpResponse(HttpStatusCode::UNAUTHORIZED); // or $response = new HttpResponse(401); $response->send();
HTTP/1.1 401 Unauthorized
Host: localhost
Connection: close
头部
$response = new HttpResponse(); $response->getHeaders()->set('foo', 'bar'); $response->send();
HTTP/1.1 200 OK
Host: localhost
Connection: close
foo: bar
Cookie
$response = new HttpResponse(); $response->getQueuedCookies()->add(new Cookie('foo', 'bar')); $response->send();
HTTP/1.1 200 OK
Host: localhost
Connection: close
set-cookie: foo=bar; path=/; httpOnly
自定义响应
HtmlResponse
$response = new HtmlResponse(); $response->setHtmlContent('<h1>Hello World!</h1>'); $response->send();
HTTP/1.1 200 OK
Host: localhost
Connection: close
content-type: text/html
<h1>Hello World!</h1>
JsonResponse
$response = new JsonResponse(); $response->getData()->set('Hello', 'World!'); $response->send();
HTTP/1.1 200 OK
Host: localhost
Connection: close
content-type: application/json
{"Hello":"World!"}
BasicAuthResponse
$response = new BasicAuthResponse('Please login'); $response->send();
HTTP/1.1 200 OK
Host: localhost
Connection: close
www-authenticate: basic realm="Please login"
请求
基本请求
构建自定义请求非常简单。
$request = new HttpRequest( HttpRequestMethod::POST, new Url('http://example.com') ); $request->setContent('foo=bar');
GET 参数
$request = new HttpRequest(); $request->getUrl()->getQuery()->set('foo', 'bar'); echo $request->getUrl()->getQuery(); // foo=bar
POST 数据
$request = new HttpRequest(); $request->getData()->set('foo', 'bar'); $request->getData()->set('bar', 'foo'); echo $request->getContent(); // foo=bar&bar=foo
头部
你可以像在 HttpResponse 类中一样访问头部信息。
$request = new HttpRequest(); $request->getHeaders()->set('foo', 'bar'); $request->getHeaders()->add('foo', 'foo'); var_dump($request->getHeaders()->get('foo')); // ['bar', 'foo'] echo $request->getHeaders()->find('foo'); // foo
当前请求
有时,有一个表示当前接收到的 HTTP 请求的对象会很有用。
$request = new CurrentRequest(); var_dump($request->toArray()); // all the data that the server received from the client
基本认证
通过基本认证认证请求非常简单。
$request = new HttpRequest(); $request->getBasicAuth()->setUsername('foo'); $request->getBasicAuth()->setPassword('bar'); echo $request->getBasicAuth()->getToken(); // Zm9vOmJhcg== echo $request->getHeaders()->find('authentication'); // Basic Zm9vOmJhcg==
相关项目
- URL:在整个项目中使用。
- HTTP Client:一个简单的 HTTP 客户端,允许你发送和接收标准化的 HttpRequest 和 HttpResponse 对象。