SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Could someone please explain this piece of code?

    Could someone please explain this piece of code?
    I found it on this thread and I would like to use it on the site I'm working on.

    Code PHP:
    function getYouTubeSubscriberCount($user)
    {
        $data = file_get_contents(sprintf('http://gdata.youtube.com/feeds/api/users/%s', $user));
        $matches = array();
        preg_match("~subscriberCount='(?<count>\d+)'~", $data, $matches);
        return isset($matches['count']) ? $matches['count'] : 0 ;
    }
    echo getYouTubeSubscriberCount('sonybmg'); #181218
    ?>

    Currently here's the changes I've made.
    Code PHP:
    function getYouTubeSubscriberCount($user)
    {
        $data = file_get_contents(sprintf('http://gdata.youtube.com/feeds/api/users/%s', $user));
        $matches = array();
        preg_match("~subscriberCount='(?<count>\d+)'~", $data, $matches);
        return isset($matches['count']) ? $matches['count'] : 0 ;
    }
    echo getYouTubeSubscriberCount($username);

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,401
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    What do you want to know about it?

  3. #3
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In this line
    Code PHP:
    $data = file_get_contents(sprintf('http://gdata.youtube.com/feeds/api/users/%s', $user));

    What does the $user part do?

    And on this line
    Code PHP:
     preg_match("~subscriberCount='(?<count>\d+)'~", $data, $matches)

    What does this '(?<count>\d+)'~" mean?

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,401
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Techwiz101 View Post
    In this line
    Code PHP:
    $data = file_get_contents(sprintf('http://gdata.youtube.com/feeds/api/users/%s', $user));

    What does the $user part do?
    http://www.php.net/sprintf
    The %s placeholder in the string is substituted with the value of $user.
    And on this line
    Code PHP:
     preg_match("~subscriberCount='(?<count>\d+)'~", $data, $matches)

    What does this '(?<count>\d+)'~" mean?
    http://www.php.net/preg_match
    See example #4.
    This part of the regex gets the numbers following 'subscriberCount=' and puts it in the $matches array with key 'count'.

  5. #5
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, thanks.

    I tried that code to extract the subscriber count from Youtube and it worked as it's ment to but I can't seem to get it working on any other fields.


    Here's the xml page I'm trying to get the data from.

    gdata.youtube.com/feeds/api/users/klayfx

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,401
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    If you want to parse an xml file, you might want to look into simplexml

  7. #7
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    54 Post(s)
    Tagged
    0 Thread(s)
    My reply (in that same thread) might be easier to make sense of (compared to using regex to work with XML!), it retrieves the information in JSON format and makes it available in a PHP array.
    Salathe
    Software Developer and PHP Manual Author.

  8. #8
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So I use this?

    Code PHP:
    $name = 'sitepoint';
    $url   = sprintf('http://gdata.youtube.com/feeds/api/users/%s?alt=json', urlencode($name));
    $json  = @file_get_contents($url); // Naughty @-operator, use proper error handling
    $data  = json_decode($json, TRUE);
    $count = (int) $data['entry']['yt$statistics']['subscriberCount'];
     
    echo $count;

    But how do I use that to get not only just the sub count but also the username as it is on youtube(same caps), the channel views, the total upload views, the amount of friends/contacts, the amount of people they're subbed to and how many favorites they have?

  9. #9
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,401
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    do a print_r($data) and see what it contains

  10. #10
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I could once I get the code working...

    There seems to be a problem with this line
    Code PHP:
    $count = (int) $data['entry']['yt$statistics']['subscriberCount'];

  11. #11
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,401
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Techwiz101 View Post
    I could once I get the code working...

    There seems to be a problem with this line
    Code PHP:
    $count = (int) $data['entry']['yt$statistics']['subscriberCount'];
    If you get to that line, then you can also do a print_r

    And what does 'a problem' mean? Do you get an error?

  12. #12
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, Parse error: syntax error, unexpected T_STRING in /home/wvvwme/public_html/beta.wvvw.me/test.php on line 6.

  13. #13
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Guys?

  14. #14
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    try
    Code:
    $count = (int) $data['entry']['yt'. $statistics]['subscriberCount'];
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  15. #15
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm still getting the same error as above.

  16. #16
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,401
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Which one is line 6?

  17. #17
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    54 Post(s)
    Tagged
    0 Thread(s)
    When debugging, show your code (not just a line) and describe what you've done to get the error. For all we know, you might have just mistyped a character.

    As for getting the other values, if they're there in the JSON then it's just a matter of finding the right array syntax. Have you read up on how to get values from nested arrays like you have?
    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •