본문 바로가기

IT노트(구)/PHP

(php) http request 예제(curl 이용)

php에서는 curl을 이용해서 http request를 간단하게 구현할 수 있다!

다음은 curl로 post 요청을 날리는 샘플 소스이다.

꼭 필요한 부분만 추린 소스라서

curl 이해에 큰 도움이 될 것이다!


$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"http://www.test.com/test.php"); // 호출할 주소
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$post = array('username' => 'testuser', 'password' => '1234567'); // post로 요청할 데이터
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));

$server_output = curl_exec($ch); // http request 수행
curl_close($ch);

echo $server_output; // response 출력