weew/http
Composer 安装命令:
composer require weew/http
包简介
Basic http layer.
README 文档
README
Table of contents
Installation
composer require weew/http
Responses
Basic response
$response = new HttpResponse(); $response->send();
HTTP/1.1 200 OK
Host: localhost
Connection: close
Content
$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>
Status codes
$response = new HttpResponse(HttpStatusCode::UNAUTHORIZED); // or $response = new HttpResponse(401); $response->send();
HTTP/1.1 401 Unauthorized
Host: localhost
Connection: close
Headers
$response = new HttpResponse(); $response->getHeaders()->set('foo', 'bar'); $response->send();
HTTP/1.1 200 OK
Host: localhost
Connection: close
foo: bar
Cookies
$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
Custom responses
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"
Requests
Basic request
It is very easy to build a custom request.
$request = new HttpRequest( HttpRequestMethod::POST, new Url('http://example.com') ); $request->setContent('foo=bar');
GET parameters
$request = new HttpRequest(); $request->getUrl()->getQuery()->set('foo', 'bar'); echo $request->getUrl()->getQuery(); // foo=bar
POST data
$request = new HttpRequest(); $request->getData()->set('foo', 'bar'); $request->getData()->set('bar', 'foo'); echo $request->getContent(); // foo=bar&bar=foo
Headers
You can access headers the same way as on the HttpResponse class.
$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
Current Request
Sometimes it is nice to have an object that would represent the current received http request.
$request = new CurrentRequest(); var_dump($request->toArray()); // all the data that the server received from the client
Basic Authentication
It is very easy to authenticate a request via basic auth.
$request = new HttpRequest(); $request->getBasicAuth()->setUsername('foo'); $request->getBasicAuth()->setPassword('bar'); echo $request->getBasicAuth()->getToken(); // Zm9vOmJhcg== echo $request->getHeaders()->find('authentication'); // Basic Zm9vOmJhcg==
Related Projects
- URL: used throughout the project.
- HTTP Client: a simple http client that allows you to send and receive the standardized HttpRequest and HttpResponse objects.
weew/http 适用场景与选型建议
weew/http 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 466 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 07 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 weew/http 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 weew/http 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 466
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 7
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-07-16