[PHP] Curl 함수 (GET,POST,PUT,DELETE)

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Curl 함수 20230615 수정
function integralCurl(string $url, bool $isSsl = false, string $method = "GET", array $sendData = [], array $option = [], array $header = []): array
{
      // $certificateLoc = $_SERVER['DOCUMENT_ROOT'] . "/inc/cacert.pem";
      $certificateLoc = "";
      $method = strtoupper($method);
 
      $defaultOptions = array(
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_CONNECTTIMEOUT => 10
      );
 
      $ch = curl_init();
      curl_setopt_array($ch, $defaultOptions);
 
      curl_setopt($ch, CURLOPT_POST, $method === "POST");
 
      if ($method === "POST") {
            // $sendData 샘플
            // [
            //       "a" => 1,
            //       "b" => "22"
            // ]
 
            if (count($sendData) >= 1) {
                  curl_setopt($ch, CURLOPT_POSTFIELDS, $sendData);
            }
      } elseif ($method === "GET") {
            if (count($sendData) >= 1) {
                  $paramsUrl = http_build_query($sendData);
                  $url .= "?" . $paramsUrl;
            }
      } elseif ($method === "PUT") {
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
 
            if (count($sendData) >= 1) {
                  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($sendData));
            }
      } elseif ($method === "DELETE") {
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
 
            if (count($sendData) >= 1) {
                  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($sendData));
            }
      }
 
      curl_setopt($ch, CURLOPT_URL, $url);
 
      if (count($option) >= 1) {
            // $option 샘플
            // [
            //       CURLOPT_HEADER => false,
            //       CURLOPT_USERAGENT => "test"
            // ]
 
            curl_setopt_array($ch, $option);
      }
 
      if (count($header) >= 1) {
            // $header 샘플
            // ['Authorization: Bearer '.$accessToken, 'Content-Type: application/x-www-form-urlencoded;charset=utf-8']
 
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
      }
 
      if ($isSsl === true && $certificateLoc != "") {
            curl_setopt($ch, CURLOPT_CAINFO, $certificateLoc);
      }
       
      $returnData  = curl_exec($ch);
      $returnState = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 
      if (isset($option[CURLOPT_HEADER]) && $option[CURLOPT_HEADER] === true) {
            $returnHeaderSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);  // 응답 헤더의 크기 가져오기
            $returnHeader = substr($returnData, 0, $returnHeaderSize);  // 응답 헤더 추출
            $returnData = substr($returnData, $returnHeaderSize);  // 응답 데이터 추출
      } else {
            $returnHeader = "";
      }
 
      if ($returnData === false) {
            $returnErr = "CURL ERROR: " . curl_error($ch);
      } else {
            $returnErr = "success";
      }
 
      curl_close($ch);
 
      return [
            "data" => $returnData,
            "code" => $returnState,
            "msg" => $returnErr,
            "header" => $returnHeader
      ];
}

Leave a Comment