错误:使用枪口时htmlspecialchars()期望参数1为字符串

时间:2019-04-12 08:28:32

标签: laravel guzzle


我正在使用Guzzle发出get请求,并且我有一个Json响应。我试图将Json响应传递给我的视图,但出现诸如title之类的错误。
这是我的控制器:

class RestApi extends Controller
{
    public  function request() {
        $endpoint = "http://127.0.0.1:8080/activiti-rest/service/form/form-data?taskId=21159";
        $client = new \GuzzleHttp\Client();
        $taskId = 21159;

        $response = $client->request('GET', $endpoint, ['auth' => ['kermit','kermit']]);

        $statusCode = $response->getStatusCode();
        $content = json_decode($response->getBody(), true);

        $formData = $content['formProperties'];

        return view('formData')->with('formData', $formData);
    }

}

当我使用dd(formData)时,数据不为空:

enter image description here

在我看来,我只想检查是否已通过formData:

@if(isset($formData))
    @foreach($formData as $formDataValue)
    {{ $formDataValue }}
    @endforeach
    @endif

这是我的路线:

Route::get('/response','RestApi@request')->middleware('cors');

我该如何解决?非常感谢你!

2 个答案:

答案 0 :(得分:1)

您正尝试直接显示数组{{ $formDataValue }}

{{ }}仅支持字符串

例如应该是这样的:

@if(isset($formData))
    <ul>
    @foreach($formData as $formDataValue)
        <li>ID : {{ $formDataValue['id'] }}</li>
        <li>Name : {{ $formDataValue['name'] }}</li>
        <li>Type : {{ $formDataValue['type'] }}</li>
        .
        .
        .
    @endforeach
    </ul>
@endif

答案 1 :(得分:1)

您在这里还有另一个数组:
enter image description here

所以

@if(isset($formData)) //$formData is an array of arrays
    @foreach($formData as $formDataValue)
        {{ $formDataValue }} //$formDataValue is still an array, so it fails
    @endforeach
@endif

{{ }}仅接受字符串。

您必须再次迭代:

@foreach($formData as $formDataItem)
    @foreach($formDataItem as $item)
        //here $item would be a string such as "id", "name", "type", but can also be an array ("enumValues")
    @endforeach
@endforeach

最后

@foreach($formData as $formDataItem)
    @foreach($formDataItem as $item)
        @if(is_array($item))
            @foreach($item as $i) //iterate once more for cases like "enumValues"
                //here you'll have one of "enumValues" array, you have to iterate it again :)
            @endforeach
        @else
            {{ $item }} //you can render a string like "id", "name", "type", ...
        @endif
    @endforeach
@endforeach