这是我从GuzzleHttp函数authMiddleware()收到的错误。我将此用于VismaSign并收到这些错误。我不确定到底是什么问题:
未捕获的TypeError:参数1传递给 app \ handlers \ integrations \ VismaSign :: authMiddleware()必须是 GuzzleHttp \ Psr7 \ Request的实例, GuzzleHttp \ HandlerStack给出。
这是我的代码:
<?php
namespace app\handlers\integrations;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
class VismaSign {
const API_URL = "https://sign.visma.net/api/v1/";
const CLIENT_IDENTIFIER = "ddf58116-6082-4bfc-a775-0c0bb2f945ce";
const SECRET_KEY = "jp7SjOOr4czRTifCo30qx0sZAIw9PW+vVpsbP09pQaY=";
private $client;
private $clientHandler;
public function __construct() {
$this->client = new Client();
$this->clientHandler = $this->client->getConfig('handler');
}
public function authMiddleware(Request $request) {
$request = new Request('POST', 'https://httpbin.org/post');
//return $request->withHeader('Authorization', $this->authorizationHeader(self::CLIENT_IDENTIFIER, base64_decode(self::SECRET_KEY), $request->getMethod(), (string)$request->getBody(), ($request->getHeader('Content-Type') ?: [''])[0], $request->getHeader('Date')[0], $request->getUri()->getPath() . ($request->getUri()->getQuery() ? '?' . $request->getUri()->getQuery() : '')));
return $request->withHeader(
'Authorization',
$this->authorizationHeader(
self::CLIENT_IDENTIFIER,
base64_decode(self::SECRET_KEY),
$request->getMethod(),
(string)$request->getBody(),
($request->getHeader('Content-Type') ?: [''])[0],
$request->getHeader('Date')[0],
$request->getUri()->getPath() .
($request->getUri()->getQuery() ? '?' . $request->getUri()->getQuery() : '')
)
);
}
private function authorizationHeader($username, $secret, $method, $body, $contentType, $date, $path) {
$str = join("\n", [
$method,
base64_encode(hash('md5', $body, true)),
$contentType,
$date,
$path
]);
$header = 'Onnistuu ' . $username . ':' . base64_encode(hash_hmac('sha512', $str, $secret, true));
return $header;
}
public function createDocument() {
$response = $this->client->request('POST', self::API_URL . 'document/', [
'json' => [
'document' => [
'name' => 'Test document ' . date(\DateTime::ATOM),
]
],
'headers' => [
'Date' => date('r'),
],
'handler' => $this->authMiddleware($this->clientHandler),
]);
if ($response->getStatusCode() !== 201) {
echo 'Could not create document';
return;
}
return $response->getHeader('Location')[0];
}
}
答案 0 :(得分:0)
在构造函数中创建添加类request
的新对象GuzzleHttp\Psr7\Request
:
public function __construct() {
$this->request = new Request();
$this->client = new Client();
$this->clientHandler = $this->client->getConfig('handler');
}
,并且在您的createDocument
方法中仅传递以下内容:
'handler' => $this->authMiddleware($this->request),
因为您的错误明确说明
必须是GuzzleHttp\Psr7\Request
的实例,GuzzleHttp\HandlerStack
的实例