我正在使用 Guzzle 执行一些对 REST API 的调用,我发现它非常方便。
我想传递一个对象,该对象通常通过 cURL 使用 -d 标志发送,但目前没有预期的结果。
$request = $client->post('rest/user/current/resources/test', [], [
'auth' => [self::USERNAME, self::PASSWORD],
'body' => ['skeletonTest' => $skeletonTest ]
]);
$response =$request->send();
$this->dd($response);
从文档:
-d 'skeletonTest={skeletonTest}'
首要问题:
HTTP/1.1 401 Unauthorized
而凭据是正确的。
第二题:
PHP Notice: Undefined variable: response in ....
这里可能是什么问题?我也尝试过使用:
$request= $client->createRequest('POST','....');
但 getBody() 不会返回有意义的东西。
有什么想法吗?
更新:
当我提供这个时:
$request = $client->post('rest/user/current/resources/test', [
'auth' => [self::USERNAME, self::PASSWORD],
'body' => ['skeletonTest' => $skeletonTest ]
]);
$this->dd($request->send()->getBody());
输出是:
PHP Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Invalid resource type: object' in
因此,当我删除 post() 方法中的第二个参数时,没有 401 但无效参数。$skeletonTest 是一个空类 (stdClass) 的对象。