我目前正在使用Laravel的WordPress。我在WordPress中发帖并使用WordPress REST API和GuzzleHttp在我的Laravel应用程序中显示帖子。 有没有什么方法可以使用不同的过滤器,比如where(),sortBy()等来过滤使用GuzzleHttp从WordPress REST API接收的数据?
答案 0 :(得分:0)
是的,您可以将json响应序列化为Laravel Collection并从那里使用functions like that。
检索响应主体,我们假设它包含json。
$response = $response->getBody();
将json转换为数组。请注意,seconds parameter设置为true
以始终输出数组。
$responseArray - json_decode($response, true);
将数组转换为Laravel集合,以便使用其酷炫的功能。
$collection = collect($responseArray);
简而言之:
$collection = collect( json_decode( $response->getBody(), true ) );
$sortedByPrice = $collection->sortBy('price');