티스토리 뷰

[업데이트 2017.02.03 01:17]

 

PHP에서 REST API 구현을 위한 간단한 HTTP Request 정보 추출 방법을 정리하였습니다.

크게 3가지에 대해 정보에 대해 추출을 해보았습니다.

 

- Request Method

- Request URI Path

- Request URI Query Parameter

 

다음은 해당 정보 추출에 대한 소스코드입니다. (예외처리를 신경쓰지 않은 코드입니다)

/*
 * Get the data from $_SERVER variable
 * - Request Method
 * - Request URI Path
 * - Request URI Query Parameter
 */
$requestUri = $_SERVER['REQUEST_URI'];
$requestMethod = $_SERVER['REQUEST_METHOD'];
$pathInfo = $_SERVER['PATH_INFO'];

echo "\n";
echo "Request Method: ".$requestMethod."\n";
echo "Request URI Path: ".$pathInfo."\n";

// Extract request parameters
$params = explode('?', $requestUri);

echo "Request URI Query Parameter: ".end($params)."\n";

$param_list = explode('&', end($params));

echo "\n";

$idx = 0;
foreach ($param_list as $param)
{
	$key_value = explode('=', $param);
	
	echo "param #".$idx." => "."{ key:".$key_value[0].", value:".$key_value[1]." }\n";
	$idx++;
}

 

<cURL을 통한 실행 결과>

C:\>curl -X GET "http://localhost/users/list?offset=10&limit=20"

Request Method: GET
Request URI Path: /users/list
Request URI Query Parameter: offset=10&limit=20

param #0 => { key:offset, value:10 }
param #1 => { key:limit, value:20 }


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함