{"id":535,"date":"2022-12-25T17:52:01","date_gmt":"2022-12-25T08:52:01","guid":{"rendered":"https:\/\/kamita.net\/program\/?p=535"},"modified":"2022-12-25T17:56:39","modified_gmt":"2022-12-25T08:56:39","slug":"php-guzzle%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6api%e6%8e%a5%e7%b6%9a%e3%82%b5%e3%83%b3%e3%83%97%e3%83%ab","status":"publish","type":"post","link":"https:\/\/kamita.net\/program\/?p=535","title":{"rendered":"php Guzzle\u3092\u4f7f\u3063\u3066api\u63a5\u7d9a\u30b5\u30f3\u30d7\u30eb"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>&lt;?php\r\n\r\n\/\/ Guzzle\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3092\u521d\u671f\u5316\u3059\u308b\r\n$client = new GuzzleHttp\\Client();\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    $response = $client->request('POST', 'https:\/\/example.com\/api', &#91;\r\n        'form_params' => &#91;\r\n            'param1' => 'value1',\r\n            'param2' => 'value2'\r\n        ]\r\n    ]);\r\n} else {\r\n    \/\/ GET\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u884c\u3046\u5834\u5408\r\n    $response = $client->request('GET', 'https:\/\/example.com\/api');\r\n}\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    $response = $response->getBody()->getContents();\r\n} else {\r\n    \/\/ html\u3092\u30ec\u30b9\u30dd\u30f3\u30b9\u3068\u3057\u3066\u8fd4\u3059\u5834\u5408\r\n    $response = $response->getBody();\r\n}\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<p>\u7c21\u5358\u3067\u3059\u306d\u3001curl\u3088\u308a\u3002<br>\u8907\u6570\u540c\u6642\u306b\u3082\u3067\u304d\u308b\u3089\u3057\u3044\u3002<\/p>\n\n\n\n<h2>Guzzle\u3092\u4f7f\u7528\u3059\u308b\u6e96\u5099<\/h2>\n\n\n\n<p>Guzzle\u3092\u4f7f\u7528\u3059\u308b\u306b\u306f\u3001Composer\u3092\u4f7f\u7528\u3057\u3066Guzzle\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002Composer\u306fPHP\u7528\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u30de\u30cd\u30fc\u30b8\u30e3\u30fc\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u306e\u624b\u9806\u306b\u5f93\u3063\u3066\u3001Guzzle\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u4f7f\u7528\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<ol>\n<li>\u4e8b\u524d\u306b\u3001Composer\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u304a\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002Composer\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u65b9\u6cd5\u306b\u3064\u3044\u3066\u306f\u3001\u4ee5\u4e0b\u306e\u30ea\u30f3\u30af\u5148\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002 <a href=\"https:\/\/getcomposer.org\/download\/\">https:\/\/getcomposer.org\/download\/<\/a><\/li>\n\n\n\n<li>\u6b21\u306b\u3001\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u30eb\u30fc\u30c8\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3067\u3001\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066Guzzle\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>composer require guzzlehttp\/guzzle\n<\/code><\/pre>\n\n\n\n<ol start=\"3\">\n<li>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u304c\u5b8c\u4e86\u3057\u305f\u3089\u3001Guzzle\u3092\u4f7f\u7528\u3059\u308bPHP\u30b9\u30af\u30ea\u30d7\u30c8\u306e\u4e2d\u3067\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b<code>GuzzleHttp\\Client<\/code>\u30af\u30e9\u30b9\u3092\u8aad\u307f\u8fbc\u3093\u3067\u304f\u3060\u3055\u3044\u3002<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&lt;?php\n\nrequire 'vendor\/autoload.php';\n\n\/\/ Guzzle\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3092\u521d\u671f\u5316\u3059\u308b\n$client = new GuzzleHttp\\Client();\n\n\/\/ API\u3092\u547c\u3073\u51fa\u3059\u51e6\u7406\u3092\u8a18\u8ff0\u3059\u308b<\/code><\/pre>\n\n\n\n<h2>Guzzle\u3063\u3066\u3069\u3093\u306a\u3082\u306e<\/h2>\n\n\n\n<p>Guzzle\u306f\u3001PHP\u7528\u306eHTTP\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002API\u3084Web\u30b5\u30a4\u30c8\u3078\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u884c\u3044\u3001\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u53d7\u3051\u53d6\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>Guzzle\u306f\u3001API\u3092\u547c\u3073\u51fa\u3059\u969b\u306b\u4fbf\u5229\u306a\u6a5f\u80fd\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u6a5f\u80fd\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u542b\u3080GET\u3084POST\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u308b\u3002<br>\u30d8\u30c3\u30c0\u3092\u542b\u3080\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u308b\u3002<br>\u30d5\u30a9\u30fc\u30e0\u30c7\u30fc\u30bf\u3084JSON\u30c7\u30fc\u30bf\u3092\u30ea\u30af\u30a8\u30b9\u30c8\u30dc\u30c7\u30a3\u3068\u3057\u3066\u9001\u4fe1\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<br>\u8907\u6570\u306eAPI\u3092\u4e26\u884c\u3057\u3066\u547c\u3073\u51fa\u3059\u3053\u3068\u304c\u3067\u304d\u308b\u3002<br>\u30a8\u30e9\u30fc\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0\u3084\u30bf\u30a4\u30e0\u30a2\u30a6\u30c8\u5236\u5fa1\u304c\u3067\u304d\u308b\u3002<br>Guzzle\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001API\u3092\u547c\u3073\u51fa\u3059\u969b\u306bcURL\u3092\u4f7f\u7528\u3059\u308b\u3088\u308a\u3082\u7c21\u5358\u306b\u306a\u308a\u307e\u3059\u3002\u307e\u305f\u3001Guzzle\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u8907\u6570\u306eAPI\u3092\u4e26\u884c\u3057\u3066\u547c\u3073\u51fa\u3059\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7c21\u5358\u3067\u3059\u306d\u3001curl\u3088\u308a\u3002\u8907\u6570\u540c\u6642\u306b\u3082\u3067\u304d\u308b\u3089\u3057\u3044\u3002 Guzzle\u3092\u4f7f\u7528\u3059\u308b\u6e96\u5099 &hellip; <a href=\"https:\/\/kamita.net\/program\/?p=535\">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\/535"}],"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=535"}],"version-history":[{"count":3,"href":"https:\/\/kamita.net\/program\/index.php?rest_route=\/wp\/v2\/posts\/535\/revisions"}],"predecessor-version":[{"id":538,"href":"https:\/\/kamita.net\/program\/index.php?rest_route=\/wp\/v2\/posts\/535\/revisions\/538"}],"wp:attachment":[{"href":"https:\/\/kamita.net\/program\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kamita.net\/program\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kamita.net\/program\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}