0

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

3
  • 1
    Either $token or $data is an array rather than a string. Use dd() or similar to debug the variables. {{ $token }} causes Blade to run htmlentities($token) behind the scenes which then fails because of the above Commented Nov 16, 2015 at 0:10
  • Given that the method used to set $data is entitled streamsFollowed() I'd expect that to return an array given it's use of the plural streams. Commented Nov 16, 2015 at 0:11
  • i checked with dd() and $data is an array, i tried displaying it with @foreach with $data as $stream and then using {{ $stream->streams }} but that failed. Do you know how to correctly display this ? Commented Nov 16, 2015 at 0:41

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Can you update your question and paste the output of dd($data) please?
dd() works, the result is there, but displaying is breaking things up
return view('twitch', ['token' => $token, 'streams' => $data->streams]); gives and other error now -> Trying to get property of non-object
Data is an array. I'd guessed it was an object before you'd posted the structure. You should get used to using dd() to determine the structure of the data you're trying to use. It'll help you fix things like that quickly and easily. Change $data->streams to $data['streams']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.