error: htmlspecialchars() expects parameter 1 to be string when use guzzle

error: htmlspecialchars() expects parameter 1 to be string when use guzzle


我正在使用 Guzzle 发出获取请求,并且收到 Json 响应。我试图将 Json 响应传递给我的视图,但收到类似标题的错误。
这是我的控制器:

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)时,数据不为空:

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

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

这是我的路线:

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

我该如何解决这个问题?非常感谢!

您正在尝试直接显示数组{{ $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

你这里有另一个数组:

所以,

@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