我正在制作一个网站,该网站使用twitch api搜索游戏并列出正在玩该游戏的流。一切都很好,除了当我在搜索有空格的游戏时出错。例如,它会列出minecraft流,但在尝试制作《英雄联盟》时出错。
<?php
$game = $_GET['game'];
$json_file = @file_get_contents("https://api.twitch.tv/kraken/streams?game{$game}", 0, null, null);
$json = json_decode($json_file);
foreach ($json->streams as $stream) {
echo($stream->channel->name);
echo "<br>";
}
?>
使用urlencode
准备要在url中使用的字符串:
$game = $_GET['game']
$game = urlencode($game);
$json_file = @file_get_contents("https://api.twitch.tv/kraken/streams?game{$game}", 0, null, null);