Syntax error in PHP script for conditional Twitch stream embedding

I’m trying to create a PHP script that shows a Twitch stream embed only when the channel is currently broadcasting. However, I keep running into this error message:

FATAL ERROR syntax error, unexpected ‘http’ (T_STRING), expecting ‘,’ or ‘;’ on line number 8

Here’s my current code:

<?php
$channelName = "teststreamer";

$apiResponse = json_decode(file_get_contents("https://api.twitch.tv/kraken/streams.json?channel=$channelName"), true);

if(isset($apiResponse['streams'][0]['channel'])) {
echo "<div class='live-container'><iframe
src='https://player.twitch.tv/?channel=teststreamer'
id='stream-player'
height='100%'
width='100%'
frameborder='0'
scrolling='no'
allowfullscreen='true'>
</iframe></div>";
} else {
echo "<div class='offline-message'>Stream is currently offline</div>";
}

?>

The basic idea is to check if the stream is active using Twitch’s API and then conditionally display the embedded player. What am I doing wrong with the syntax here?

Your echo statement breaks across multiple lines without proper concatenation. PHP sees the line break and thinks the string ended, then hits the URL on the next line and throws a syntax error. You need to escape the newlines or concatenate properly. Try this:

echo "<div class='live-container'><iframe src='https://player.twitch.tv/?channel=teststreamer' id='stream-player' height='100%' width='100%' frameborder='0' scrolling='no' allowfullscreen='true'></iframe></div>";

You could also use heredoc syntax - it handles multiline strings much better. BTW, that Kraken API you’re using is deprecated. Switch to the newer Helix API with proper auth headers for better reliability.

your echo spans multiple lines without proper string handling. php treats each line as separate code. use heredoc - it’s the easiest fix:

echo <<<HTML
<div class='live-container'><iframe
src='https://player.twitch.tv/?channel=teststreamer'
id='stream-player'
height='100%'
width='100%'
frameborder='0'
scrolling='no'
allowfullscreen='true'>
</iframe></div>
HTML;

no concatenation needed and way more readable.

Your multiline echo is breaking because PHP sees each new line as a separate statement. That’s why you’re getting the ‘http’ error - PHP thinks the URL is a new command.

Quick fix with concatenation:

echo "<div class='live-container'><iframe " .
"src='https://player.twitch.tv/?channel=teststreamer' " .
"id='stream-player' height='100%' width='100%' " .
"frameborder='0' scrolling='no' allowfullscreen='true'>" .
"</iframe></div>";

I’d use double quotes and escape when needed - cleaner to read. Also, file_get_contents can fail silently, so add error handling for when the API request fails.