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

时间:2018-03-06 08:47:47

标签: wordpress laravel guzzle

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

1 个答案:

答案 0 :(得分: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);
    
  4. 简而言之:

    $collection = collect( json_decode( $response->getBody(), true ) );
    $sortedByPrice = $collection->sortBy('price');