{"id":954,"date":"2023-08-03T17:19:49","date_gmt":"2023-08-03T17:19:49","guid":{"rendered":"http:\/\/robinluo.top\/?p=954"},"modified":"2023-08-03T17:20:23","modified_gmt":"2023-08-03T17:20:23","slug":"php-guzzle-%e5%8f%91%e9%80%81http-%e8%af%b7%e6%b1%82","status":"publish","type":"post","link":"https:\/\/robinluo.top\/?p=954","title":{"rendered":"php guzzle \u53d1\u9001http \u8bf7\u6c42"},"content":{"rendered":"\n<p>docs  <a href=\"https:\/\/docs.guzzlephp.org\/\" data-type=\"URL\" data-id=\"https:\/\/docs.guzzlephp.org\/\">https:\/\/docs.guzzlephp.org\/<\/a><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>install<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>composer require guzzlehttp\/guzzle\n<\/code><\/pre>\n\n\n\n<p>sync get<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$client = new \\GuzzleHttp\\Client();\n$response = $client->request('GET', 'https:\/\/api.github.com\/repos\/guzzle\/guzzle');\n\necho $response->getStatusCode(); \/\/ 200\necho $response->getHeaderLine('content-type'); \/\/ 'application\/json; charset=utf8'\necho $response->getBody(); \/\/ '{\"id\": 1420053, \"name\": \"guzzle\", ...}'\n<\/code><\/pre>\n\n\n\n<p>async get<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$request = new \\GuzzleHttp\\Psr7\\Request('GET', 'http:\/\/httpbin.org');\n$promise = $client->sendAsync($request)->then(function ($response) {\n    echo 'I completed! ' . $response->getBody();\n});\n\n$promise->wait();<\/code><\/pre>\n\n\n\n<p>post<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$client = new \\GuzzleHttp\\Client();\n$response = $client->request('POST', 'https:\/\/api.github.com\/repos\/guzzle\/guzzle',\n &#91;\n   'headers' => &#91;\n        'User-Agent' => 'testing\/1.0',\n        'Accept'     => 'application\/json',\n        'X-Foo'      => &#91;'Bar', 'Baz']\n    ],\n    \/\/'query' => &#91;'foo' => 'bar']  \/\/query params\n    \/\/'body' => 'foo'   \/\/ plain string BODY\n    \/* 'form_params' => &#91;   \/\/  application\/x-www-form-urlencoded\n        'foo' => 'bar',\n        'baz' => &#91;'hi', 'there!']\n    ]*\/\n    \/\/'json' => &#91;'foo' => 'bar']]  \/\/json body  application\/json\n\n    \/*'multipart' => &#91;    multipart    multipart\/form-data\n        &#91;\n            'name'     => 'foo',\n            'contents' => 'data',\n            'headers'  => &#91;'X-Baz' => 'bar']\n        ],\n        &#91;\n            'name'     => 'baz',\n            'contents' => Psr7\\Utils::tryFopen('\/path\/to\/file', 'r')\n        ],\n        &#91;\n            'name'     => 'qux',\n            'contents' => Psr7\\Utils::tryFopen('\/path\/to\/file', 'r'),\n            'filename' => 'custom_filename.txt'\n        ],\n    ]*\/\n\n\n ],\n \n);\n\necho $response->getStatusCode(); \/\/ 200\necho $response->getHeaderLine('content-type'); \/\/ 'application\/json; charset=utf8'\necho $response->getBody(); \/\/ '{\"id\": 1420053, \"name\": \"guzzle\", ...}'<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>docs https:\/\/docs.guzzlephp.org\/ install sync get async get post<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[153,37],"_links":{"self":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/954"}],"collection":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=954"}],"version-history":[{"count":1,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/954\/revisions"}],"predecessor-version":[{"id":956,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/954\/revisions\/956"}],"wp:attachment":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}