【发布时间】:2023-03-20 12:37:01
【问题描述】:
情况:
我需要将请求发送到 api 以更新帐户信息。 API 文档说我需要向 API 发送 PUT 请求。 我尝试在 Laravel 5.6 中执行此操作,尽管我认为这并不重要。
我目前所拥有的:
Guzzle 客户端的工作构造函数; 检索帐户信息的工作函数。
什么不起作用:
提交请求后,我收到 Guzzle 异常
Client error: \`PUT https://sandbox.proapi.itemize.com/api/enterprise/v1/accounts/<my account id>\` resulted in a \`400 Bad Request\` response: IOException:
这是我目前的代码:
<?php
namespace App\Http\Controllers;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
class ApiController extends Controller {
private $apiKey;
private $uri;
private $client;
public function __construct() {
$this->apiKey = 'my api key';
$this->uri = 'https://sandbox.proapi.itemize.com/api/enterprise/v1/accounts/my account id';
$this->client = new Client([
'base_uri' => $this->uri,
'auth' => [null, $this->apiKey]]);
}
public function accountInfo() {
$response = $this->client->request('GET','');
echo $response->getBody()->getContents();
}
public function updateAccountInfo() {
$response = $this->client->request('PUT','',[
'headers' => [
'Content-Type' => 'application/json',
],
'body' => '{"markets":"UK"}'
]);
echo $response->getBody()->getContents();
}
}
?>
【问题讨论】: