{"id":232,"date":"2021-03-24T15:12:31","date_gmt":"2021-03-24T06:12:31","guid":{"rendered":"https:\/\/blog.mydepot.kr\/?p=232"},"modified":"2023-06-15T11:48:50","modified_gmt":"2023-06-15T02:48:50","slug":"php-curl-%ed%95%a8%ec%88%98-getpostputdelete","status":"publish","type":"post","link":"https:\/\/blog.mydepot.kr\/?p=232","title":{"rendered":"[PHP] Curl \ud568\uc218 (GET,POST,PUT,DELETE)"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n\/\/ Curl \ud568\uc218 20230615 \uc218\uc815\nfunction integralCurl(string $url, bool $isSsl = false, string $method = &quot;GET&quot;, array $sendData = &#x5B;], array $option = &#x5B;], array $header = &#x5B;]): array\n{\n      \/\/ $certificateLoc = $_SERVER&#x5B;&#039;DOCUMENT_ROOT&#039;] . &quot;\/inc\/cacert.pem&quot;;\n      $certificateLoc = &quot;&quot;;\n      $method = strtoupper($method);\n\n      $defaultOptions = array(\n            CURLOPT_RETURNTRANSFER =&gt; true,\n            CURLOPT_CONNECTTIMEOUT =&gt; 10\n      );\n\n      $ch = curl_init();\n      curl_setopt_array($ch, $defaultOptions);\n\n      curl_setopt($ch, CURLOPT_POST, $method === &quot;POST&quot;);\n\n      if ($method === &quot;POST&quot;) {\n            \/\/ $sendData \uc0d8\ud50c\n            \/\/ &#x5B;\n            \/\/       &quot;a&quot; =&gt; 1,\n            \/\/       &quot;b&quot; =&gt; &quot;22&quot;\n            \/\/ ]\n\n            if (count($sendData) &gt;= 1) {\n                  curl_setopt($ch, CURLOPT_POSTFIELDS, $sendData);\n            }\n      } elseif ($method === &quot;GET&quot;) {\n            if (count($sendData) &gt;= 1) {\n                  $paramsUrl = http_build_query($sendData);\n                  $url .= &quot;?&quot; . $paramsUrl;\n            }\n      } elseif ($method === &quot;PUT&quot;) {\n            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, &quot;PUT&quot;);\n\n            if (count($sendData) &gt;= 1) {\n                  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($sendData));\n            }\n      } elseif ($method === &quot;DELETE&quot;) {\n            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, &quot;DELETE&quot;);\n\n            if (count($sendData) &gt;= 1) {\n                  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($sendData));\n            }\n      }\n\n      curl_setopt($ch, CURLOPT_URL, $url);\n\n      if (count($option) &gt;= 1) {\n            \/\/ $option \uc0d8\ud50c\n            \/\/ &#x5B;\n            \/\/       CURLOPT_HEADER =&gt; false,\n            \/\/       CURLOPT_USERAGENT =&gt; &quot;test&quot;\n            \/\/ ]\n\n            curl_setopt_array($ch, $option);\n      }\n\n      if (count($header) &gt;= 1) {\n            \/\/ $header \uc0d8\ud50c\n            \/\/ &#x5B;&#039;Authorization: Bearer &#039;.$accessToken, &#039;Content-Type: application\/x-www-form-urlencoded;charset=utf-8&#039;]\n\n            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);\n      }\n\n      if ($isSsl === true &amp;&amp; $certificateLoc != &quot;&quot;) {\n            curl_setopt($ch, CURLOPT_CAINFO, $certificateLoc);\n      }\n      \n      $returnData  = curl_exec($ch);\n      $returnState = curl_getinfo($ch, CURLINFO_HTTP_CODE);\n\n      if (isset($option&#x5B;CURLOPT_HEADER]) &amp;&amp; $option&#x5B;CURLOPT_HEADER] === true) {\n            $returnHeaderSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);  \/\/ \uc751\ub2f5 \ud5e4\ub354\uc758 \ud06c\uae30 \uac00\uc838\uc624\uae30\n            $returnHeader = substr($returnData, 0, $returnHeaderSize);  \/\/ \uc751\ub2f5 \ud5e4\ub354 \ucd94\ucd9c\n            $returnData = substr($returnData, $returnHeaderSize);  \/\/ \uc751\ub2f5 \ub370\uc774\ud130 \ucd94\ucd9c\n      } else {\n            $returnHeader = &quot;&quot;;\n      }\n\n      if ($returnData === false) {\n            $returnErr = &quot;CURL ERROR: &quot; . curl_error($ch);\n      } else {\n            $returnErr = &quot;success&quot;;\n      }\n\n      curl_close($ch);\n\n      return &#x5B;\n            &quot;data&quot; =&gt; $returnData,\n            &quot;code&quot; =&gt; $returnState,\n            &quot;msg&quot; =&gt; $returnErr,\n            &quot;header&quot; =&gt; $returnHeader\n      ];\n}\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"no","_lmt_disable":"","footnotes":""},"categories":[8],"tags":[79,82],"class_list":["post-232","post","type-post","status-publish","format-standard","hentry","category-php","tag-curl","tag-82"],"modified_by":"\ucc38\ube5b\ubc14\ub2e4","_links":{"self":[{"href":"https:\/\/blog.mydepot.kr\/index.php?rest_route=\/wp\/v2\/posts\/232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.mydepot.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mydepot.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mydepot.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mydepot.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=232"}],"version-history":[{"count":5,"href":"https:\/\/blog.mydepot.kr\/index.php?rest_route=\/wp\/v2\/posts\/232\/revisions"}],"predecessor-version":[{"id":727,"href":"https:\/\/blog.mydepot.kr\/index.php?rest_route=\/wp\/v2\/posts\/232\/revisions\/727"}],"wp:attachment":[{"href":"https:\/\/blog.mydepot.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mydepot.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mydepot.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}