SERVER/LARAVEL & PHP

PHP CURL GET방식 POST방식 정리

나나나나나나나ㅏ나난ㄴ나ㅏ나나 2021. 3. 9. 14:24
728x90

curl이란?

cURL은 다양한 통신 프로토콜을 이용하여 데이터를 전송하기 위한 라이브러리와 명령 줄 도구를 제공하는 컴퓨터 소프트웨어 프로젝트이다.

 

정의는 위와 같고 php 내부에서 외부에 통신할 일 있을경우 사용하면 통신을 할 수있는데 GET방식과 POST방식으로 나뉘어져있다!

 

GET방식

$headers = array();

$headers[] = 'Authorization:'.$accessToken;
$opts = array(
  CURLOPT_URL => "",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HTTPHEADER => $headers,
);

curl_setopt_array($ch, $opts);
$response = curl_exec($ch);
curl_close($ch);

$response = json_decode($response, true);

 

여기서 header를 array() 이렇게 선언했더니 header가 같이 전송되지않았다ㅠㅠ

 

POST 방식

$data = array(

);

$ch = curl_init(); //curl 사용 전 초기화 필수(curl handle)
$opts = array(
CURLOPT_URL => "",
CURLOPT_POST => true,	//post 방식
CURLOPT_POSTFIELDS => $data,	// post data 전송
CURLOPT_RETURNTRANSFER => true	// text로받을건지 아닌지 체크
);

curl_setopt_array($ch, $opts);
$response = curl_exec($ch);
$response = json_decode($response, true);

curl_close($ch);

return $response;

 

정리해보니 간단한거같다! 

다음엔 헤메지않기를 🙏

728x90