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

问题描述 投票:0回答:2

我正在使用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)时,数据不为null:

enter image description here

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

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

这是我的路线:

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

我怎样才能解决这个问题?非常感谢你!

laravel guzzle
2个回答
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
投票

你有另一个数组: 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
© www.soinside.com 2019 - 2024. All rights reserved.