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.
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.
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);
What do you want to know about it?
In this line
$data = file_get_contents(sprintf('http://gdata.youtube.com/feeds/api/users/%s', $user));
What does the $user part do?
And on this line
preg_match("~subscriberCount='(?<count>\\d+)'~", $data, $matches)
What does this ‘(?<count>\d+)’~" mean?
Techwiz101:
In this line
$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
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’.
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
If you want to parse an xml file, you might want to look into simplexml
salathe
November 1, 2011, 12:16pm
7
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.
So I use this?
$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?
do a print_r($data) and see what it contains
I could once I get the code working…
There seems to be a problem with this line
$count = (int) $data['entry']['yt$statistics']['subscriberCount'];
Techwiz101:
I could once I get the code working…
There seems to be a problem with this line
$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?
Yes, Parse error: syntax error, unexpected T_STRING in /home/wvvwme/public_html/beta.wvvw.me/test.php on line 6.
SpikeZ
November 2, 2011, 10:34pm
14
try
[COLOR=#000088]$count[/COLOR] [COLOR=#339933]=[/COLOR] [COLOR=#009900]([/COLOR]int[COLOR=#009900])[/COLOR] [COLOR=#000088]$data[/COLOR][COLOR=#009900][[/COLOR][COLOR=#0000FF]'entry'[/COLOR][COLOR=#009900]][/COLOR][COLOR=#009900][[/COLOR][COLOR=#0000FF]'yt'. $statistics[/COLOR][COLOR=#009900]][/COLOR][COLOR=#009900][[/COLOR][COLOR=#0000FF]'subscriberCount'[/COLOR][COLOR=#009900]][/COLOR][COLOR=#339933];[/COLOR]
I’m still getting the same error as above.
salathe
November 4, 2011, 9:21am
17
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?