취업/CodeIgniter

[php] curl 유형 QS방식으로 변환하기.

카슈밀 2023. 2. 1. 15:10
반응형

2021.12.30 - [취업/PHP] - [php] curl 통신 유형 post,get,put, delete

 

[php] curl 통신 유형 post,get,put, delete

차트 유형이 여러게 있는데 이렇게도 쓰더라. function CallAPI($method, $api, $data) { $url = "http://localhost:82/slimdemo/RESTAPI/" . $api; $curl = curl_init($url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); switch ($method) { case

kasumil.tistory.com

 

기존 코드들이 POST 방식의 Curl이었는데, 해당 방법은 qs 방식

Delete QueryString방식

function cDelQS($param, $url, $token)
{
    $query = http_build_query($param);
    $domain = $url . "?" . $query;

    $header = array(
        'Content-Type: application/x-www-form-urlencoded',
        'Accept: application/json',
        'Authorization: Bearer '. $token
    );
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_URL, $domain);  
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}

 

PUT QueryString 방식

function cPutQS ($param, $url, $token)
{
    $query = http_build_query($param);
    $domain = $url . "?" . $query;

    $header = array(
        'Content-Type: application/x-www-form-urlencoded',
        'Accept: application/json',
        'Authorization: Bearer '.$token
    );
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_URL, $domain);  
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}

이상 끝.

728x90