PHP Curl 함수 사용법

  1. 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;
    
    }
    
  2. Get함수 호출

    $get_data = array(
        "field1" => "value1",
        "field2" => "value2",
    );
    
    // get함수 호출
    get('http://wooyg.com', $get_data);
    
  3. Post함수

    // POST 방식 함수
    function curlPost($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;
    
    }
    
  4. Post함수 호출

    $post_data = array(
        "field1" => "value1",
        "field2" => "value2",
    );
    
    // post함수 호출
    post('http://wooyg.com', $post_data);