我正在尝试致电https://api.binance.com/api/v3/ticker/price/price作为JSON对象,但是我一直在 rand dror to arnay转换到字符串转换当我使用json_decode时。我在这里做错了什么?
<?php namespace AppHelpers;
use GuzzleHttpExceptionGuzzleException;
use GuzzleHttpClient;
class Ticker
{
private $client;
public function __construct()
{
$this->client = new Client(['base_uri' => 'https://api.binance.com/api/']);
}
public function update()
{
$response = json_decode($this->client->get('v3/ticker/price')->getBody());
return $response;
}
}
guzzle响应上的getBody
方法不会返回字符串,它返回流。
尝试:
$this->client->get('v3/ticker/price')->getBody()->getContents()
json_decode
正在将guzzle响应字符串变成PHP数组。然后,您将从控制器方法返回该数组。无论您从控制器返回什么,Laravel都会尝试将其转换为字符串。由于您返回了一个数组,因此您将数组转到字符串转换错误。
要么不解码Guzzle响应,要么将其转换为您想要的字符串或其他响应。