情况:
我需要将请求发送到api以更新帐户信息。 API文档说我需要向PAPI发送一个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();
}
}
?>
答案 0 :(得分:0)
400 Bad Request
的意思是:客户端由于语法无效而发送的请求。
根据itemize api documentation“市场”应作为字符串数组传递。您还可以使用json格式。
尝试一下:
public function updateAccountInfo() {
$response = $this->client->request('PUT', '', [
'json' => ["markets" => ["UK"]],
]);
echo $response->getBody()->getContents();
}