• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

PatrickLouys/http: Http component

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

PatrickLouys/http

开源软件地址:

https://github.com/PatrickLouys/http

开源编程语言:

PHP 100.0%

开源软件介绍:

Http Component

Build Status Coverage Status Scrutinizer Code Quality Latest Stable Version Total Downloads License

Installation

You can use composer to install this component. The package is:

patricklouys/http

Basic Usage

Request

The Request class provides an object oriented wrapper around the PHP superglobals. This makes it possible to inject it as a dependency into any of your classes that require it.

use Http\HttpRequest;

$request = new HttpRequest($_GET, $_POST, $_COOKIE, $_FILES, $_SERVER, file_get_contents('php://input'));

Now you can use the following methods on the $request object:

$request->getParameter($key, $defaultValue = null);
$request->getFile($key, $defaultValue = null);
$request->getCookie($key, $defaultValue = null);
$request->getParameters();
$request->getQueryParameters();
$request->getBodyParameters();
$request->getRawBody();
$request->getCookies();
$request->getFiles();
$request->getMethod();
$request->getHttpAccept();
$request->getReferer();
$request->getUserAgent();
$request->getIpAddress();
$request->isSecure();
$request->getQueryString();

Please note that both GET and POST parameters are merged together and accessible with getParameter.

Response

The HttpResponse object is the data holder for the HTTP response. It has no constructor dependencies and can be instantiated with just:

use Http\HttpResponse;

$response =  new HttpResponse;

The response can be modified with following methods:

$response->setStatusCode($statusCode, $statusText = null);
$response->addHeader($name, $value);
$response->setHeader($name, $value);
$response->addCookie(Cookie $cookie);
$response->deleteCookie(Cookie $cookie);
$response->setContent($content);
$response->redirect($url);

If you don't supply a status text with setStatusCode then an appropriate default status text will be selected for the HTTP status code if available.

addHeader adds a new header value without overwriting existing values, setHeader will overwrite an existing value.

The redirect method will set the status code and text for a 301 redirect.

deleteCookie will set the cookie content to nothing and put the expiration in the past.

The following two methods are available to send the response to the client:

$response->getHeaders();
$response->getContent();

They can be used like this:

foreach ($response->getHeaders() as $header) {
    header($header, false);
}

echo $response->getContent();

The second parameter of header must be false. Otherwise existing headers will be overwritten.

Cookies

To avoid new calls in your classes and to have the ability to set default cookie settings for you application, there is a CookieBuilder class that you can use to create your cookie objects. It has the following methods available:

$cookieBuilder->setDefaultDomain($domain); // defaults to NULL
$cookieBuilder->setDefaultPath($path); // defaults to '/'
$cookieBuilder->setDefaultSecure($secure); // defaults to TRUE
$cookieBuilder->setDefaultHttpOnly($httpOnly); // defaults to TRUE
$cookieBuilder->build($name, $value); // returns the cookie object

You can use the following methods to manipulate an existing cookie:

$cookie->setValue($value);
$cookie->setMaxAge($seconds);
$cookie->setDomain($domain);
$cookie->setPath($path);
$cookie->setSecure($secure);
$cookie->setHttpOnly($httpOnly);

The cookie object can the be used with the HttpResponse methods addCookie and deleteCookie.

Example

<?php

use Http\HttpRequest;
use Http\HttpResponse;
use Http\CookieBuilder;

$loader = require_once __DIR__ . '/vendor/autoload.php';

$cookieBuilder = new CookieBuilder;

// Disable the secure flag because this is only an example
$cookieBuilder->setDefaultSecure(false);

$request = new HttpRequest($_GET, $_POST, $_COOKIE, $_FILES, $_SERVER, file_get_contents('php://input'));
$response = new HttpResponse;

$content = '<h1>Hello World</h1>';
$content .= $request->getCookie('TestCookie', 'Cookie is not set.');

if ($request->getParameter('setCookie') === 'true') {
    $cookie = $cookieBuilder->build('TestCookie', 'Cookie is set.');
    $response->addCookie($cookie);
}

$response->setContent($content);

foreach ($response->getHeaders() as $header) {
    header($header);
}

echo $response->getContent();



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap