0

So I am new to PHP development, and have been testing out a few scripts recently.

One script that I have attempted to create is one that retrieves the subscriber count of a given YouTube channel, and I have come up with the following script:

<!DOCTYPE html>
<html>
    <head>
        <title>
            Test project.
        </title>
    </head>
    <body>
        <?php
            $json = file_get_contents('https://www.googleapis.com/youtube/v3/channels?part=statistics&forUsername=Stackoverflow&key={API_KEY}');
            $subscribers = json_decode($json);

            var_export(current($subscribers->items)->statistics->subscriberCount);

            print '<p>' . $subscribers . "</p>\n";
        ?>
    </body>
</html>

However, this script seems to break the HTML document, because whenever the page is loaded any tags that come after the PHP script are removed.

Example (resulting HTML document after I use the script above):

<!DOCTYPE html>
<html>
    <head>
        <title>
            Test project.
        </title>
    </head>
    <body>
        '1'

As you can see, the document does not end with the appropriate </body> and </html> tags which are present in the original document.

I have attempted to validate the PHP document using various tools online, but have not managed to figure out the issue. Any help would be appreciated.

5
  • 2
    Try a good IDE and install Xdebug on the testing server. Writing code without good debugging tools is a fool's errand. Commented Jun 12, 2016 at 7:12
  • 1
    $subscribers is an object, You can't print on object. (unless you have created the class yourself and have added a __toString()-method.) Commented Jun 12, 2016 at 7:24
  • Ah, it did seem to be a misunderstanding of the var_export() function. After removing the function call from the start of the line and replacing it with a variable ($sub_count) decleration, the issue was resolved. A beginner mistake on my end. Commented Jun 12, 2016 at 7:29
  • I created an answer with my comment and a couple of examples, so you can mark it as answered if it works for you. Commented Jun 12, 2016 at 7:32
  • @keyboardSmasher I will take another look into an IDE as well as Xdebug, thank you. Commented Jun 12, 2016 at 7:41

2 Answers 2

1

$subscribers is an object, You can't print on object, unless you have created the class yourself and have added a __toString()-method.

This should work:

<?php
    $json = file_get_contents('https://www.googleapis.com/youtube/v3/channels?part=statistics&forUsername=Stackoverflow&key={API_KEY}');
    $subscribers = json_decode($json);

    // Save the value in a variable first
    $count = current($subscribers->items)->statistics->subscriberCount;
    print '<p>' . $count . "</p>\n";

    // Or use it straight away
    print '<p>' . current($subscribers->items)->statistics->subscriberCount . "</p>\n";
?>
Sign up to request clarification or add additional context in comments.

Comments

1

I think under syntax is wrong. print '<p>' . $subscribers . "</p>\n";

How about make some temporal string variable to store $subscribers. ?

Comments

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.