<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Vinelab\Http\Client as HttpClient;
use App\Requests\SearchRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class SearchResults extends Controller
{
public function index()
{
return view('results.search-results');
}
public function store(Requests\SearchRequest $request)
{
$search_phrase = $request->input('search');
$client = new HttpClient;
$response = $client->get('https://www.reddit.com/search.json?q='. $search_phrase .'');
$responseArray = $response->json();
dd($responseArray);
return view('results.search-results');
}
}
Using the above code, I make a call to the reddit API using this HTTP service
使用上面的代码,我使用此HTTP服务调用reddit API
https://github.com/Vinelab/http/tree/master
The response that comes back gives me an Array of a lot of data, but I only want to get the title field from this and Parse it into a Laravel array that can be sent to a view where I will display the titles in a foreach loop.
回来的响应给了我一个包含大量数据的数组,但我只想从这里获取title字段并将其解析为Laravel数组,该数组可以发送到一个视图,我将在foreach循环中显示标题。
I thought to maybe store the title of the results in the DB and then query the DB and send that through to the view. I am new to all of this so any assistance and theory will be appreciated.
我想可能会将结果的标题存储在数据库中,然后查询数据库并将其发送到视图中。我是所有这一切的新手,所以任何帮助和理论都将受到赞赏。
Is there a way in Laravel 5.2 to convert the output of this JSON array to a usable array that can be compact and sent to the view?
在Laravel 5.2中是否有办法将此JSON数组的输出转换为可以紧凑并发送到视图的可用数组?
1 个解决方案
#1
18
You can do this, to convert json into Array format.
您可以这样做,将json转换为Array格式。
json_decode($response->content(), true);
and can access via this
并可以通过此访问
$response[0]['title']
#1
18
You can do this, to convert json into Array format.
您可以这样做,将json转换为Array格式。
json_decode($response->content(), true);
and can access via this
并可以通过此访问
$response[0]['title']