ããŸãã«ãåçŽããããã¬ãŒã³ããŒã·ã§ã³ãšè€éããããã¬ãŒã³ããŒã·ã§ã³ã®äž¡æ¹ã«ã€ããŠãç§ã¯ãŸã æèŠãèããŠããŸãã ãããããã®æçš¿ãæžãããçç±ã§ã-å ¬éãããæšå¥šäºé ã®äœ¿çšãå®èšŒããåæã«ããããæäŸããã·ã³ãã«ããšå®å šæ§ãšä¿¡é Œæ§ãåæã«ç€ºãããã§ãã
ãŸãããã®æšæºãèŠå¶ãããã®ã«ã€ããŠç°¡åã«èª¬æããŸãã
HTTPã¡ãã»ãŒãž
HTTPã¯ããªãåçŽãªãããã³ã«ã§ãã ãããäœå¹Žãã®ééŠå°Ÿãã䜿çšãããŠããçç±ã§ãã ãã®äžã®ã¡ãã»ãŒãžã®æ§é ã¯æ¬¡ã®ãšããã§ãã
<message line> Header: value Another-Header: value Message body
èŠåºãã¯ããŒãšå€ã®ãã¢ã§ãã ããŒã§ã¯å€§æåãšå°æåãåºå¥ãããŸãã å€ã¯æååã§ãã 1ã€ã®ã¿ã€ãã«ã«ã¯ããã€ãã®æå³ããããŸãã ãã®å Žåãå€ã¯éåžžãã³ã³ãåºåããªã¹ãã§è¡šãããŸãã
ã¡ãã»ãŒãžæ¬æã¯æååã§ãã éåžžããµãŒããŒãšã¯ã©ã€ã¢ã³ãã«ãã£ãŠã¹ããªãŒã ãšããŠæ±ãããã¡ã¢ãªæ¶è²»éãšåŠçè² è·ãåæžãããŸãã ããã¯ã倧ããªããŒã¿ã»ããã転éããå Žåãç¹ã«ãã¡ã€ã«ã転éããå Žåã«éåžžã«éèŠã§ãã ããšãã°ããã®ãŸãŸäœ¿çšã§ããPHPã¯ãçä¿¡èŠæ±æ¬æãphpïŒ//å ¥åã¹ããªãŒã ãšããŠè¡šããåºåãããã¡ãŒïŒæ£åŒã«ã¯ã¹ããªãŒã ïŒã䜿çšããŠå¿çãè¿ããŸãã
ã¡ãã»ãŒãžè¡ã¯ãHTTPãªã¯ãšã¹ããšã¬ã¹ãã³ã¹ãåºå¥ããå Žæã§ãã
èŠæ±ã®ã¡ãã»ãŒãžè¡ïŒä»¥éãèŠæ±è¡ãšåŒã³ãŸãïŒã®åœ¢åŒã¯æ¬¡ã®ãšããã§ãã
METHOD request-target HTTP/VERSION
ã¡ãœããïŒãMETHODãïŒããªã¯ãšã¹ãã®ã¿ã€ãã決å®ããå ŽåïŒGETãPOSTãPUTãPATCHãDELETEãOPTIONSãHEADãªã©ããããã³ã«ããŒãžã§ã³ïŒãVERSIONãïŒã¯éåžž1.0ãŸãã¯1.1ïŒææ°ã®Webã¯ã©ã€ã¢ã³ãã§ã¯1.1ïŒã§ãã ãããŠããªã¯ãšã¹ãïŒããªã¯ãšã¹ãã¿ãŒã²ãããïŒã®ç®çã«é¢ããŠãç§ãã¡ã¯ãã詳现ã«èª¬æããŸãã
èŠæ±ã®ç®çã¯æ¬¡ã®ããã«è¡šãããšãã§ããŸãã
- ãã¹ãšã¯ãšãªæååïŒæäŸãããŠããå ŽåïŒã§ããæšæºåœ¢åŒãã€ãŸã URI ïŒUniversal Resource IdentifierïŒã
- 絶察URIã§ãã絶察圢åŒã
- æ¿èªã«å¿ èŠãªURIã®äžéšã§ããæ¿èªãã©ãŒã ïŒãŠãŒã¶ãŒæ å ±ïŒæäŸãããŠããå ŽåïŒããã¹ããããã³ããŒãïŒæšæºã§ãªãå ŽåïŒïŒã
- *ã®åœ¢åŒãã€ãŸããæåã*ãã§æ§æãããæååã
éåžžãã¯ã©ã€ã¢ã³ãã¯ãHTTPãµãŒããŒã«æ¥ç¶ããæåã®èŠæ±ã§ã®ã¿ãæ¿èªã®ããã«ããŒã¿ã転éããŸãã 次ã«ããªãœãŒã¹ïŒèªèšŒããŒã¿ãªãã®URIïŒãžã®çžå¯ŸïŒãŸãã¯çµ¶å¯ŸïŒãã¹ãèŠæ±ã¿ãŒã²ãããšããŠéä¿¡ãããŸãã ãããã£ãŠãèªèšŒããŒã¿ã¯æ¥ç¶èŠæ±ïŒCONNECTã¡ãœããïŒã«å¯ŸããŠã®ã¿éä¿¡ãããŸããããã¯éåžžããããã·ãµãŒããŒã§äœæ¥ãããšãã«å®è¡ãããŸãã ã*ãæåã¯OPTIONSã¡ãœããã§äœ¿çšãããWebãµãŒããŒã«é¢ããäžè¬æ å ±ãååŸããŸãã
èŠããã«ãã¯ãšãªã®ç®çã«ã¯å€ãã®çšéããããŸãã
ããŠãããªããå®å šã«æ··ä¹±ãããããã«ãURIãèæ ®ããŠãã ããã 以äžã衚瀺ãããŸãã
<scheme>://<authority>[/<path>][?<query string>]
httpãªã¯ãšã¹ãã®ãã¹ããŒã ãã¯ãhttpãŸãã¯httpsã«ãªããŸãã ããã¹ãã¯èª°ã«ãšã£ãŠãç解å¯èœãªéšåã§ããããŸãã ãããããæš©éããšã¯äœã§ããïŒ
[user-info@]host[:port]
ãæš©éãã«ã¯åžžã«ãã¹ããå«ãŸããŸãããã¹ãã¯ãã¡ã€ã³åãŸãã¯IPã¢ãã¬ã¹ã§ãã ããŒãã¯ãªãã·ã§ã³ã§ããããã®åç·ã®æšæºã§ã¯ãªãå ŽåïŒãŸãã¯åç·ãäžæãªå ŽåïŒã«ã®ã¿å¿ èŠã§ãã ãŠãŒã¶ãŒæ å ±ã¯æ¬¡ã®ããã«è¡šç€ºãããŸã
user[:pass]
ãã¹ã¯ãŒãã¯ãªãã·ã§ã³ã§ãã å®éãæ¢åã®ä»æ§ã§ã¯ãURIã§ãã¹ã¯ãŒãããŸã£ãã䜿çšããªãããšããå§ãããŸãã ã¯ã©ã€ã¢ã³ããããã¹ã¯ãŒãã匷å¶ããããšããå§ãããŸãã
ã¯ãšãªæååã¯ãã¢ã³ããµã³ãã§åºåãããããŒãšå€ã®ãã¢ã®ã»ããã§ãã
?foo=bar&baz&quz=1
å®è£ èšèªã«å¿ããŠããªã¹ããŸãã¯é åãã¢ãã«åããããšãã§ããŸãã
?sort[]=ASC&sort[]=date&filter[product]=name
PHPã¯ããã®æååã2次å é åã«å€æããŸãã
[ 'sort' => [ 'ASC', 'date' ], 'filter' => [ 'product' => 'name' ], ]
ãã®ããããªã¯ãšã¹ãã®ç®æšã圢æããæè»æ§ãååã§ãªãå ŽåãURIã¯ç¬èªã«æäŸããŸãã
幞ããªããšã«ãHTTPãµãŒããŒã®å¿çã¯ç°¡åã§ãã å¿çè¡ã¯æ¬¡ã®ãšããã§ãã
HTTP/VERSION <status>[ <reason>]
åè¿°ã®ããã«ããããŒãžã§ã³ãã¯éåžž1.0ããŸãã¯ããé »ç¹ã«1.1ã§ãã ãã¹ããŒã¿ã¹ãã¯100ãã599ãŸã§ã®æ°åã§ãã ãçç±ãã¯ãåã¹ããŒã¿ã¹ã®ã¹ããŒã¿ã¹èª¬ææšæºã§ãã
ãããã£ãŠãããã¯HTTPã¡ãã»ãŒãžã®ç°¡åãªæŠèŠã§ããã PSR-7ãã©ã®ããã«ã¢ãã«åããããèŠãŠã¿ãŸãããã
ã¡ãã»ãŒãžããããŒ
ã¡ãã»ãŒãžããããŒã®ååã¯ãæåã¯å€§æåãšå°æåãåºå¥ãããŸããã æ®å¿µãªãããã»ãšãã©ã®èšèªãšã©ã€ãã©ãªã¯ããããåãã¬ãžã¹ã¿ã«å°ããŸãã äŸãšããŠãPHPã¯ã倧æåã®$ _SERVERé åã«HTTP_ãã¬ãã£ãã¯ã¹ãä»ããŠ_forã眮ãæããŠä¿åããŸãïŒããã¯ã Common Gateway Interface ïŒCGIïŒä»æ§ã«æºæ ããããã§ãïŒã
PSR-7ã¯ãããããŒã®äžã«ãªããžã§ã¯ãæåã¬ã€ã€ãŒãæäŸããããšã«ãããããããŒãžã®ã¢ã¯ã»ã¹ãç°¡çŽ åããŸã
// null, : $header = $message->getHeader('Accept'); // , : if (! $message->hasHeader('Accept')) { } // , // : $values = $message->getHeaderLines('X-Foo');
äžèšã®ããžãã¯ã¯ãã¹ãŠãããããŒã®æå®æ¹æ³ã«äŸåããŸããã acceptãACCEEPTããŸãã¯aCCePtãæå¹ãªããããŒåã§ãããåãçµæãè¿ããŸãã
PSR-7ã¯ããã¹ãŠã®ããããŒã解æãããšæ§é äœãé åãšããŠè¿ããããšæ³å®ããŠããŸãã
/* Returns the following structure: [ 'Header' => [ 'value1' 'value2' ] ] */ foreach ($message->getAllHeaders() as $header => $values) { }
æ§é ãå®çŸ©ããããšããŠãŒã¶ãŒã¯åä¿¡å 容ãæ£ç¢ºã«ææ¡ããå®è£ ã«é¢ä¿ãªãããŠãŒã¶ãŒã«ãšã£ãŠäŸ¿å©ãªæ¹æ³ã§ããããŒãåŠçã§ããŸãã
ããããããšãã°ããªã¯ãšã¹ããäœæããŠHTTPã¯ã©ã€ã¢ã³ãã«éä¿¡ãããªã©ãã¡ãã»ãŒãžã«ããããŒãè¿œå ããå Žåã¯ã©ãã§ããããã
PSR-7ã®ã¡ãã»ãŒãžã¯ã å€ãªããžã§ã¯ããšããŠã¢ãã«åãããŸã ã ã€ãŸããç¶æ ã®å€åã¯å®éã«ã¯å¥ã®æå³ã§ãã ãããã£ãŠãæ°ããããããŒãå®çŸ©ãããšãæ°ããã¡ãã»ãŒãžãªããžã§ã¯ããäœæãããŸãã
$new = $message->withHeader('Location', 'http://example.com');
å€ãæŽæ°ããã ãã®å Žåã¯ãåã«ãªãŒããŒã©ã€ãã§ããŸãã
$message = $message->withHeader('Location', 'http://example.com');
æ¢åã®ããããŒã«å¥ã®å€ãè¿œå ããå Žåã¯ã次ãå®è¡ã§ããŸãã
$message = $message->withAddedHeader('X-Foo', 'bar');
ãŸãã¯ãã¿ã€ãã«ãåé€ããããšãã§ããŸãã
$message = $message->withoutHeader('X-Foo');
ã¡ãã»ãŒãžæ¬æ
åè¿°ã®ããã«ãã¡ãã»ãŒãžæ¬æã¯éåžžãããã©ãŒãã³ã¹ãæ¹åããããã®ã¹ããªãŒã ãšããŠæ±ãããŸãã ããã¯ãHTTPã䜿çšããŠãã¡ã€ã«ã転éããå Žåã«ç¹ã«éèŠã§ãã çŸåšã®ããã»ã¹ã§äœ¿çšå¯èœãªãã¹ãŠã®ã¡ã¢ãªã䜿çšããå Žåãé€ããŸãã ç§ãèŠãHTTPã¡ãã»ãŒãžã®ã»ãšãã©ã®å®è£ ã¯ããããå¿ããããäºåŸã«æ¯ãèããå€æŽããããšããŸãïŒã¯ããZF2ããããç¯ããŸãïŒïŒã ãã®ã¢ãããŒãã®å©ç¹ã«ã€ããŠè©³ããç¥ãããå Žåã¯ãMichael Dowlingã®èšäºãèªãã§ãã ããã 圌ã¯ããã°ã§ãæšå€ã®PSR-7ã§ã®ã¹ããªãŒã ã®äœ¿çšã«ã€ããŠæžããŠããŸãã
ãã®ãããPSR-7ã®ã¡ãã»ãŒãžæ¬æã¯ã¹ããªãŒã ãšããŠã¢ãã«åãããŸã ã
ãããããããã¯è¡ã§ããŸãããå Žåã®80ïŒ ã®å Žåã«ã¯è€éãããŸãïŒãã¡ãã»ãŒãžæ¬æåŠçã®ãã®å®è£ ãæ¹å€ãã人ã ã®éã§æãé »ç¹ã«è°è«ãããŠããŸãã ã§ã¯ã次ãèŠãŠã¿ãŸãããã
$body = new Stream('php://temp'); $body->write('Here is the content for my message!');
ãã®äŸãããã³ãã®æçš¿ã§HTTPã¡ãã»ãŒãžãæäœãããã¹ãŠã®åŸç¶ã®äŸã§ã¯ãPSR-7ã®éçºãåæ ãããç§ãäœæããphly / httpã©ã€ãã©ãªã䜿çšããŸãã ãã®å ŽåãStreamã¯StreamableInterfaceãå®è£ ããŸãã
åºæ¬çã«ãã¡ãã»ãŒãžæ¬æãšå¯Ÿè©±ããããã®ãªããžã§ã¯ãæåã®èãã€ã³ã¿ãŒãã§ã€ã¹ãååŸããŸããããã«ãããæ å ±ãè¿œå ããããèªãã ãããããšãã§ããŸãã ã¡ãã»ãŒãžãå€æŽãããã§ããïŒ æ°ããã¡ãã»ãŒãžæ¬æãäœæããŸãã
$message = $message->withBody(new Stream('php://temp'));
ç§ã®æèŠã§ã¯ãã¹ããªãŒã ãšããŠã®ã¡ãã»ãŒãžæ¬æã®è¡šç€ºã¯è€éã«æããŸãããå®éã«ã¯ãå®è£ ãšäœ¿çšã¯éåžžã«ã·ã³ãã«ã§ç解ãããããã®ã§ãã
PSR-7ã§StreamableInterfaceã䜿çšããå©ç¹ã¯ãããŸããŸãªãã¶ã€ã³ãã¿ãŒã³ã®å®è£ ãç°¡çŽ åããæè»æ§ãæäŸããããšã§ãã ããšãã°ãreadïŒïŒãŸãã¯getContentsïŒïŒã¡ãœãããåŒã³åºããããšãã«ã¡ãã»ãŒãžã®ã³ã³ãã³ããè¿ããã³ãŒã«ããã¯ãé¢æ°ãå®è£ ã§ããŸãïŒç¹ã«ãDrupalã¯ãã®ãã³ãã¬ãŒãã䜿çšããŸãïŒã ãŸãã¯ãã€ãã¬ãŒã¿ãTraversableã䜿çšããŠã³ã³ãã³ããè¿ãããããŒãžãããããå®è£ ã éèŠãªã®ã¯ããã®ãããªã€ã³ã¿ãŒãã§ã€ã¹ã䜿çšãããšãã¡ãã»ãŒãžæ¬æãæäœããããã®å€ãã®ãã³ãã¬ãŒããå®è£ ããããã®å¹ åºãç¯å²ãæäŸããããšããããšã§ãã ãŸããåã«è¡ããã¡ã€ã«ã«éå®ãããã®ã§ã¯ãããŸããã
StreamableInterfaceã¯ãHTTPã¡ãã»ãŒãžã®æ¬æãæäœãããšãã«æãé »ç¹ã«äœ¿çšãããäžé£ã®ã¡ãœãããæäŸããŸãã ããã¯ã絶察ã«ãã¹ãŠãæäŸãããšããæå³ã§ã¯ãªããæœåšçã«å¿ èŠãªæäœã®å€§èŠæš¡ãªã»ããã察象ãšããŠããŸãã
å人çã«ã¯ãphpïŒ// tempã¹ããªãŒã ã䜿çšããŸããããã¯ãååã«å€§ãããªããŸã§ã¡ã¢ãªå ã«ããããã§ãïŒãã®å Žåããã£ã¹ã¯äžã®äžæãã¡ã€ã«ã«æžã蟌ãŸããŸãïŒã ãã®æ¹æ³ã¯éåžžã«å¹æçã§ãã
çã
ãããŸã§ããã¹ãŠã®ã¡ãã»ãŒãžã«å ±éã®æ©èœãæ€èšããŠããŸããã ããã§ãç¹ã«çãã詳ãã説æããŸãã
çãã«ã¯ãã¹ããŒã¿ã¹ã³ãŒããšèª¬æãã¬ãŒãºããããŸãã
$status = $response->getStatusCode(); $reason = $response->getReasonPhrase();
èŠããããã§ãã ã§ã¯ãç§ãã¡èªèº«ãçãã圢æãããã©ããªãã§ããããïŒ
説æå¥ã¯ãªãã·ã§ã³ãšèŠãªãããŸãïŒãã ããåæã«åã¹ããŒã¿ã¹ã³ãŒãã®æšæºïŒã ãã®ããã«ãã€ã³ã¿ãŒãã§ãŒã¹ã¯å¿çåºæã®ãã¥ãŒããŒã¿ãŒwithStatusïŒïŒãæäŸããŸãïŒ
$response = $response->withStatus(418, "I'm a teapot");
ç¹°ãè¿ããŸãããã¡ãã»ãŒãžã¯å€ãªããžã§ã¯ããšããŠã¢ãã«åãããŸãã å€ãå€æŽãããšãå¿çãŸãã¯èŠæ±ã«é¢é£ä»ããããæ°ããã€ã³ã¹ã¿ã³ã¹ãäœæãããŸãã ãã ããã»ãšãã©ã®å Žåãåã«çŸåšã®ã€ã³ã¹ã¿ã³ã¹ãåå²ãåœãŠããŸãã
ãåãåãã
ã¯ãšãªã«ã¯æ¬¡ãå«ãŸããŸãã
- æ¹æ³ã
- URI /ãªã¯ãšã¹ãã®ç®çã
åŸè ã¯ã¢ãã«åãå°ãé£ããã§ãã ãããã99ïŒ ã®ã±ãŒã¹ã§ã¯ããªã¯ãšã¹ãã®ã¿ãŒã²ãããšããŠæšæºURIã衚瀺ãããŸãã ããããããã¯ãä»ã®ã¿ã€ãã®èŠæ±ç®æšãæäŸããå¿ èŠããããšããäºå®ãåŠå®ãããã®ã§ã¯ãããŸããã ãããã£ãŠãã¯ãšãªã€ã³ã¿ãŒãã§ã€ã¹ã¯æ¬¡ã®ããšãè¡ããŸãã
- èŠæ±URIãã¢ãã«åããUriInterfaceã®ã€ã³ã¹ã¿ã³ã¹ãäœæããŸãã
- èŠæ±ã¿ãŒã²ããã«2ã€ã®ã¡ãœãããæäŸããŸããgetRequestTargetïŒïŒãèŠæ±ã¿ãŒã²ãããè¿ããæäŸãããªãå Žåã¯è©äŸ¡ããŸãïŒææ¡ãããURIã䜿çšããŠå ã®ãã©ãŒã ã«æ»ãããURIãæäŸãããªãããã¹ãå«ãŸããªãå Žåã«ã/ããè¿ããŸãïŒ; withRequestTargetïŒïŒã¯ããªã¯ãšã¹ãã®ç¹å®ã®ç®çã®ããã«æ°ããã€ã³ã¹ã¿ã³ã¹ãäœæããŸãã
ããã«ãããå¿ èŠã«å¿ããŠããªã¯ãšã¹ãã®ä»»æã®ç®çã§ãªã¯ãšã¹ãã«å¯ŸåŠããããšãã§ããŸãïŒããšãã°ããªã¯ãšã¹ãå ã®URIæ å ±ã䜿çšããŠHTTPã¯ã©ã€ã¢ã³ããšã®æ¥ç¶ã確ç«ããŸãïŒã
ãªã¯ãšã¹ãããã¡ãœãããšURIãååŸããŸãããïŒ
$method = $request->getMethod(); $uri = $request->getUri();
ãã®å Žåã®$ uriã¯UriInterfaceã®ã€ã³ã¹ã¿ã³ã¹ã§ãããURIã䜿çšã§ããŸãã
// URI: $scheme = $uri->getScheme(); $userInfo = $uri->getUserInfo(); $host = $uri->getHost(); $port = $uri->getPort(); $path = $uri->getPath(); $query = $uri->getQuery(); // $authority = $uri->getAuthority(); // [user-info@]host[:port]
HTTPã¡ãã»ãŒãžãšåæ§ã«ãURIã¯å€ãªããžã§ã¯ããšããŠè¡šãããURIã®äžéšãå€æŽãããšå€ãå€æŽãããã¡ãœãããå€æŽãããšæ°ããã€ã³ã¹ã¿ã³ã¹ãè¿ãããŸãã
$uri = $uri ->withScheme('http') ->withHost('example.com') ->withPath('/foo/bar') ->withQuery('?baz=bat');
URIã®å€æŽã¯æ°ããã€ã³ã¹ã¿ã³ã¹ã®äœæãæå³ãããããå€æŽããªã¯ãšã¹ãã«åæ ããå Žåã¯ãå€æŽããªã¯ãšã¹ããªããžã§ã¯ãã«å ±åããå¿ èŠããããŸãã ãŸããä»ã®ã¡ãã»ãŒãžãšåæ§ã«ãç¹å®ã®ã€ã³ã¹ã¿ã³ã¹ã®ã¡ãœãããŸãã¯URIãå€æŽããå¿ èŠãããå Žåã¯ã次ã®ã¡ãœããã䜿çšããå¿ èŠããããŸãã
$request = $request ->withMethod('POST') ->withUri($uri->withPath('/api/user'));
ãµãŒããŒãªã¯ãšã¹ã
ãµãŒããŒèŠæ±ã«ã¯ãæšæºã®HTTPèŠæ±ã¡ãã»ãŒãžãšã¯è¥å¹²ç°ãªãã¿ã¹ã¯ããããŸãã PHPãµãŒããŒAPIïŒSAPIïŒã®ãã€ãã£ãã¯ãPHPéçºè åãã®äžé£ã®éåžžã®æ©èœãæäŸããŸãã
- ã¯ãšãªæååã®åŒæ°ã®éã·ãªã¢ã«åïŒ$ _GETïŒã
- POSTã¡ãœããïŒ$ _POSTïŒã«ãã£ãŠéä¿¡ããããšã³ã³ãŒãããŒã¿ã®éã·ãªã¢ã«åã
- Cookieãã·ãªã¢ã©ã€ãŒãŒã·ã§ã³ïŒ$ _COOKIEïŒã
- ããŠã³ããŒããããã¡ã€ã«ïŒ$ _FILESïŒã®è¡šç€ºãšåŠçã
- CGI / SAPIãã©ã¡ãŒã¿ãŒã®ã«ãã»ã«åïŒ$ _SERVERïŒã
ã¯ãšãªæååããã®åŒæ°ããªã¯ãšã¹ãæ¬æããã®ããŒã¿ãCookieã¯ãªã¯ãšã¹ãã®ããŸããŸãªéšåããååŸã§ããŸããããããå®è£ ãããŠããã°äŸ¿å©ã§ãã ãããã®å€ã䜿çšããå¿ èŠãããå ŽåããããŸãã
- APIã®å ŽåãããŒã¿ã¯XMLãŸãã¯JSON圢åŒã«ããããšãã§ããPOSTã¡ãœããã ãã§ãªãéä¿¡ããããšãã§ããŸãã ã€ãŸããããŒã¿ã埩å·åããŠããããªã¯ãšã¹ãã«å床åã蟌ãå¿ èŠããããŸãã
- å€ãã®ãã¬ãŒã ã¯ãŒã¯ã¯Cookieãæå·åããããã«ãªããŸãããããã¯ãCookieã埩å·åããŠãªã¯ãšã¹ãã«åã蟌ãå¿ èŠãããããšãæå³ããŸãã
ãã®ãããPSR-7ã¯ç¹å¥ãªã€ã³ã¿ãŒãã§ã€ã¹ãServerRequestInterfaceãæäŸããŸããããã¯ãããŒã¹RequestInterfaceãæ¡åŒµããåæ§ã®ããŒã¿ãæäœããããã®æ©èœãèšè¿°ããŸãã
$query = $request->getQueryParams(); $body = $request->getBodyParams(); $cookies = $request->getCookieParams(); $files = $request->getFileParams(); $server = $request->getServerParams();
APIãèšè¿°ããŠããŠãJSON圢åŒã®ãªã¯ãšã¹ããåãå ¥ããããšæ³åããŠãã ããã ãããè¡ããšã次ã®ããã«ãªããŸãã
$accept = $request->getHeader('Accept'); if (! $accept || ! preg_match('#^application/([^+\s]+\+)?json#', $accept)) { $response->getBody()->write(json_encode([ 'status' => 405, 'detail' => 'This API can only provide JSON representations', ])); emit($response ->withStatus(405, 'Not Acceptable') ->withHeader('Content-Type', 'application/problem+json') ); exit(); } $body = (string) $request->getBody(); $request = $request ->withBodyParams(json_decode($body));
äžèšã®äŸã¯ãããã€ãã®æ©èœã瀺ããŠããŸãã æåã«ããªã¯ãšã¹ãããããããŒãæœåºãããã®ããããŒã«åºã¥ããŠåå²ããžãã¯ã瀺ããŸãã 第äºã«ããšã©ãŒãçºçããå Žåã®ãªã¯ãšã¹ããªããžã§ã¯ãã®åœ¢æã瀺ããŸãïŒemitïŒïŒé¢æ°ã¯ä»®æ³çã§ããããªã¯ãšã¹ããªããžã§ã¯ããååŸãããªã¯ãšã¹ãã®ããããŒãšæ¬æãæäŸããŸãïŒã æåŸã«ããã®äŸã§ã¯ããªã¯ãšã¹ãæ¬æãååŸããããããã·ãªã¢ã©ã€ãºãããªã¯ãšã¹ãã«å床åã蟌ãããšã瀺ããŠããŸãã
å±æ§
ãµãŒããŒèŠæ±ã®ãã1ã€ã®æ©èœã¯å±æ§ã§ãã ãããã¯ãçŸåšã®ãªã¯ãšã¹ãããååŸããå€ãæ ŒçŽããããã«èšèšãããŠããŸãã äžè¬çãªäœ¿çšäŸã¯ãã«ãŒãã£ã³ã°çµæã®ä¿åã§ãïŒURIãããŒãšå€ã®ãã¢ã«åå²ããŸãïŒã
å±æ§ã®æäœã¯ã次ã®ã¡ãœããã§æ§æãããŠããŸãã
- getAttributeïŒ$ nameã$ default = nullïŒç¹å®ã®å±æ§ãååŸããå±æ§ãèŠã€ãããªãå Žåã¯ããã©ã«ãå€ãè¿ããŸãã
- getAttributesïŒïŒã¯ããã¹ãŠã®å±æ§ãååŸããŸãã
- withAttributeïŒ$ nameã$ valueïŒã¯ããã®å±æ§ãå«ãæ°ããServerRequestInterfaceã€ã³ã¹ã¿ã³ã¹ãè¿ããŸãã
- withoutAttributeïŒïŒ$ nameïŒã¯ãæå®ãããå±æ§ãªãã§ServerRequestInterfaceã®ã€ã³ã¹ã¿ã³ã¹ãè¿ããŸãã
äŸãšããŠããªã¯ãšã¹ãã€ã³ã¹ã¿ã³ã¹ã§Aura RouterãèŠãŠã¿ãŸãããã
use Aura\Router\Generator; use Aura\Router\RouteCollection; use Aura\Router\RouteFactory; use Aura\Router\Router; $router = new Router( new RouteCollection(new RouteFactory()), new Generator() ); $path = $request->getUri()->getPath(); $route = $router->match($path, $request->getServerParams()); foreach ($route->params as $param => $value) { $request = $request->withAttribute($param, $value); }
ãã®å Žåã®ã¯ãšãªã€ã³ã¹ã¿ã³ã¹ã¯ãããŒã¿ã®æŽçãšã«ãŒãã®éä¿¡ã«äœ¿çšãããŸãã ãã®åŸãã«ãŒãã£ã³ã°çµæã䜿çšããŠãå¿çãã€ã³ã¹ã¿ã³ã¹åããŸãã
ãŠãŒã¹ã±ãŒã¹
ããã§ãPSR-7ã®ããŸããŸãªã³ã³ããŒãã³ãã®ã¯ã€ãã¯ãã¢ãŒã®åŸãç¹å®ã®ãŠãŒã¹ã±ãŒã¹ã«æ»ããŸãããã
ã客ããŸ
ç§ã«ãšã£ãŠãPSR-7æšæºã®äž»ãªäœæè ã¯ã人æ°ã®ããGuzzle HTTPã¯ã©ã€ã¢ã³ãã®äœæè ã§ããMichael Dowlingã§ãã ãããã£ãŠãPSR-7ãHTTPã¯ã©ã€ã¢ã³ãã«æ¹åãããããããšã¯æããã§ãã æ¹æ³ã説æããŸãããã
ãŸããããã¯éçºè ãã¯ãšãªå®è¡ã®ããã®ãŠããŒã¯ãªã¡ãã»ãŒãžã€ã³ã¿ãŒãã§ãŒã¹ãæã€ããšãæå³ããŸãã PSR-7æšæºã«åŸã£ãŠèŠæ±ãªããžã§ã¯ããã¯ã©ã€ã¢ã³ãã«éä¿¡ããåãæšæºã«åŸã£ãŠå¿çãªããžã§ã¯ããåä¿¡ã§ããŸãã
$response = $client->send($request);
ã¡ãã»ãŒãžãšURIã¯å€ãªããžã§ã¯ããšããŠã¢ãã«åããããããéçºè ã¯åºæ¬çãªã¯ãšãªã€ã³ã¹ã¿ã³ã¹ãšURIãäœæãããããããåå¥ã®ãªã¯ãšã¹ããšURIãäœæã§ããããšãæå³ããŸãã
$baseUri = new Uri('https://api.example.com'); $baseRequest = (new Request()) ->withUri($baseUri) ->withHeader('Authorization', $apiToken); while ($action = $queue->dequeue()) { // ! // URI . $request = $baseRequest ->withMethod($action->method) ->withUri($baseUri->withPath($action->path)); // URI! foreach ($action->headers as $header => $value) { // , // ! $request = $request->withHeader($header, $value); } $response = $client->send($request); $status = $response->getStatusCode(); if (! in_array($status, range(200, 204))) { // ! break; } // ! $data->enqueue(json_decode((string) $response->getBody())); }
PSR-7ãæäŸããã®ã¯ãã¯ã©ã€ã¢ã³ãããéä¿¡ããããªã¯ãšã¹ããåä¿¡ããå¿çãšããåãããæšæºçãªæ¹æ³ã§ãã å€ãªããžã§ã¯ããå®è£ ããããšã«ãããããªã»ãããªã¯ãšã¹ãããã³ãã¬ãŒãã®åçŽåãç®çãšããããã€ãã®èå³æ·±ããŠãŒã¹ã±ãŒã¹ã®å¯èœæ§ãéããŸãããªã¯ãšã¹ããå€æŽãããšãåžžã«æ°ããã€ã³ã¹ã¿ã³ã¹ãäœæãããåžžã«æ¡åŒµå¯èœãªæ¢ç¥ã®ç¶æ ã®ããŒã¹ã€ã³ã¹ã¿ã³ã¹ãæã€ããšãã§ããŸãã
ãªã³ã¯
ç§ã¯é·ãéããã«ãã ãããªã ãã§ã«èšäºã§ãããè¡ããŸããã èŠããã«ãäž»ãªã¢ã€ãã¢ã¯æ¬¡ã®ãšããã§ãã
function ( ServerRequestInterface $request, ResponseInterface $response, callable $next = null ) { }
ãã®é¢æ°ã¯2ã€ã®HTTPã¡ãã»ãŒãžãåãå ¥ãããããã䜿çšããŠããã€ãã®å€æãå®è¡ããŸãïŒæ¬¡ã«äœ¿çšå¯èœãªãªã³ã¯ãžã®å§ä»»ãå«ãŸããå ŽåããããŸãïŒã éåžžããããã®ãªã³ã¯ã¯å¿çãªããžã§ã¯ããè¿ããŸãã
ãã䜿çšãããå¥ã®ãªãã·ã§ã³ã¯ãã©ã ãåŒã§ãïŒãã®çšèªãã¡ãŒã«ã§éã£ãŠãããLarry Garfieldã«æè¬ããŸãïŒïŒïŒ
/* response = */ function (ServerRequestInterface $request) { /* ... */ return $response; }
ã©ã ããªã³ã¯ã§ã¯ãäºãã«æ§æããŸãã
$inner = function (ServerRequestInterface $request) { /* ... */ return $response; }; $outer = function (ServerRequestInterface $request) use ($inner) { /* ... */ $response = $inner($request); /* ... */ return $response; }; $response = $outer($request);
ãããŠæåŸã«ãRackãšWSGIã«ãã£ãŠããã¢ãŒããããã¡ãœããããããŸããåãªã³ã¯ã¯ãªããžã§ã¯ãã§ãããåºå£ã«ç§»åããŸãã
class Command { private $wrapped; public function __construct(callable $wrapped) { $this->wrapped = $wrapped; } public function __invoke( ServerRequestInterface $request, ResponseInterface $response ) { // $new = $request->withAttribute('foo', 'bar'); // , : $result = ($this->wrapped)($new, $response); // , if ($result instanceof ResponseInterface) { $response = $result; } // return $reponse->withHeader('X-Foo', 'Bar'); } }
äžéãªã³ã¯ã®äœ¿çšã¯ãèŠæ±ãšå¿çã®é¢ä¿ãå®è£ ããæšæºãã€ãŸãäºæž¬å¯èœãªåäœãåããäºæž¬å¯èœãªãã¿ãŒã³ã«åŸãããšã§ãã ããã¯ãåå©çšå¯èœãªWebã³ã³ããŒãã³ããäœæããããã®åªããæ¹æ³ã§ãã
ãã¬ãŒã ã¯ãŒã¯
ãã¬ãŒã ã¯ãŒã¯ãé·å¹Žã«ããã£ãŠæäŸããŠããããšã®1ã€ã¯ã... HTTPã¡ãã»ãŒãžã®æœè±¡åã¬ã€ã€ãŒã§ãã PSR-7ã®ç®æšã¯ããã¬ãŒã ã¯ãŒã¯ã«åãæœè±¡åã䜿çšã§ããããã«ããã¬ãŒã ã¯ãŒã¯ã«å ±éã®ã€ã³ã¿ãŒãã§ã€ã¹ã»ãããæäŸããããšã§ãã ããã«ãããéçºè ã¯ãåå©çšå¯èœãªããã¬ãŒã ã¯ãŒã¯ã«äŸåããªãã³ãŒããäœæã§ããŸããå°ãªããšãããããç§ãèŠãããã®ã§ãïŒ
Zend Framework 2ãæ€èšããŠãã ãããZend\ Stdlib \ DispatchableInterfaceã€ã³ã¿ãŒãã§ã€ã¹ãå®çŸ©ããŸããããã¯ããã¬ãŒã ã¯ãŒã¯ã§äœ¿çšããã³ã³ãããŒã©ãŒã®ããŒã¹ã§ãã
use Zend\Http\RequestInterface; use Zend\Http\ResponseInterface; interface DispatchableInterface { public function dispatch( RequestInterface $request, ResponseInterface $response ); }
ããã¯ãåè¿°ã®äžéãªã³ã¯ã«ãããŸããã å¯äžã®éãã¯ããã®ãã¬ãŒã ã¯ãŒã¯ã«åºæã®HTTPã¡ãã»ãŒãžã®å®è£ ã䜿çšããããšã§ãã 代ããã«PSR-7ããµããŒãããå Žåã¯ã©ããªããŸããïŒ
ãã¬ãŒã ã¯ãŒã¯ã§ã®HTTPã¡ãã»ãŒãžã®ã»ãšãã©ã®å®è£ ã¯ããã€ã§ãã¡ãã»ãŒãžã®ç¶æ ãå€æŽã§ããããã«èšèšãããŠããŸããç¹ã«ã¡ãã»ãŒãžã®ç¶æ ããã§ã«ç¡å¹ã§ãããšä»®å®ããå Žåãããã¯å®å šã«çå®ã§ã¯ãªãå ŽåããããŸããããããããã¯ãããããã®æ¹æ³ã®å¯äžã®æ¬ ç¹ã§ãã
PSR-7ã¡ãã»ãŒãžã¯å€ãªããžã§ã¯ãã§ããããã«ãããã¡ãã»ãŒãžã®å€æŽã«ã€ããŠã¢ããªã±ãŒã·ã§ã³ã«éç¥ããå¿ èŠããªããªããŸããããã«ãããå®è£ ãããæ確ã«ãªããã³ãŒãå ã§è¿œè·¡ãããããªããŸãïŒãããã¬ãŒã§ã®æ®µéçãªã¹ããããšéçã³ãŒãã¢ãã©ã€ã¶ãŒã®äœ¿çšã®äž¡æ¹ïŒã
äŸãšããŠãPSF-7ã«åŸã£ãŠZF2ãæŽæ°ãããå Žåãéçºè ã¯ééãã顧客ã«æž¡ãããå€æŽãMvcEventã«éç¥ããå¿ èŠã¯ãããŸããã
// $request = $request->withAttribute('foo', 'bar'); $response = $response->withHeader('X-Foo', 'bar'); $event = $this->getEvent(); $event->setRequest($request) ->setResponse($response);
äžèšã®ã³ãŒãã¯ãã¢ããªã±ãŒã·ã§ã³ã®ç¶æ ãå€æŽããŠããããšãæ確ã«ç€ºããŠããŸãã
å€ãªããžã§ã¯ãã䜿çšãããšã1ã€ã®ç¹å®ã®ãã©ã¯ãã£ã¹ããµãã¯ãšãªã®å²ãåœãŠãŸãã¯éå±€MVCïŒHMVCïŒã®å®è£ ã容æã«ãªããŸãããã®å Žåãã¢ããªã±ãŒã·ã§ã³ã«éç¥ããã«ãçŸåšã®ã¯ãšãªã«åºã¥ããŠæ°ããã¯ãšãªãäœæããã¢ããªã±ãŒã·ã§ã³ã®ç¶æ ãå€ãããªãããšã確èªã§ããŸãã
äžè¬ã«ãã»ãšãã©ã®ãã¬ãŒã ã¯ãŒã¯ã§ã¯ãPSR-7ã¡ãã»ãŒãžã䜿çšãããšãHTTPã¡ãã»ãŒãžãä»ããããŒã¿ãã«ãªæœè±¡åã«å€æãããŸããããã«ããããŠãããŒãµã«äžéãªã³ã¯ãå®è£ ã§ããŸãããã ããã¡ãã»ãŒãžã調æŽããã«ã¯ãå°ããªå€æŽãå¿ èŠã§ããéçºè ã¯ãã¢ããªã±ãŒã·ã§ã³ã®ã¹ããŒã¿ã¹ãç£èŠããã³ãŒããæŽæ°ããå¿ èŠããããŸãã
ãœãŒã¹
PSR-7æšæºãæäŸããå©ç¹ãã芧ãã ããïŒHTTPã¡ãã»ãŒãžã«å¯Ÿããçµ±äžãããå®å šãªæœè±¡åãããã«ããã®æœè±¡åã¯ãHTTPãã©ã³ã¶ã¯ã·ã§ã³ã®åéšåïŒHTTPã¯ã©ã€ã¢ã³ããä»ããŠãªã¯ãšã¹ããéä¿¡ãããããµãŒããŒãªã¯ãšã¹ãã解æããïŒã«äœ¿çšã§ããŸãã
PSR-7ä»æ§ã¯ãŸã å®å šã§ã¯ãããŸãããããããç§ãäžã§æŠèª¬ããããšã¯ãæ祚ãªãã§ã¯å€§ããªå€åãçµéšããªãã§ãããã次ã®ãªã³ã¯ã§ãä»æ§ã«ã€ããŠè©³ããç¥ãããšãã§ããŸãã
ãŸããã¢ã€ãã¢ãéçºããããœãªã¥ãŒã·ã§ã³ãããã³2幎éã«ãããïŒç¡éã®ïŒçŽäºã®çµæã«ã€ããŠèª¬æããŠããã説æçšã®ã¡ã¢ããèªãããšããå§ãããŸãã
ææ°ã®æŽæ°ã¯psr / http-messageããã±ãŒãžã§å ¬éãããcomposerã䜿çšããŠã€ã³ã¹ããŒã«ã§ããŸãããããã¯åžžã«ææ°ã®æŽæ°ããããªãã¡ãŒã§ãã
ã©ã€ãã©ãªphly / httpãäœæããææ¡ãããã€ã³ã¿ãŒãã§ã€ã¹ã®å ·äœçãªå®è£ ãæäŸããŸããcomposerãä»ããŠã€ã³ã¹ããŒã«ããããšãã§ããŸãã
æåŸã«ãPSR-7ããŒã¹ã®äžéäœãè©ŠããŠã¿ããå Žåã¯ã次ã®ãªãã·ã§ã³ããå§ãããŸãã
- phly / conduitãSenchaãã移æ€ãããConnectã©ã€ãã©ãªã§ãã³ã¢ã§phly / httpããã³psr / http-messageã䜿çšããŸãã
- StackerãStackPHPã¯ã Larry Garfieldã«ãã£ãŠæžãããåæ§ã®å®è£ ã§ãã
PSR-7ã§ã®éçºã®æªæ¥ãèŠããŸãããããŠãããã¯ãŸã£ããæ°ããäžä»£ã®PHPã¢ããªã±ãŒã·ã§ã³ãçã¿åºããšä¿¡ããŠããŸãã