版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、sr-7_HTTPPSR-7:它的全部含義是什么?psr-7This article was peer reviewed by . Thanks to all of SitePoints peer reviewers for making SitePoint content the best it canbe!本由同評審。 感謝所有SitePoint的同評審員使SitePoint內(nèi)容達(dá)到最佳狀態(tài)!The PHP Framework Interoperability Group (PHP-FIG) has relatively recently approved another proposal
2、, the . The documentcrystallizes HTTP messages into 7 interfaces which a PHP library should implement if they subscribe to the specification. In, Matthew Weier OPhinney, editor of the PSR, gives an interesting overview of the specification. So what is it?PHP框架互操作性組(PHP-FIG)較近期批準(zhǔn)了另項(xiàng)建議,即 。 該檔將HTTP消息具體
3、化為7個接,如果PHP訂閱該規(guī)范,則PHP庫應(yīng)該實(shí)現(xiàn)這些接。 在 ,PSR的編輯Matthew Weier OPhinney對該規(guī)范進(jìn)了有趣的概述。 那是什么If you typebbc.co.uk in your browser, you go straight to the homepage of the BBC,but a number of things might have takenplace between the time the browser sent an HTTP request to the server and it getting a response back.
4、如果在瀏覽器中鍵bbc.co.uk ,則直接轉(zhuǎn)到BBC的主頁,但是在瀏覽器向服務(wù)器發(fā)送HTTP請求到它返回響應(yīng)之間可能發(fā)了很多事情。Heres a sample raw request.這是原始請求例。Accept: */*Referer:Its always made up of a request line (GET / HTTP/1.1), a number of header field lines listed as: , a single emptyline and an optional body for any data payload (for example query p
5、arameters or post data).它始終由請求(GET / HTTP / 1.1),列為: 的多個標(biāo)頭字段,單個空和任何數(shù)據(jù)有效負(fù)載(例如查詢參數(shù)或后置數(shù)據(jù))的可選主體組成)。The blank line after the zero or more header lines must be a CRLF by itself. This means 2 characters an ASCII 13(Carriage Return), followed by ASCII 10 (Line Feed) or.零個或多個標(biāo)題之后的空本必須是CRLF。 這意味著2個字符-ASCII13(
6、回車),然后是ASCII10(換)或rn。Lets send this request from the command line via curl and see the response:讓我們通過curl從命令發(fā)送此請求,并查看響應(yīng):curl -i -H Host: bbc.co.uk -H User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0) -H Accept: */* -X GET http:/bbc.co.ukDate: Sun, 02 Oct 2016 20:49:42 GMTLo
7、cation: http:/www.bbc.co.uk/Connection: Keep-AliveContent-Length: 0curl -i -H Host: www.bbc.co.uk -H User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0) -H Accept: */* -X GET http:/www.bbc.cServer: nginxContent-Type: text/html; charset=utf-8ETag: W/29c44-MXo+6t7MoPRad358MSWqw
8、AX-Frame-Options: SAMEORIGINx-origin-route: xrt-extContent-Length: 171076Date: Sun, 02 Oct 2016 20:54:27 GMTConnection: keep-aliveSet-Cookie: BBC-UID=15c73fe11704a0731344da5ec13869204c1a22a0c7b444d60a708762e631ac0c0Mozilla/5.0%20(compatible%3b%20MSIE%209.0%3b%20X-Cache-Action: HITX-Cache-Hits: 1223X
9、-Cache-Age: 55!-Thats more like it. The first line,HTTP/1.1 200 OK, is the status line. Then we have headers, in a similar pattern to requests: , an empty line, and the response body. Note: Wepassed the output throughless so that we can see the firstpart of the response.這還差不多。 HTTP/1.1 200 OK是狀態(tài)。 然后
10、,我們具有與請求類似的頭件-: value ,空和響應(yīng)主體。 注意:我們將輸出傳遞給less以便我們可以看到響應(yīng)的第部分。Requests and responses can be broken down into a message line, a number of header lines and body lines. Thecommonalities can be abstracted in an interface( essageInterface) which the request RequestInterface) and response(ResponseInterface
11、) can extend with their distinct flavor of HTTP message.請求和響應(yīng)可以分為消息,許多標(biāo)題和正。 可以在接(MessageInterface)中抽象這些共性,請求(RequestInterface )和響應(yīng)(ResponseInterface )可以使其獨(dú)特的HTTP消息RequestInterface進(jìn)擴(kuò)展。PHP doesnt only run within a web environment, and web requests might have originated from APIs. TheServerRequestInter
12、face was designed to take care of other types of HTTP requests.PHP不僅在絡(luò)環(huán)境中運(yùn),且絡(luò)請求可能源API。ServerRequestInterface旨在處理其他類型的HTTP請求。The other three interfaces are a further abstraction of specific aspects in messages. Looking back at the request messageline:其他三個接是消息中特定的進(jìn)步抽象。 回顧請求消息:GET / HTTP/1.1This compri
13、ses:這包括:METHOD:Although defines safe and idempotent types of methods, for general applications, its sufficient to identifythem by name GET, POST, PUT, PATCH,DELETE, OPTIONS, HEAD and TRACE.CONNECT is reserved for use with aproxy that can dynamically switch to being a tunnel e.g. SSL tunneling.法:盡管定義
14、了安全和冪等的法類型,但對于般應(yīng)程序,通過名稱來識別它們就夠了-GET,POST,PUT,PATCH,DELETE,OPTIONS,HEAD和TRACE。 CONNECT保留供與代理起使,該代理可以動態(tài)切換為隧道,例如SSL隧道。TARGET: This is the URIor our request target, and things get a bit interesting here as we can have:標(biāo):這是URI或我們的請求標(biāo),在這,事情變得有些有趣,因?yàn)槲覀兛梢該碛校簅rigin-form path and query string of the URI. The
15、query string may or may not be present.origin-form URI的路徑和查詢字符串。 查詢字符串可能存在也可能不存在。absolute-form an absolute URI.絕對形式絕對URI。authority-form the authority part of a URI, made up of a maximum of 3 parts user-info (optional), host andport (optional). The user-info may require a password too user:password.
16、Weend up with a pattern ofuser:passwordhost:port. The user-info may also have require an權(quán)威形式 URI的權(quán)威部分,最多由3部分組成戶信息(可選),主機(jī)和端(可選)。 戶信息可能也需要密碼 user:password。 我們以user:password host:port的模式結(jié)束。 戶信息可能還需要個asterisk-form just the string, *星號形式僅是字符串,*Weend up withscheme:/user:passwordhost:port/path?query#fragme
17、nThis part of the request message wasabstracted to theUriInterface.我們以以下scheme:/user:passwordhost:port/path?query#fragment 束scheme:/user:passwordhost:port/path?query#fragment。 請求消息的這部分被抽象到UriInterface 。VERSION: Theres limited choice here as HTTP/1.1 is the current version. Prior to that, we had HTTP
18、/1.0, and the nextdraft version is HTTP/2.0版本:這的選擇有限,因?yàn)镠TTP / 1.1是當(dāng)前版本。 在此之前,我們使HTTP / 1.0,下個草案版本是HTTP / 2.0Also, file uploads required special attention. In non-SAPI environments, $_FILES environment variable is empty and insome situations such as non-POST requests,$_FILES isnt populated at all. Th
19、eUploadedFileInterface was designed to allowfor a more consistent interaction with files.另外,件上傳需要特別注意。 在SAPI環(huán)境中,$_FILES環(huán)境變量為空,在某些情況下(例如POST請求),根本不會填充$_FILES 。UploadedFileInterface旨在允許與件進(jìn)更致的交互。The message (request or response) needs to be able to handle large data efficiently for both client and serv
20、er. PHP has hadstreams built in since 4.3.0. TheStreamInterfacepart of PSR-7 provides a wrapper around common operations andserialization of an entire stream to a string.消息(請求或響應(yīng))需要能夠有效地為客戶端和服務(wù)器處理量數(shù)據(jù)。 從4.3.0開始,PHP內(nèi)置了流。 PSR-7的StreamInterface部分提供了通操作以及將整個流序列化為字符串的包裝。挑戰(zhàn)性 (Challenges)The path to PSR-7 w
21、as paved with strong debate and differing opinions at every step of the way.通往PSR-7的道路在每個步驟中都經(jīng)過激烈的辯論和不同的意見。Immutable objects The immutability or mutability of objects was one of the most hotly-debated points, and PSR-7eventually settled for this:不可變的對象對象的不可變性或可變性是最熱門的爭論點(diǎn)之,PSR-7最終為此解決了:Messages are v
22、alues where the identity is the aggregate of all parts of the message; a change to any aspect of themessage is essentially a new message. This is the very definition of a value object. The practice by which changesresult in a new instance is termed immutability, and is a feature designed to ensure t
23、he integrity of a given value.消息是值,其中份是消息所有部分的總和; 對消息的任何進(jìn)的更改本質(zhì)上都是新消息。 這就是值對象的定義。 更改導(dǎo)致新實(shí)例的做法稱為不變性,并且是旨在確保給定值完整性的功能。What this means is that each time you make any change to a message object, you end up with a new copy. Thecomplexity of URI, headers and streams require an assurance that a full adoption
24、 of immutability by all collaboratorsoffered to the designers of the interfaces.這意味著每次對消息對象進(jìn)任何更改時,都會得到個新副本。 URI,標(biāo)頭和流的復(fù)雜性要求確保提供給接設(shè)計(jì)員的所有協(xié)作者都完全采不變性。With immutability, any state change you make requires you to assign the result.有了不變性,您所做的任何狀態(tài)更改都需要您分配結(jié)果。$request =$request-setHeader(Cache-Control, public)
25、;Any method that changes the state of the current message returns an instance of it with the changes made.As long asa result assignment is made,you can chain any number of changes in a fluent-like syntax.更改當(dāng)前消息狀態(tài)的任何法都將返回所做更改的實(shí)例。 只要進(jìn)了結(jié)果分配,就可以使流利的語法鏈接任意數(shù)量的更改。-setHeader(Cache-Control, public)-addHeader
26、(Cache-Control, max-age=18600)-setStatus(200);The methods having the prefix “with” must conform to the immutability requirement. A concern is that supportingmutability in one of the message interfaces means enforcing it across all the other interfaces.前綴為“ with”的法必須符合不變性要求。 個問題是,在消息接之中持可變性意味著要在所有其他接
27、上強(qiáng)制實(shí)現(xiàn)可變性。On the other hand, it has been highlighted that as they are generally thought to be. Its worthy of note that similarHTTP object implementations in Ruby and Node are mutable by design. So, PHP is in good company.另,已經(jīng)強(qiáng)調(diào)了通常認(rèn)為的 。 值得注意的是,Ruby和Node中類似的HTTP對象實(shí)現(xiàn)在設(shè)計(jì)上是可變的。 因此,PHP表現(xiàn)出。Nomenclature Thes
28、e objects are designed as interfaces. Isnt calling it MessageInterface superfluous? A methodsignature taking a request and response ends up being too long. Compare the following:命名法這些對象被設(shè)計(jì)為接。 這不是MessageInterface多余的嗎? 接受請求和響應(yīng)的法簽名最終太長。 較以下內(nèi)容:public function _invoke(ServerRequest $request, Response $re
29、sponse, callable $next) : ResponseAliasing is the suggested solution for those who prefer to drop the Interface suffixes.對于那些喜歡刪除接后綴的戶,建議使別名解決案。Middleware Weve got the message interfaces which are like the ingredients for making a cake, perfectly measuredout. I want a cake to consume but I dont know
30、 how to bake. How about a recipe? PSR-7 only prescribes a standard forthe definition of a request and response. How do we move from request to response? That “middleman” that doesthe leg work between the request and response is called middleware.中間件我們有消息接,就像制作蛋糕的原料樣,經(jīng)過精設(shè)計(jì)。 我想吃塊蛋糕,但我不知道怎么烤。 譜怎么樣? PSR
31、-7僅規(guī)定了定義請求和響應(yīng)的標(biāo)準(zhǔn)。 我們?nèi)绾螐恼埱筠D(zhuǎn)移到響應(yīng)? 在請求和響應(yīng)之間起作的那個“中間”稱為中間件。The next step would be a unification of how to plumb PSR-7 interfaces so that applications and frameworks thatconform to them can be completely swappable. This part of the debate which borders on providing a template for aninteroperable implemen
32、tation of PSR-7, has been moved to the separate PSR-15: HTTP Middlewares.下步將是如何使PSR-7接垂直化,以便符合它們的應(yīng)程序和框架可以完全交換。 辯論的這部分與為PSR-7的互操作實(shí)現(xiàn)提供模板有關(guān),已經(jīng)轉(zhuǎn)移到單獨(dú)的PSR-15:HTTP中間件。法 (Usage)A number of libraries and frameworks have added support for PSR-7 in different ways.許多庫和框架以不同的式增加了對PSR-7的持。1. Symfony The is one of
33、 the most popular OOP abstractions of the HTTP specification prior to PHP-FIG. With theemergence of PSR-7, Symfony opted for a which convertsHttpFoundation objects from and to objects which implementPSR-7 message interfaces.Symfony 是PHP-FIG之前HTTP規(guī)范中最流的OOP抽象之。 隨著PSR-7的出現(xiàn),Symfony選擇了種 ,HttpFoundation將H
34、ttpFoundation對象與實(shí)現(xiàn)PSR-7消息接的對象相互轉(zhuǎn)換。2. Zend Framework (ZF) They came up with a Composer package,endframework/zend-diactoros, of implementations ofthe HTTP message interfaces, not surprising as the editor of PSR-7 is the ZF Project Lead. goes a step further byincluding a server that is similar to the o
35、ne provided by in NodeJS.Zend Framework(ZF)他們提出了HTTP消息接實(shí)現(xiàn)的Composer軟件包zendframework/zend-diactoros ,這并不奇怪,因?yàn)镻SR-7的編輯者是ZF項(xiàng)負(fù)責(zé)。 通過在中包含與提供的服務(wù)器相似的服務(wù)器,進(jìn)步了步。3. Slim describes a flexible approach that makes Slim accept any implementation of PSR-7. That is, if the one providedby Slim doesnt suffice.Slim 描述了種靈
36、活的法,可使Slim接受PSR-7的任何實(shí)現(xiàn)。 也就是說,如果Slim提供的功能還不夠。4. Guzzle Being an HTTP client library, PSR-7 is of great relevance to this library. They built theguzzlehttp/psr7Composer package, a message implementation of PSR-7, which they rely on. gives an excellent overview of their takeon the specification.Guzzle
37、作為HTTP客戶端庫,PSR-7與該庫常相關(guān)。 他們構(gòu)建了guzzlehttp/psr7 Composer軟件包,這是他們依賴的PSR-7的消息實(shí)現(xiàn)。 很好地概述了它們對規(guī)范的看法。5. Aura Theyve included , a web router implementation of PSR-7 in their collection of independent library packages.All router objects are managed by RouterContainer from which you retrieve an instance of aMap
38、object. Each methodof thisMap object may accept an HTTP message interface as one of its arguments.Aura 他們在獨(dú)庫包的集合中包含了 -7的Web路由器實(shí)現(xiàn) 。 所有路由器對象都由個RouterContainer管理,您可以從中檢索Map對象的實(shí)例。 Map對象的每種法都可以接受HTTP消息接作為其參數(shù)之。6. HTTPlug A package of interfaces that allow library and application developers to create HTTP
39、clients that are fullycompatible with PSR-7. The HttpClient sends a PSR-7 Request and returns a PSR-7 response. See for a practical wayof using this package.HTTPlug 組接,允許庫和應(yīng)程序開發(fā)員創(chuàng)建與PSR-7完全兼容的HTTP客戶端。 HttpClient發(fā)送PSR-7請求并返回PSR-7響應(yīng)。 有關(guān)使此軟件包的實(shí)法,請參閱 。Packagist has a with a wide range of popularity or ac
40、ceptance. However, a developer still has the following usage options:Packagist提供了并受到泛的歡迎或接受。 但是,開發(fā)員仍然具有以下使選項(xiàng):1. Direct As the specification states, while PsrHttpMessageMessageInterface MAY be implemented directly,implementors SHOULD implement PsrHttpMessageRequestInterface and PsrHttpMessageResponse
41、Interface.The simplest way is to install the package with Composer.直接規(guī)范指出,雖然可以直接實(shí)現(xiàn)Psr Http Message MessageInterface,但是實(shí)現(xiàn)者應(yīng)該實(shí)現(xiàn)Psr Http Message RequestInterface和Psr Http Message ResponseInterface。 最簡單的法是使Composer安裝軟件包。composer require psr/http-message2. Indirect The interfaces are used indirectly through an adapter. For example, PSR-7 support in Symfony is throughthe
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年貴州岑鞏縣糧食購銷公司招聘筆試參考題庫含答案解析
- 2025年山西忻州五寨縣醫(yī)療集團(tuán)招聘筆試參考題庫含答案解析
- 2025年安徽金安區(qū)所屬城投公司招聘筆試參考題庫含答案解析
- 2025年廣東佛山市三水發(fā)展公司招聘筆試參考題庫含答案解析
- 二零二五年度碳排放權(quán)交易與托管合同2篇
- 寧波市公共交通集團(tuán)限公司第三分公司招聘高頻重點(diǎn)提升(共500題)附帶答案詳解
- 二零二五年房地產(chǎn)租賃經(jīng)紀(jì)服務(wù)合作協(xié)議3篇
- 國網(wǎng)寧夏電力限公司2025年高校畢業(yè)生招聘(第一批)高頻重點(diǎn)提升(共500題)附帶答案詳解
- 二零二五年汽車展覽活動宣傳服務(wù)全文合同3篇
- 國家統(tǒng)計(jì)局張家界調(diào)查隊(duì)(湖南?。┒裙_招考2名公益性崗位人員高頻重點(diǎn)提升(共500題)附帶答案詳解
- Q∕GDW 12147-2021 電網(wǎng)智能業(yè)務(wù)終端接入規(guī)范
- 公園廣場綠地文化設(shè)施維修改造工程施工部署及進(jìn)度計(jì)劃
- 塑料件缺陷匯總
- 2020年的中國海外工程示范營地申報(bào)材料及評分標(biāo)準(zhǔn)
- 跌落測試(中文版)-ISTA-2A-2006
- ppt魚骨圖模板圖
- t.c.electronic M-One XL 快速操作指南
- 右心導(dǎo)管檢查及心血管造影ppt課件
- 乳膠漆質(zhì)量檢驗(yàn)批驗(yàn)收記錄
- 大氣課程設(shè)計(jì)---袋式除塵器
- WTY-53,54說明書
評論
0/150
提交評論