如何在Laravel的GuzzleHttp中使用过滤器?

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

我目前正在使用Laravel的WordPress。我在WordPress中发帖并使用WordPress REST API和GuzzleHttp在我的Laravel应用程序中显示帖子。有没有什么办法可以使用不同的过滤器,比如where(),sortBy()等来过滤使用GuzzleHttp从WordPress REST API接收的数据?

wordpress laravel guzzle
1个回答
0
投票

是的,您可以将json响应序列化为Laravel Collection并从那里使用functions like that

  1. 检索响应体,我们假设它包含json。 $response = $response->getBody();
  2. 将json转换为数组。请注意,seconds parameter设置为true以始终输出数组。 $responseArray - json_decode($response, true);
  3. 将数组转换为Laravel集合以使用其酷炫的功能。 $collection = collect($responseArray);

简而言之:

$collection = collect( json_decode( $response->getBody(), true ) );
$sortedByPrice = $collection->sortBy('price');
© www.soinside.com 2019 - 2024. All rights reserved.