엄지월드

(PHP) POST 요청을 보내기 위해 Rest API를 활용하기 본문

PHP

(PHP) POST 요청을 보내기 위해 Rest API를 활용하기

킨글 2020. 4. 26. 16:53
반응형

PHP 코드를 활용해서 특정 API 주소에 요청을 보낼 때 사용 가능하다.

<?php

$url = 'https://apiLink/apiPath';
$data = array('Key1' => 'value1', 'key2' => 'value2');

// header, method, body를 설정한다.
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data)
    )
);

$context  = stream_context_create($options); // 데이터 가공
$result = file_get_contents($url, false, $context); // 전송 ~ 결과값 반환
$data = json_decode($result, true); // 반환된 결과를 json으로 변환한다.

// print_r($data); // 결과 값을 출력한다.

// json 출력 방식 1
foreach($data as $row){
	print $row['index'];
}

// json 출력 방식 2
foreach($data['column'] as $row){
	print $row['companySubjectName'];
}

?>

 

 

도움이 되셨다면 광고 한번씩 클릭 부탁드립니다 😁

Comments