라이브러리
[PHP] SwooleHttpRequest::rawcontent - 원시 HTTP POST 본문을 가져옵니다.
SwooleHttpRequest::rawcontent
Swoole는 PHP에서 사용할 수 있는 고성능의 네트워크 프레임워크입니다. SwooleHttpRequest::rawcontent은 Swoole의 HTTP 요청 객체에서 사용할 수 있는 메서드로, HTTP 요청의 바디를 가져올 수 있습니다.
사용 방법
SwooleHttpRequest::rawcontent은 HTTP 요청 객체의 메서드이므로, Swoole의 HTTP 서버를 구현할 때 사용할 수 있습니다. 예를 들어, Swoole의 HTTP 서버를 구현한 후, HTTP 요청을 처리하는 함수에서 SwooleHttpRequest::rawcontent을 사용할 수 있습니다.
예제
다음은 Swoole의 HTTP 서버를 구현한 예제입니다. 이 예제에서는 HTTP 요청의 바디를 가져와 콘솔에 출력합니다.
#hostingforum.kr
php
<?php
use SwooleHttpRequest;
use SwooleHttpResponse;
function handleRequest(Request $request, Response $response)
{
// HTTP 요청의 바디를 가져와 콘솔에 출력
$rawContent = $request->rawContent();
echo "HTTP 요청의 바디: $rawContent
";
// HTTP 요청의 바디를 HTTP 응답의 바디로 설정
$response->write($rawContent);
$response->status(200);
$response->end();
}
// Swoole의 HTTP 서버를 구현
$http = new SwooleHttpServer("0.0.0.0", 9501);
$http->set([
'worker_num' => 4,
]);
$http->on('request', function (Request $request, Response $response) {
handleRequest($request, $response);
});
$http->start();
이 예제에서는 Swoole의 HTTP 서버를 구현한 후, HTTP 요청을 처리하는 함수인 `handleRequest`에서 SwooleHttpRequest::rawcontent을 사용합니다. `handleRequest` 함수에서는 HTTP 요청의 바디를 가져와 콘솔에 출력한 후, HTTP 요청의 바디를 HTTP 응답의 바디로 설정합니다.
주의
SwooleHttpRequest::rawcontent은 HTTP 요청의 바디를 가져올 때, HTTP 요청의 바디가 존재하는지 여부를 확인해야 합니다. 만약 HTTP 요청의 바디가 존재하지 않으면, `false`를 반환합니다.
#hostingforum.kr
php
$rawContent = $request->rawContent();
if ($rawContent === false) {
echo "HTTP 요청의 바디가 존재하지 않습니다.
";
} else {
echo "HTTP 요청의 바디: $rawContent
";
}
이 예제에서는 HTTP 요청의 바디가 존재하지 않으면, 콘솔에 "HTTP 요청의 바디가 존재하지 않습니다."를 출력합니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.