封装使用Client作为请求体
写入日志这是之前封装的日志
<?php
namespace App\Common;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use App\Common\ApiLog as Log;
class HttpClient
{
/**
* @throws GuzzleException
*/
public static function get($uri, array $queryOptions = [], $headers = []): array
{
$client = new Client(['base_uri' => $uri, 'timeout' => 5]);
$requestOptions = [
'query' => $queryOptions,
'headers' => $headers
];
if (empty($queryOptions) && empty($headers)) {
$requestOptions = [];
}
$log = [
'url' => $uri,
'method' => 'GET',
'remark' => 'HttpClient get',
'request' => $requestOptions
];
try {
$response = $client->get($uri, $requestOptions);
$content = json_decode($response->getBody()->getContents(), true);
$status = $response->getStatusCode();
$log['statusCode'] = $status;
$log['body'] = $content;
Log::debug($log);
return [$status, $content];
} catch (\Throwable $throwable) {
$log['error'] = $throwable->getMessage();
$log['line'] = $throwable->getLine();
Log::error($log);
return [HTTP_ERROR, array('errorMsg' => $throwable->getMessage())];
}
}
public static function postForm($uri, array $formParams = [], array $headers = []): array
{
$requestOptions = [
'form_params' => $formParams,
'headers' => $headers
];
return self::postAction($uri, $requestOptions);
}
public static function postJson($uri, $jsonParams = [], array $headers = []): array
{
$headers = array_merge(['Content-Type' => 'application/json; charset=utf-8'], $headers);
$requestOptions = [
'json' => $jsonParams,
'headers' => $headers
];
return self::postAction($uri, $requestOptions);
}
private static function postAction($uri, array $options = []): array
{
$client = new Client(['base_uri' => $uri, 'timeout' => 20]);
$log = [
'url' => $uri,
'method' => 'POST',
'remark' => 'HttpClient postAction',
'request' => $options
];
try {
$response = $client->post($uri, $options);
$content = json_decode($response->getBody()->getContents(), true);
$status = $response->getStatusCode();
if (strpos($uri, "lumen") === false) {
$log['statusCode'] = $status;
$log['body'] = $content;
Log::debug($log);
}
return [$status, $content];
} catch (\Throwable $throwable) {
$log['error'] = $throwable->getMessage();
$log['line'] = $throwable->getLine();
Log::error($log);
return [HTTP_ERROR, array('errorMsg' => $throwable->getMessage())];
}
}
public static function delete($uri, array $queryOptions = [], $headers = [], $body = []): array
{
$client = new Client(['base_uri' => $uri, 'timeout' => 5]);
$requestOptions = [
'query' => $queryOptions,
'headers' => $headers,
];
if (!empty($body)) {
$requestOptions['json'] = $body;
}
$log = [
'url' => $uri,
'method' => 'DELETE',
'remark' => 'HttpClient delete',
'request' => $requestOptions,
];
try {
$response = $client->delete($uri, $requestOptions);
$content = json_decode($response->getBody()->getContents(), true);
$status = $response->getStatusCode();
$log['statusCode'] = $status;
$log['body'] = $content;
Log::debug($log);
return [$status, $content];
} catch (\Throwable $throwable) {
$log['error'] = $throwable->getMessage();
$log['line'] = $throwable->getLine();
Log::error($log);
return [HTTP_ERROR, array('errorMsg' => $throwable->getMessage())];
}
}
public static function isSuccess($statusCode): bool
{
if ($statusCode >= 200 && $statusCode < 300) {
return true;
}
return false;
}
}
简单的调取示例
private static function extracted($backUrl, $authorization, array $paramData)
{
$header = ["Authorization" => $authorization];
list($status, $content) = HttpClient::postJson($backUrl, $paramData, $header);
if (HttpClient::isSuccess($status) && isset($content['code']) && $content['code'] == 0) {
return response(['retcode' => 0, 'retmsg' => 'OK']);
}
return response(['retcode' => -1, 'retmsg' => 'FAIL']);
}