{"id":533,"date":"2022-12-25T17:47:25","date_gmt":"2022-12-25T08:47:25","guid":{"rendered":"https:\/\/kamita.net\/program\/?p=533"},"modified":"2022-12-25T17:47:25","modified_gmt":"2022-12-25T08:47:25","slug":"php-curl%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%9fapi%e3%82%b5%e3%83%b3%e3%83%97%e3%83%ab%e3%82%b9%e3%82%af%e3%83%aa%e3%83%97%e3%83%88","status":"publish","type":"post","link":"https:\/\/kamita.net\/program\/?p=533","title":{"rendered":"PHP curl\u3092\u4f7f\u3063\u305fapi\u30b5\u30f3\u30d7\u30eb\u30b9\u30af\u30ea\u30d7\u30c8"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>&lt;?php\n\n\/\/api\u306eURL\n$url=\"https:\/\/example.com\/api\";\n\/\/ \u30ea\u30af\u30a8\u30b9\u30c8\u306e\u30bf\u30a4\u30d7\u3092\u6307\u5b9a\u3059\u308b (POST\u304bGET\u304b)\r\n$requestType = \"POST\"; \/\/ \u307e\u305f\u306f \"GET\"\n\/\/ \u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u30bf\u30a4\u30d7\u3092\u6307\u5b9a\u3059\u308b (json\u304bhtml\u304b)\r\n$responseType = \"json\"; \/\/ \u307e\u305f\u306f \"html\"\n\n\/\/ cURL\u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u521d\u671f\u5316\u3059\u308b\n$ch = curl_init();\n\n\/\/ cURL\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u8a2d\u5b9a\u3059\u308b\ncurl_setopt($ch, CURLOPT_URL, $url);\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\n\n\nif ($requestType === \"POST\") {\n    \/\/ POST\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u884c\u3046\u5834\u5408\n    curl_setopt($ch, CURLOPT_POST, true);\n\n    \/\/ POST\u3059\u308b\u30c7\u30fc\u30bf\u3092\u6307\u5b9a\u3059\u308b\n    $data = array(\n        \"param1\" => \"value1\",\n        \"param2\" => \"value2\"\n    );\n    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));\n} else {\n    \/\/ GET\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u884c\u3046\u5834\u5408\n    curl_setopt($ch, CURLOPT_HTTPGET, true);\n}\n\n\nif ($responseType === \"json\") {\n    \/\/ json\u3092\u30ec\u30b9\u30dd\u30f3\u30b9\u3068\u3057\u3066\u8fd4\u3059\u5834\u5408\n    curl_setopt($ch, CURLOPT_HTTPHEADER, array(\n        \"Content-Type: application\/json\",\n        \"Accept: application\/json\"\n    ));\n} else {\n    \/\/ html\u3092\u30ec\u30b9\u30dd\u30f3\u30b9\u3068\u3057\u3066\u8fd4\u3059\u5834\u5408\n    curl_setopt($ch, CURLOPT_HTTPHEADER, array(\n        \"Content-Type: text\/html\",\n        \"Accept: text\/html\"\n    ));\n}\n\n\/\/ cURL\u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u5b9f\u884c\u3059\u308b\n$response = curl_exec($ch);\n\n\/\/ cURL\u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u7d42\u4e86\u3059\u308b\ncurl_close($ch);\n\n\/\/ \u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u51fa\u529b\u3059\u308b\necho $response;\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/kamita.net\/program\/index.php?rest_route=\/wp\/v2\/posts\/533"}],"collection":[{"href":"https:\/\/kamita.net\/program\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kamita.net\/program\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kamita.net\/program\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kamita.net\/program\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=533"}],"version-history":[{"count":1,"href":"https:\/\/kamita.net\/program\/index.php?rest_route=\/wp\/v2\/posts\/533\/revisions"}],"predecessor-version":[{"id":534,"href":"https:\/\/kamita.net\/program\/index.php?rest_route=\/wp\/v2\/posts\/533\/revisions\/534"}],"wp:attachment":[{"href":"https:\/\/kamita.net\/program\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kamita.net\/program\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kamita.net\/program\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}