Jump to content

Dom Requests Taking Ages To Load / Also Crash Iis


LeeMC1989

Recommended Posts

Hi there!

 

I have some code which connects to iTunes to grab the top40 from a RSS feed, the problem in which I'm facing is when I try to visit this page it takes about 5-10 seconds to load, also if i try to reload the page or click another top40 link it takes a really long time to load (Also crashes IIS)..

 

$dom = new DomDocument;
$shortc = $country;// (GB or US)?
$dom->load('(URL address blocked: See forum rules)=100/explicit=true/xml');

$feed = array();
$feed1 = array();
$feed2 = array();
foreach ($dom->getElementsByTagName('entry') as $node) {
   $item = array (
       'img' => $node->getElementsByTagName('image')->item(2)->nodeValue,
       'artist' => $node->getElementsByTagName('artist')->item(0)->nodeValue,
       'title' => $node->getElementsByTagName('name')->item(0)->nodeValue,
       'album' => $node->getElementsByTagName('collection')->item(0)->nodeValue,
       'itunes' => $node->getElementsByTagName('id')->item(0)->nodeValue,
       );
   array_push($feed, $item);
}

$params = $dom->getElementsByTagName('category');

foreach ($params as $param) {
   $item1 = array (
       'genre' => $param->getAttribute('label')
       );
   array_push($feed1, $item1);
}    

$params1 = $dom->getElementsByTagName('id');

foreach ($params1 as $param1) {    
   $item2 = array (
       'id' => $param1->getAttribute('im:id')
       );
   array_push($feed2, $item2);
}
$limit = 100;


$counter = $starting + 1;
$i = 0;
$y = 1;
$pos = 1;
$color = 1;

for($x=0;$x<$limit;$x++)
{

   $rand = rand(000000,999900);
   $tracktitle = str_replace("'", "'", $feed[$x]['title']);
   $musicartist = str_replace("'", "'", $feed[$x]['artist']);

   $querydo = mysql_query("SELECT * FROM music WHERE musictitle = '".$tracktitle."' AND musicartist = '".$musicartist."' LIMIT 0,1") or die(mysql_error());
   $querydo1 = mysql_query("SELECT * FROM music WHERE musictitle = '".$tracktitle."' AND musicartist = '".$musicartist."' LIMIT 0,1") or die(mysql_error());
   $qu = mysql_fetch_array($querydo);
   $isOn = mysql_num_rows($querydo1);
   $totalMusic++;

   if($color==1){
       $color="2";
       $col="#fffafa";
   }else{
       $color="1";
       $col="#f3f3f3";
   }

   $id = $feed2[$y]['id'];
   $y++;

   $musicid = $qu['musicid'];
   $aart = $feed[$x]['img'];
   $preview = $item3['preview'];
   $views = $qu['views'];
   if($isOn==1){ $url = "player.php?id=".$musicid."&v=".$musicid; }
   $itunes = $feed[$x]['itunes'];
   $genre = $feed1[$x]['genre'];
   $title = strlen(cleanmusic($feed[$x]['title'])) > 70 ? substr(cleanmusic($feed[$x]['title']), 0, 70).'..' : cleanmusic($feed[$x]['title']);
   $artist = strlen(cleanmusic($feed[$x]['artist'])) > 50 ? substr(cleanmusic($feed[$x]['artist']), 0, 52).'..' : cleanmusic($feed[$x]['artist']);
   $album = strlen(cleanmusic($feed[$x]['album'])) > 50 ? substr(cleanmusic($feed[$x]['album']), 0, 50).'..' : cleanmusic($feed[$x]['album']);
   if($qu['itunes']=="No iTunes Link Specified" || $r['itunes']=="0"){ $buyitunes = ""; }else{ $buyitunes = "<a href='".$itunes."' target='_new'><img src='img/ButtoniTunes.png' class='imgopt'></a>"; }
   if($isOn==1){ $share = "<a href='#' onclick='ShareBox(\"".$qu['musicid']."\");'><img src='img/ButtonShare.png' class='imgopt'></a>"; }else{ $share = ""; }

   echo "<tr class='songlist' onmouseover='$(\"#".$rand."options\").show();' onmouseout='$(\"#".$rand."options\").hide();' style='background-color:".$col.";'><td width='50px' align='center'><img src='".$aart."' style='background-color:#FFFFFF;height:32px;width:32px;padding:3px;border:1px solid #e5e5e5;'></td><td width='20px' align='center'>"; if($isOn==1){ echo "<a href='#' onclick='"; infodisplay($musicid); echo "parent.document.getElementById(\"playerframe\").src = \"".$url."\"; return false;'><img src='img/ButtonPlay.png' class='imgopt'></a>"; }else{ echo "<a href='#' onclick='"; infodisplayyt($title, $artist, $aart); echo "parent.document.getElementById(\"playerframe\").src = \"player.php?yt=".$title."-".$artist."&aart=".$aart."\"; return false;'><img src='img/ButtonYTS.png'></a>"; } echo "</td><td class='songlist'>";

   if($isOn==1) {
       echo "<div style='float:left;'><a href='albums.php?id=".$musicid."' title='".$title."'><span class='songlist_title'>".$title."</span></a> - <a href='artists.php?query=".$artist."' title='".$artist."'><span class='songlist_title'>".$artist."</span></a><br><a href='albums.php?id=".$musicid."' title='".$album."'><span class='songlist_artist'>".$album."</span></a>, <span class='songlist_artist'>".$genre."</span></div><div style='float:right;font-size:20px;'><b>".$pos."</b></div>";
   }else{
       echo "<div style='float:left;'><span class='songlist_disabled'>".$title."</span> - <a href='artists.php?query=".$artist."' title='".$artist."'><span class='songlist_disabled'>".$artist."</span></a><br><span class='songlist_artist_disabled'><a href='albums.php?id=".$album."' title='".$album."'><span class='songlist_artist_disabled'>".$album."</span></a>, <span class='songlist_artist_disabled'>".$genre."</span></div><div style='float:right;font-size:20px;'><b>".$pos."</b></div>";
   }
}

I have no idea if its because I'm doing 3 for each and adding to the array but I can't figure out how to do just 1 foreach and get the data from those attributes, if anyone could help me solve this problem it would be great! - PS I also have another DOM request type script that does the same thing.. (With lastfm) so I know its not itunes.. Please help :)

Unfortunately, without profiling your code it's almost impossible to say why it takes so long. Especially since I don't know what the custom functions in it does, and the lack of a clear commenting/style isn't exactly helping.

I do recommend that you run it through a proper profiler XHprof and Zend Studio are the only two (worthwhile) profilers I know about, might be others but I haven't been able to find them. For the former you have two implementations which are worth looking at: XHProf.io and preinheimer-XHPRof.

 

Once you've profiled your code, you'll have a lot more information on what exactly is taking time. ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.