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.

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题