PHP - cUrl를 이용해 PUT, POST, GET으로 json을 전달하는 방법

리눅스/PHP|2021. 10. 22. 09:00
반응형

Rest API를 구축하였다면 PHP를 사용하여 curl로 json 문자열을 주고 받을 수 있어야 합니다. 그것은 아마도 POST 가 될 수 있고, GET 또는 PUT 이 될 수 있습니다.

curl 은 가장 보편적인 방법이며 PHP 는 간단한 방법이 될 수 있습니다.


PUT 방식

<?php
$url = 'https://example.com';
$data = '{"key": "value"}';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response  = curl_exec($ch);

curl_close($ch);
?>

 

GET 방식 

<?php
$url = 'https://example.com';
$data = '{"key": "value"}';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response  = curl_exec($ch);

curl_close($ch);
?>


POST 방식

<?php
$url = 'https://example.com';
$data = '{"key": "value"}';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response  = curl_exec($ch);

curl_close($ch);
?>

 

DELETE 방식 

<?
$url = 'https://example.com';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');

$response  = curl_exec($ch);

curl_close($ch);
?>

 

다음은 인증을 위해 헤더를 추가하는 방법입니다.

<?php
$url = 'https://example.com';
$data = '{"key": "value"}';
// 인증용 사용자 및 비밀번호
$auth = 'yourname:password';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// 기본 인증 헤더 설정
curl_setopt($ch, CURLOPT_USERPWD, $auth);
$response  = curl_exec($ch);

curl_close($ch);
?>

 

위 코드를 정리해서 아래와 같이 함수를 만들어 사용할 수 있습니다.

<?php
function restAPI($method = 'POST', $url, $data = null, $auth = null)
{
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

  if($method == 'POST')
  {
    curl_setopt($ch, CURLOPT_POST, 1);
  }
  if(strlen($auth) > 0)
  {
    curl_setopt($ch, CURLOPT_USERPWD, $auth);
  }
  if(strlen($data) > 0)
  {
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
  }

  $response  = curl_exec($ch);
  curl_close($ch);

  return $response;
}

$url = 'https://example.com';
$data = '{"key": "value"}';
$auth = 'yourname:password';


// POST
$rest = restAPI('POST', $uri, $data, $auth);

// GET
$rest = restAPI('GET', $uri, $data, $auth);

// PUT
$rest = restAPI('PUT', $uri, $data, $auth);

// DELETE
$rest = restAPI('DELETE', $uri);
?>

 

[출처] https://www.habonyphp.com/2021/07/php-curl-put-post-get-json.html

반응형

댓글()