외부 API를 호출 할 때 최근에는 POST방식으로 JSON 데이터를 사용하는 경우가 늘고 있다.
PHP에서 외부 API에 POST로 JSON데이터를 보낼 때 cURL을 사용하는 코드는 아래와 같다.
$json_data = json_encode($json);$ch = curl_init($url);curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: '.strlen($json_data)));curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); // 이건 아래 옵션 때문에 필요 없긴 하다.curl_setopt($ch, CURLOPT_POST, 1);$output = curl_exec($ch);
조건이라면 cURL 라이브러리가 설치 되어 있어야 한다는 것...


