PHP

curl을 이용하여 post, get 방식 으로 데이터 전송하기

씨엔아이소프트 2021. 4. 28. 17:44
반응형

 

curl을 이용하여 post, get 방식 으로 데이터 전송하기

<?
// 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'));
?>

 

 

 

출처 : itfresh.tistory.com/23?category=691747

반응형
댓글수0