{"id":539,"date":"2022-12-25T17:59:55","date_gmt":"2022-12-25T08:59:55","guid":{"rendered":"https:\/\/kamita.net\/program\/?p=539"},"modified":"2022-12-25T18:04:35","modified_gmt":"2022-12-25T09:04:35","slug":"php-api-http-functions%e3%82%92%e4%bd%bf%e7%94%a8%e3%81%99%e3%82%8b%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/kamita.net\/program\/?p=539","title":{"rendered":"php api HTTP functions\u3092\u4f7f\u7528\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>&lt;?php\r\n\r\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\"\r\nif ($requestType === \"POST\") {\r\n    \/\/ POST\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u884c\u3046\u5834\u5408\r\n    $options = array(\r\n        'http' => array(\r\n            'header'  => \"Content-type: application\/x-www-form-urlencoded\\r\\n\",\r\n            'method'  => 'POST',\r\n            'content' => http_build_query(array(\r\n                'param1' => 'value1',\r\n                'param2' => 'value2'\r\n            )),\r\n        ),\r\n    );\r\n} else {\r\n    \/\/ GET\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u884c\u3046\u5834\u5408\r\n    $options = array(\r\n        'http' => array(\r\n            'method'  => 'GET',\r\n        ),\r\n    );\r\n}\r\n\r\n\/\/ \u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3092\u751f\u6210\u3059\u308b\r\n$context  = stream_context_create($options);\r\n\r\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\"\r\nif ($responseType === \"json\") {\r\n    \/\/ json\u3092\u30ec\u30b9\u30dd\u30f3\u30b9\u3068\u3057\u3066\u8fd4\u3059\u5834\u5408\r\n    $options&#91;'http']&#91;'header'] .= \"Accept: application\/json\\r\\n\";\r\n} else {\r\n    \/\/ html\u3092\u30ec\u30b9\u30dd\u30f3\u30b9\u3068\u3057\u3066\u8fd4\u3059\u5834\u5408\r\n    $options&#91;'http']&#91;'header'] .= \"Accept: text\/html\\r\\n\";\r\n}\r\n\r\n\/\/ API\u3092\u547c\u3073\u51fa\u3059\r\n$response = file_get_contents('https:\/\/example.com\/api', false, $context);\r\n\r\n\/\/ \u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u51fa\u529b\u3059\u308b\r\necho $response;\r\n<\/code><\/pre>\n\n\n\n<h2>\u305f\u3093\u306bfile_get_contents\u3067api\u3092\u547c\u3073\u51fa\u3059\u3082\u306e\u3068\u306e\u9055\u3044<\/h2>\n\n\n\n<p>HTTP functions\u3092\u4f7f\u7528\u3059\u308b\u65b9\u6cd5\u3068\u3001<code>file_get_contents<\/code>\u95a2\u6570\u3092\u4f7f\u7528\u3059\u308b\u65b9\u6cd5\u306f\u3001\u305d\u306e\u4f7f\u3044\u65b9\u306b\u304a\u3044\u3066\u7570\u306a\u308b\u70b9\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>HTTP functions\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u51e6\u7406\u3092\u8a18\u8ff0\u3059\u308b\u3053\u3068\u3067\u3001API\u3092\u547c\u3073\u51fa\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Copy code<code>$options = array(\n    'http' =&gt; array(\n        'method'  =&gt; 'GET',\n    ),\n);\n$context  = stream_context_create($options);\n$response = file_get_contents('https:\/\/example.com\/api', false, $context);\n<\/code><\/pre>\n\n\n\n<p>\u4e00\u65b9\u3001<code>file_get_contents<\/code>\u95a2\u6570\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3059\u308b\u3053\u3068\u3067\u3001API\u3092\u547c\u3073\u51fa\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>$response = file_get_contents('https:\/\/example.com\/api');\n<\/code><\/pre>\n\n\n\n<p>HTTP functions\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u306f\u3001<code>stream_context_create<\/code>\u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3092\u751f\u6210\u3057\u3001\u305d\u306e\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3092\u6307\u5b9a\u3057\u3066<code>file_get_contents<\/code>\u95a2\u6570\u3092\u547c\u3073\u51fa\u3059\u3053\u3068\u3067\u3001API\u3092\u547c\u3073\u51fa\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3092\u751f\u6210\u3059\u308b\u969b\u306b\u306f\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u30bf\u30a4\u30d7\u3092\u6307\u5b9a\u3059\u308b\u306b\u306f\u3001<code>method<\/code>\u30d7\u30ed\u30d1\u30c6\u30a3\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u307e\u305f\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u30dc\u30c7\u30a3\u3084\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u306b\u542b\u3081\u308b\u3079\u304dHTTP\u30d8\u30c3\u30c0\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4e00\u65b9\u3001<code>file_get_contents<\/code>\u95a2\u6570\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u306f\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u305b\u3093\u3002\u305d\u306e\u305f\u3081\u3001HTTP functions\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u306b\u6bd4\u3079\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\u304c\u3067\u304d\u308b\u7bc4\u56f2\u304c\u72ed\u304f\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u305d\u306e\u305f\u3081\u3001HTTP functions\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u306f\u3001\u3088\u308a\u8a73\u7d30\u306a\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\u304c\u3067\u304d\u308b\u306e\u304c\u30e1\u30ea\u30c3\u30c8\u3067\u3059\u3002\u4e00\u65b9\u3001<code>file_get_contents<\/code>\u95a2\u6570\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u306f\u3001\u3088\u308a\u30b7\u30f3\u30d7\u30eb\u306a\u51e6\u7406\u3067API\u3092\u547c\u3073\u51fa\u3059\u3053\u3068\u304c\u3067\u304d\u308b\u306e\u304c\u30e1\u30ea\u30c3\u30c8\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u305f\u3093\u306bfile_get_contents\u3067api\u3092\u547c\u3073\u51fa\u3059\u3082\u306e\u3068\u306e\u9055\u3044 HTTP &hellip; <a href=\"https:\/\/kamita.net\/program\/?p=539\">Continued<\/a><\/p>\n","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\/539"}],"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=539"}],"version-history":[{"count":3,"href":"https:\/\/kamita.net\/program\/index.php?rest_route=\/wp\/v2\/posts\/539\/revisions"}],"predecessor-version":[{"id":542,"href":"https:\/\/kamita.net\/program\/index.php?rest_route=\/wp\/v2\/posts\/539\/revisions\/542"}],"wp:attachment":[{"href":"https:\/\/kamita.net\/program\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=539"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kamita.net\/program\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=539"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kamita.net\/program\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}