最近在阅读老项目的时候,发现服务器对外请求前面的开发者都是自己写一大段curl,但是风格各异(估计经历了多人维护) ,于是便想起引入Guzzle来发送请求,尽可能让代码简洁高效。
1.0版本
<?php
$appId = C('APPID');
$secret = C('APPKEY');
$url = C('URL');
$client = new Client();
//start获取鉴权token
if (!S('esign_token')) {
//前辈封装的方法^_^
$res = curl($url . "/v1/oauth2/access_token?appId={$appId}&secret={$secret}&grantType=client_credentials", 'GET');
if ($res) {
$res = json_decode($res);
if ($res->code == 0) {
$token = $res->data->token;
S('esign_token', $token, 280);
} else {
exit_error(-1, $res->message);
}
} else {
exit_error(-1, '网络繁忙,请稍后再试.');
}
} else {
$token = S('esign_token');
}
//END获取鉴权token
$response = $client->request('PUT', $url . "/v2/identity/auth/pub/individual/{$flowId}/bankCard4Factors", [
'headers' => [
'X-Tsign-Open-App-Id' => $appId,
'X-Tsign-Open-Token' => $token
],
'json' => [
'authcode' => $data['code']
]
]);
if ($response->getStatusCode() != 200) {
exit_error(-1, '网络繁忙,请稍后再试试');
}
$body = json_decode($response->getBody());
if ($body->code == 0) {
$result = M('xxx')->where(['uid' => $uid])->save(['is_auth' => 888]);
if (!$result) {
exit_error(-1, '修改实名状态出错');
} else {
$h->editNickname($user_id, $data['name']);
cnl_exit_json(0, '恭喜您,实名认证通过');
}
} else {
exit_error($body->code, $body->message);
}
?>
但是单单这样改造的话,好像还是过于啰嗦,并没有带来什么改变,想一下这里每次请求带上的头信息基本是相同的,然后返回的结果判断也是基本相同,鉴于axios使用过拦截器,于是找到Guzzle手册有一共相关的中间件能实现请求之前自动添加头,请求返回后判断状态,如果状态码都正确就将需要的内容序列化成对象后,将预处理的结果返回
2.0版本
<?php
namespace Common\Common;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Think\Controller;
class Eclient extends Controller
{
private static $appId;
private static $url;
private static $appKey;
public static function getClient()
{
self::$appId = C('ESIGN.APPID');
self::$url = C('ESIGN.URL');
self::$appKey = C('ESIGN.APPKEY');
$token = S('token');
if (!$token = S('token')) {
$token = self::_getToken();
echo "shengqing";
if ($token) {
S('token', $token, 290);
} else {
self::error('获取token失败');
}
} else {
$token = S('token');
}
$stack = new HandlerStack();
$stack->setHandler(new CurlHandler());
$stack->push(Middleware::mapRequest(function (RequestInterface $request) use ($token) {
return $request->withHeader('X-Tsign-Open-App-Id', self::$appId)
->withHeader('X-Tsign-Open-Token', $token);
}));
$stack->push(Middleware::mapResponse(function (ResponseInterface $response) {
if ($response->getStatusCode() != 200 || json_decode($response->getBody())->code != 0) {
$msg = json_decode($response->getBody())->message ?: '空返回';
$this->error($msg);
} else {
var_dump($response);
return $response->withBody(json_decode($response->getBody())->data);
}
}));
return new Client(['handler' => $stack, 'base_uri'=>self::$url]);
}
private function _getToken()
{
$client = new Client();
$response = $client->get(self::$url . "/v1/oauth2/access_token", [
'query' => [
'appId' => self::$appId,
'secret' => self::$appKey,
'grantType' => 'client_credentials'
]
]);
if ($response->getStatusCode() == 200 && json_decode($response->getBody())->code == 0) {
return json_decode($response->getBody())->data->token;
} else {
return false;
}
}
}
封装完成后,在调用的地方两句话即可完成调用
$client = Eclient::getClient();
$response = $client->get("/v1/accounts/xxx");
$this->success($response->getBody()->name);