{"id":2081,"date":"2023-01-13T17:26:58","date_gmt":"2023-01-13T09:26:58","guid":{"rendered":"https:\/\/www.tra56.com\/?p=2081"},"modified":"2023-01-13T17:26:58","modified_gmt":"2023-01-13T09:26:58","slug":"2081","status":"publish","type":"post","link":"https:\/\/www.tra56.com\/2081.html","title":{"rendered":"guzzlehttp\u8bf7\u6c42\u5c01\u88c5"},"content":{"rendered":"
\/**\r\n * \u53d1\u9001\u8bf7\u6c42\r\n * https:\/\/guzzle-cn.readthedocs.io\/zh_CN\/latest\/quickstart.html\r\n *\r\n * @param string $url\r\n * @param array $params\r\n * @param string $method\r\n * @param array $configs\r\n * @param string $contentType\r\n * @return array\r\n *\/\r\n public static function request(string $url, array $params = [], string $method = 'POST', array $configs = [], string $contentType='form_params')\r\n {\r\n $configs['timeout'] = array_get($configs, 'timeout', 5);\r\n $client = new Client($configs);\r\n $params = strtoupper($method) == 'GET' ? ['query' => $params] : [$contentType => $params];\r\n Log::info("httpRequest send", ['url' => $url, 'params' => $params, 'method' => $method, 'configs' => $configs]);\r\n\r\n try {\r\n $request = $client->request($method, $url, $params);\r\n } catch (RequestException $e) {\r\n $errorCode = $e->getCode();\r\n $errorMessage = $e->getMessage();\r\n\r\n Log::info("httpRequest response error:", ['url' => $url, 'params' => $params, 'method' => $method,\r\n 'configs' => $configs, 'errorCode' => $errorCode, 'errorMessage' => $errorMessage]);\r\n\r\n return [\r\n 'success' => false,\r\n 'errorCode' => $errorCode,\r\n 'errorMessage' => $errorMessage,\r\n ];\r\n }\r\n\r\n $httpCode = $request->getStatusCode();\r\n $return = $request->getBody()->getContents();\r\n $response = json_decode($return, true);\r\n $success = $httpCode == 200 ? 'success' : 'error';\r\n\r\n Log::info("httpRequest response $success:", ['url' => $url, 'params' => $params, 'method' => $method,\r\n 'configs' => $configs, 'httpCode' => $httpCode, 'response' => $response]);\r\n\r\n if($httpCode != 200) {\r\n return [\r\n 'success' => false,\r\n 'errorCode' => $httpCode,\r\n 'errorMessage' => '',\r\n ];\r\n }\r\n\r\n return [\r\n 'success' => true,\r\n 'data' => $response\r\n ];\r\n }<\/pre>\n<\/p>\n","protected":false},"excerpt":{"rendered":"
\/** * \u53d1\u9001\u8bf7\u6c42 &nb […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,92],"tags":[],"class_list":["post-2081","post","type-post","status-publish","format-standard","hentry","category-php","category-phpcode"],"_links":{"self":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2081"}],"collection":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/comments?post=2081"}],"version-history":[{"count":1,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2081\/revisions"}],"predecessor-version":[{"id":2082,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2081\/revisions\/2082"}],"wp:attachment":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media?parent=2081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/categories?post=2081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/tags?post=2081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}