• 0

Need help displaying downloads from mozilla


Question

Awhile ago the Neowins helped me with some code to display the downloads from the addons download page. https://addons.mozilla.org/en-US/firefox/addon/4780/ But I guess Mozilla changed something and now it stopped working. Can anyone help me again? :D

here is the old code

													<?php
													function get_mozilla_theme_download_counter($id){
       														$xpath = new DOMXPath(@DOMDocument::loadHTMLFile(
        													sprintf(
        													'https://addons.mozilla.org/en-US/firefox/addon/%d',
        													$id
        													)
        													));
       														return $xpath->query('//strong[@class="downloads"]')->item(0)->nodeValue;
													}

													echo get_mozilla_theme_download_counter(4780); #8,379,779
													?>

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0
<?php
function get_mozilla_theme_download_counter($id){
 $xpath = new DOMXPath(@DOMDocument::loadHTMLFile(
	sprintf(
 	'https://addons.mozilla.org/en-US/firefox/addon/%d/',
 	$id
	)
 ));
 return $xpath->query('//strong[@class="downloads"]')->item(0)->nodeValue;
}

echo get_mozilla_theme_download_counter(4908); #11,521,465
?>

Link to comment
Share on other sites

  • 0

No problem, but here's a much more robust solution, returns 0 on error.

<?php
function get_mozilla_theme_download_count($id, $url = 'https://addons.mozilla.org/en-US/firefox/addon/%d/'){
 $count = 0;
 if(false !== ($doc = @DOMDocument::loadHTMLFile(sprintf($url, $id)))){
	$xpath = new DOMXPath($doc);
	$nodes = $xpath->query('//strong[@class="downloads"]');
	if(0 < $nodes->length){
 	$count = preg_replace('~[^0-9]~', null, $nodes->item(0)->nodeValue);
	}
 }
 return $count;
}

echo get_mozilla_theme_download_count(4908); #11521465
?>

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.