PHP 프로그래밍

본문 바로가기
사이트 내 전체검색


PHP 프로그래밍
PHP 프로그래밍

4. CURL 사용법 (GET, POST)

페이지 정보

작성자 관리자 댓글 0건 조회 960회 작성일 21-07-26 14:21

본문

4. CURL 사용법 (GET, POST)

cURL이란? 


다양한 프로토콜로 데이터 전송이 가능한 Command Line Tool이다. 



cURL 사용법



1) GET 방식


$data = array(

    'test' => 'test'

);


$url = "https://www.naver.com" . "?" , http_build_query($data, '', );


$ch = curl_init();                                 //curl 초기화

curl_setopt($ch, CURLOPT_URL, $url);               //URL 지정하기

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    //요청 결과를 문자열로 반환 

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);      //connection timeout 10초 

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);   //원격 서버의 인증서가 유효한지 검사 안함

 

$response = curl_exec($ch);

curl_close($ch);

 

return $response;

Colored by Color Scripter

cs







2) POST 방식 


$data = array(

    'test' => 'test'

);

 

$url = "https://www.naver.com";

 

$ch = curl_init();                                 //curl 초기화

curl_setopt($ch, CURLOPT_URL, $url);               //URL 지정하기

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    //요청 결과를 문자열로 반환 

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);      //connection timeout 10초 

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);   //원격 서버의 인증서가 유효한지 검사 안함

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);       //POST data

curl_setopt($ch, CURLOPT_POST, true);              //true시 post 전송 

 

$response = curl_exec($ch);

curl_close($ch);

 

return $response;

 

Colored by Color Scripter

cs





3) 결과 값 및 오류 확인



$response = curl_exec ($ch);

 

var_dump($response);        //결과 값 출력

print_r(curl_getinfo($ch)); //모든 정보 출력

echo curl_errno($ch);       //에러 정보 출력

echo curl_error($ch);       //에러 정보 출력

cs





참고사이트


PHP CURL 사용법 (GET, POST) (tistory.com) 

댓글목록

등록된 댓글이 없습니다.


개인정보취급방침 서비스이용약관 모바일 버전으로 보기 상단으로

TEL. 063-469-4551 FAX. 063-469-4560 전북 군산시 대학로 558
군산대학교 컴퓨터정보공학과

Copyright © www.leelab.co.kr. All rights reserved.