DOMDocument Help Needed

Discussion in 'PHP' started by xadet3, Oct 25, 2007.

  1. #1
    Okay so basically this script will print an image with the stats retrieved from the XML file at http://75.126.211.99/player2xml.php?n=(playersname).

    Problem is, if the name is wrong the XML file will return just "Error". And that makes my script go crazy.

    Working example: http://stats.aruaguru.com/stats.php?q=xadet3
    Non-working example: http://stats.aruaguru.com/stats.php?q=thisnamedoesntexist

    How can I make it so it doesn't do anything if the Character tag doesn't exist?

    if($_GET["q"] != NULL)
    {
    	create_image();
    	exit();
    }
    else
    {
    	echo "Error.";
    }
    
    function create_image()
    {
    	$q=$_GET["q"];
    
    	$xmlDoc = new DOMDocument();
    	$xmlDoc->load("http://75.126.211.99/player2xml.php?n=".$q."");
    
    	$x=$xmlDoc->getElementsByTagName('Character');
    
    	$font = 'PORKYS.TTF';
    
    	$image = imagecreatefrompng('pstats.png');  
    
    	$colour = ImageColorAllocate($image, 229, 229, 229);
    
    	$y=$x->item($i)->getElementsByTagName('Level');
    	$z=$x->item($i)->getElementsByTagName('Job');
       
    	$name = $q;
    
    	$class = $z->item(0)->childNodes->item(0)->nodeValue;
    
    	$level = $y->item(0)->childNodes->item(0)->nodeValue;
    
    	imagettftext($image, 8, 0, 110, 33, $colour, $font, $name);
    	
    	imagettftext($image, 8, 0, 110, 53, $colour, $font, $class);
    
    	imagettftext($image, 8, 0, 110, 72, $colour, $font, $level);
    
    	header("Content-Type: image/png"); 
    
    	Imagepng($image);
       
    	ImageDestroy($image);
    
    }
    PHP:
     
    xadet3, Oct 25, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Try:
    
    if (@$xmlDoc->load("http://75.126.211.99/player2xml.php?n=".$q.""))
    {
    	$class = $z->item(0)->childNodes->item(0)->nodeValue;
    	$level = $y->item(0)->childNodes->item(0)->nodeValue;
    }
    else
    {
    	$class = 'default';
    	$level = 'default';
    }
    
    PHP:
     
    nico_swd, Oct 25, 2007 IP
  3. xadet3

    xadet3 Peon

    Messages:
    565
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Still giving errors for nicks that don't exist. Thanks anyway.
     
    xadet3, Oct 25, 2007 IP