为什么httpClient的$response->getBody()返回NULL?

问题描述 投票:0回答:1

我尝试使用 Http::get 方法从 https://climate-api.open-meteo.com 服务获取数据,但正在运行请求 方法 getBody 返回 null,而不是数据数组:

$response = Http::get('https://climate-api.open-meteo.com/v1/climate', [
'query' => [
'latitude' => $latitude,
'longitude' => $longitude,
'start_date' => $from->format('Y-m-d'),
'end_date' => $to->format('Y-m-d'),
]
]);

// RETURNS TRUE
\Log::info(varDump($response->successful(), ' -10 $response->successful()::'));


// RETURNS 200
\Log::info(varDump($response->getStatusCode(), ' -11 $response->getStatusCode()::'));


// RETURNS NULL
\Log::info(varDump(json_decode($response->getBody(), true), ' -12 $response->getBody()::'));

提出样品请求,例如:

https://climate-api.open-meteo.com/v1/climate?latitude=40.4165&longitude=-3.7026&start_date=2023-07-09&end_date=2023-07-11&models=CMCC_CM2_VHR4&daily=temperature_2m_max

我获得了有效的数据结构,但没有看到我在 $response->getBody() 方法中获得了无效数据?

laravel httpclient
1个回答
0
投票

我认为错误在于您提供的查询字符串,您应该通过以下方式传递:

$response = Http::get('https://climate-api.open-meteo.com/v1/climate', [
    'latitude'   => '40.4165',
    'longitude'  => '3.7026',
    'start_date' => '2023-07-09',
    'end_date'   => '2023-07-11',
    // 'models'     => 'CMCC_CM2_VHR4',
    // 'daily'      => 'temperature_2m_max'
]);

此外,没有名为

getBody
的方法,但它被称为
body
,如 Laravel 所示here

© www.soinside.com 2019 - 2024. All rights reserved.