我正在尝试传递json数据并希望在视图中打印,但我得到错误
为foreach()提供的参数无效(查看: C:\ XAMPP \ htdocs中\ myapi \资源\视图\ contacts.blade.php)
这是我正在使用的代码。 Pages控制器具有以下函数来调用API并以JSON格式获取数据。
public function contacts(){
//$rs = file_get_contents("http://someurl.com");
$url ="http://someurl.com/ac/get";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
return view('contacts')->with('leads' ,json_decode($data, true));
}
API返回的数据如下:
[
{
"id":"16828",
"first_name":"Ray",
"last_name":"Smith",
"email":"ray@smith.com",
"phone":"123456789",
"date_added":"2017-12-12 13:32:30"
},
{
"id":"13161",
"first_name":"Lou",
"last_name":"Spa",
"email":"lou@some.com",
"phone":"982345678",
"date_added":"2017-12-01 13:25:33"
}
]
在我看来,我想打印所有潜在客户:
@extends('layouts.app')
@section('content')
<div class="container">
<H3>Contacts </H3>
@foreach ($leads as $key => $user)
{{ $user["id"] }}
{{ $user["name"] }}
{{ $user["email"] }}
@endforeach
</div>
@endsection