<?php
// GET 방식 함수
function get($url, $data=[], $header=[])
{
$url = $url.'?'.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); //인증서 유효 검사 안함
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //헤더 설정(배열 혹은 JSON)
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
// POST 방식 함수
function post($url, $data, $header=[])
{
$post_field_string = 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); //인증서 유효 검사 안함
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_field_string); //POST data
curl_setopt($ch, CURLOPT_POST, true); //true시 post 전송
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //헤더 설정(배열 혹은 JSON)
$response = curl_exec($ch);
curl_close ($ch);
return $response;
}
// 결과 값 및 오류 확인
var_dump($response); //결과 값 출력
print_r(curl_getinfo($ch)); //모든 정보 출력
echo curl_errno($ch); //에러 정보 출력
echo curl_error($ch); //에러 정보 출력
CURLOPT_RETURNTRANSFER를 true로 세팅하지 않으면 코드가 변수에 담기지 않고 화면에 뿌려진다.
사용 가능한 데이터 타입은 배열, 객체 타입이다. 가능하면 JSON을 사용하자
참조
PHP cURL POST 전송 사용법 예제 :: KiwiSoft (tistory.com)
curl을 이용하여 post, get 방식 으로 데이터 전송하기
curl을 이용하여 post, get 방식 으로 데이터 전송하기 GET과 POST에 대해 간단하게 논하겠습니다. GET은 눈에 보이는것, POST는 눈에 보이지 않는것이라 생각하면 됩니다. 즉 GET은 주소창에 http://itfresh.t
itfresh.tistory.com
curl을 이용하여 post, get 방식 으로 데이터 전송하기 (tistory.com)
PHP cURL POST 전송 사용법 예제
PHP cURL POST 전송을 위한 함수를 생성합니다. function post($url, $fields) { $post_field_string = http_build_query($fields, '', '&'); $ch = curl_init(); // curl 초기화 ..
kiwinote.tistory.com
PHP CURL 사용법 (GET, POST) (tistory.com)
PHP CURL 사용법 (GET, POST)
cURL이란? 다양한 프로토콜로 데이터 전송이 가능한 Command Line Tool이다. cURL 사용법 1) GET 방식 1 2 3 4 5 6 7 8 9 10 11 12 $data = array( 'test' => 'test' ); $url = "https://www.naver.com" . "?" ,..
qjadud22.tistory.com
[php] PHP cURL 커스텀 헤더 - 리뷰나라 (daplus.net)
[php] PHP cURL 커스텀 헤더 - 리뷰나라
PHP의 cURL HTTP 요청에 사용자 정의 헤더를 추가 할 수 있는지 궁금합니다. iTunes가 아트 워크를 가져 오는 방법을 모방하려고 시도하고 다음과 같은 비표준 헤더를 사용합니다. X-Apple-Tz: 0 X-Apple-Stor
daplus.net
'개발관련 > php' 카테고리의 다른 글
camel <-> snake (0) | 2022.02.03 |
---|---|
파일 업로드 템플릿 (0) | 2022.01.27 |