dougao2830 2015-11-15 23:57
浏览 348
已采纳

Laravel htmlentities()期望参数1为字符串,给定数组

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

  • 写回答

1条回答 默认 最新

  • dongzhang4301 2015-11-16 00:35
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向