简体   繁体   中英

Laravel htmlentities() expects parameter 1 to be string, array given

I'm trying to use https://github.com/skmetaly/laravel-twitch-restful-api package to get twitch integration to my website.

That's the error that i get.

ErrorException in helpers.php line 469:
htmlentities() expects parameter 1 to be string, array given (View: /var/www/rafdev.ovh/html/msvixen/resources/views/twitch.blade.php)

My controller $code = Input::get('code');

    if ($code !== null)
    {
        $token = TwitchApi::requestToken($code);
    } else
    {
        $token = null;
    }

    $data = TwitchApi::streamsFollowed($token);

    return view('twitch', ['token' => $token, 'data' => $data]);

my view

@extends('master')

@section('content')
    <h1>Twitch.TV</h1>
    {{ $token }}

    {{ $data }}
@endsection

After using dd()

array:9 [▼
  0 => array:11 [▼
    "_id" => 17733016640
    "game" => "World of Warcraft"
    "viewers" => 15551
    "created_at" => "2015-11-15T22:27:13Z"
    "video_height" => 1080
    "average_fps" => 60.2769481401
    "delay" => 0
    "is_playlist" => false
    "_links" => array:1 [▶]
    "preview" => array:4 [▶]
    "channel" => array:22 [▶]
  ]
  1 => array:11 [▶]
  2 => array:11 [▶]
  3 => array:11 [▶]
  4 => array:11 [▶]
  5 => array:11 [▶]
  6 => array:11 [▶]
  7 => array:11 [▶]
  8 => array:11 [▶]
]

so it works, but when i try to display data - its back to the htmlentities() error

This is happening because $data is returned as an array.

When TwitchApi::streamsFollowed($token); is called, the Facade calls the method in Skmetaly\\TwitchApi\\Services\\TwitchApiService .

This in turn creates an instance of Skmetaly\\TwitchApi\\API\\Users and calls the streamsFollowed() method there.

This method makes a call to /streams/followed which returns a data set such as the example below. It's automatically converted to an array rather than JSON using the Guzzle HTTP Client's json() method .

{
  "_links": {
    "self": "https://api.twitch.tv/kraken/streams/followed?limit=25&offset=0",
    "next": "https://api.twitch.tv/kraken/streams/followed?limit=25&offset=25"
  },
  "_total": 123,
  "streams": [...]
}

In order to display the streams you'd need to iterate over the streams array within $data .

If you were to modify your controller slightly

return view('twitch', ['token' => $token, 'streams' => $data->streams]);

You'd then be able to iterate over the streams in your view.

@foreach($streams as $stream)
    {{ $stream }}
@endforeach

Update: You'll notice that each stream is also an array. What this means is you need to choose which of the keys in each array you'd like to display. Let's assume that inside one of the streams there is a key called broadcaster which contains a string; you could modify the above as follows.

@foreach($streams as $stream)
    {{ $stream['broadcaster'] }}
@endforeach

Having now read the streams example response documentation it would appear that the contents of a stream varies depending on whether or not the stream is online. NB: This is assuming the data structure is the same as you've not posted the contents of a stream in your question.

This means that offline, {{ $stream['broadcaster'] }} would work, but when online it wouldn't and you'd get the same error. What you'll likely need to do is use an @if @else block in your @foreach to determine if the stream is null before trying to echo part of the information.

You could also filter the offline streams in the controller by removing null values from data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM