要在Laravel控制器中调用Binance API,您可以按照以下步骤操作:
guzzlehttp/guzzle
包,可以通过运行以下命令来安装:composer require guzzlehttp/guzzle
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
public function binanceApiCall()
{
$client = new Client();
try {
$response = $client->request('GET', 'https://api.binance.com/api/v3/ticker/price');
$body = $response->getBody();
$data = json_decode($body, true);
// 处理API响应数据...
return response()->json($data);
} catch (RequestException $e) {
// 处理请求异常...
return response()->json(['error' => 'API请求失败'], 500);
}
}
Route::get('/binance-api', 'YourController@binanceApiCall');
请注意,上述代码仅作为示例,您需要根据自己的需求进行相应的更改和扩展。此外,确保您的服务器可以访问Binance API,并根据需要进行身份验证。