본문 바로가기
개발/php

php curl 새창 읽기 get 방식, post 방식 데이터 넘기기 예제

by 향유 2021. 1. 19.

함수로 만들어 놓고 쓰면 좋을듯 싶다.

<?php

// GET 방식 함수
function get($url, $params=array()) 
{ 
    $url = $url.'?'.http_build_query($params, '', '&');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}
 
// get함수 호출
get('http://itfresh.tistory.com', array('param1'=>'value1', 'param2'=>'value2'));
 
 
// POST 방식 함수
function post($url, $fields)
{
    $post_field_string = http_build_query($fields, '', '&');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_field_string);
    curl_setopt($ch, CURLOPT_POST, true);
    $response = curl_exec($ch);
    curl_close ($ch);
    return $response;
}
 
// post함수 호출
post('http://itfresh.tistory.com', array('field1'=>'value1', 'field2'=>'value2'));

?>

댓글