PHP CURL 사용법

WEB 관련 2022. 6. 29. 19:58

<?
// 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('url', 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('url', array('field1'=>'value1', 'field2'=>'value2'));
?>

블로그 이미지

AutoLoop

컴퓨터 하드웨어 소프트웨어 정보 각종 유틸리티 해외정보 가상화폐 코인 주식 차트 마케팅 컨설팅 자료모음 및 설명

,