Jump to content

[SOLVED] XML_parse error in 5.1.2


tarod

Recommended Posts

Thank you in advance for your help.  I'm having an issue with a script I have for XML parsing.  It runs on a server I have access to running PHP 4.4.1 but not where I need it to work on a server with 5.1.2.  I'm lost in finding why it doesn't work as the newer version should be doing a better job of handling the xml file.  Anyway, here's the code.  Any help would be greatly appreciated.

 

<?php

$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
$pic = "";
$width = "";
$height = "";
$popupurl = "";

function startElement($parser, $name, $attrs) {
global $insideitem, $tag, $title, $description, $link, $pic, $width, $height, $popupurl;
if ($insideitem) {
	$tag = $name;
	if ($tag == "MEDIA:CONTENT") {
		$pic = $attrs[url];
		$size = getimagesize($pic);
		$width = $size[0];
		$height = $size[1];
		if ($width > 250) {
			$ratio = 250/$width;
			$width = 250;
			$height = $height * $ratio; 
		}
	} elseif ($tag == "MEDIA-REFERENCE") {
		if ($attrs["CLASS"] == "slate") {
			$pic = $attrs[sOURCE];
			$size = getimagesize($pic);
			$width = $size[0];
			$height = $size[1];
			if ($width > 250) {
				$ratio = 250/$width;
				$width = 250;
				$height = $height * $ratio; 
			}
		}
		if ($attrs["CLASS"] == "popup-link") {
			$popupurl = $attrs[sOURCE];
		}
	}
} elseif ($name == "ITEM") {
	$insideitem = true;
}
}

function endElement($parser, $name) {
global $insideitem, $tag, $title, $description, $link, $pic, $width, $height, $popupurl;
if ($name == "ITEM") {
	printf("<tr><td><p><strong><a href='%s'>%s</a></strong></p>\n",
		trim($link),htmlspecialchars(trim($title)));
	if (strlen($popupurl) == 0) {
		printf("<p><a href=\"" . $link . "\"><img src=\"" . $pic . "\" alt=\"" . htmlspecialchars(trim($title)) . "\" border=\"0\" width=\"" . $width . "\" height=\"" . $height . "\" class=\"floatLeft\"></a>%s</p></td></tr>\n<tr><td><p> </p></td></tr>",trim($description)); 
	}
	else {
		printf("<p><a href=\"" . $popupurl. "\" target=\"_blank\" onclick=\"return popitup('" . $popupurl . "')\"><img src=\"" . $pic . "\" alt=\"" . htmlspecialchars(trim($title)) . "\" border=\"0\" width=\"" . $width . "\" height=\"" . $height . "\" class=\"floatLeft\"></a>%s</p></td></tr>\n<tr><td><p> </p></td></tr>",trim($description));
	}
	$title = "";
	$description = "";
	$link = "";
	$insideitem = false;
	$pic = "";
	$width = "";
	$height = "";
	$popupurl = "";
}
}

function characterData($parser, $data) {
global $insideitem, $tag, $title, $description, $link;
if ($insideitem) {
switch ($tag) {
	case "TITLE":
	$title .= $data;
	break;
	case "DESCRIPTION":
	$description .= $data;
	break;
	case "LINK":
	$link .= $data;
	break;
}
}
}

$xml_feed_url = "file location goes here";
$xml = @implode("",file($xml_feed_url));

$xml_parser = xml_parser_create();
xml_parser_set_option( $xml_parser, XML_OPTION_SKIP_WHITE, 1 );
xml_parser_set_option( $xml_parser, XML_OPTION_CASE_FOLDING, 1 );
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
xml_parse($xml_parser, $xml, TRUE)
	or die(sprintf("XML error: %s at line %d", 
		xml_error_string(xml_get_error_code($xml_parser)), 
		xml_get_current_line_number($xml_parser))); 
xml_parser_free($xml_parser);

?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/122588-solved-xml_parse-error-in-512/
Share on other sites

Ok, I found that the issue with loading the data was that the allow_url_fopen is set to off which means I can't open the remote file directly.  I've gone through and used cUrl to get the data in.  Now though, the getimagesize will not work as well because of the same permissions restriction.  I can't seem to get a second cUrl to get the image from the xml to determine the width and size.  Any suggestions?  Here's the code now.  I need the startElement() function corrected so that it returns the width and height from the img referenced in the xml without using fopen.

 

<?php

$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
$pic = "";
$width = "";
$height = "";
$popupurl = "";

function startElement($parser, $name, $attrs) {
global $insideitem, $tag, $title, $description, $link, $pic, $width, $height, $popupurl;
if ($insideitem) {
	$tag = $name;
	if ($tag == "MEDIA:CONTENT") {
		$pic = $attrs[url];
		$size = getimagesize($pic);
		$width = $size[0];
		$height = $size[1];
		if ($width > 250) {
			$ratio = 250/$width;
			$width = 250;
			$height = $height * $ratio; 
		}
	} elseif ($tag == "MEDIA-REFERENCE") {
		if ($attrs["CLASS"] == "slate") {
			$pic = $attrs[sOURCE];
			$size = getimagesize($pic);
			$width = $size[0];
			$height = $size[1];
			if ($width > 250) {
				$ratio = 250/$width;
				$width = 250;
				$height = $height * $ratio; 
			}
		}
		if ($attrs["CLASS"] == "popup-link") {
			$popupurl = $attrs[sOURCE];
		}
	}
} elseif ($name == "ITEM") {
	$insideitem = true;
}
}

function endElement($parser, $name) {
global $insideitem, $tag, $title, $description, $link, $pic, $width, $height, $popupurl;
if ($name == "ITEM") {
	printf("<tr><td><p><strong><a href='%s'>%s</a></strong></p>\n",
		trim($link),htmlspecialchars(trim($title)));
	if (strlen($popupurl) == 0) {
		printf("<p><a href=\"" . $link . "\"><img src=\"" . $pic . "\" alt=\"" . htmlspecialchars(trim($title)) . "\" border=\"0\" width=\"" . $width . "\" height=\"" . $height . "\" class=\"floatLeft\"></a>%s</p></td></tr>\n<tr><td><p> </p></td></tr>",trim($description)); 
	}
	else {
		printf("<p><a href=\"" . $popupurl. "\" target=\"_blank\" onclick=\"return popitup('" . $popupurl . "')\"><img src=\"" . $pic . "\" alt=\"" . htmlspecialchars(trim($title)) . "\" border=\"0\" width=\"" . $width . "\" height=\"" . $height . "\" class=\"floatLeft\"></a>%s</p></td></tr>\n<tr><td><p> </p></td></tr>",trim($description));
	}
	$title = "";
	$description = "";
	$link = "";
	$insideitem = false;
	$pic = "";
	$width = "";
	$height = "";
	$popupurl = "";
}
}

function characterData($parser, $data) {
global $insideitem, $tag, $title, $description, $link;
if ($insideitem) {
switch ($tag) {
	case "TITLE":
	$title .= $data;
	break;
	case "DESCRIPTION":
	$description .= $data;
	break;
	case "LINK":
	$link .= $data;
	break;
}
}
}

$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, "http://www.somesite.com/somefile.xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$curldata = curl_exec($ch);
curl_close($ch);

$xml = $curldata;

$xml_parser = xml_parser_create();
xml_parser_set_option( $xml_parser, XML_OPTION_SKIP_WHITE, 1 );
xml_parser_set_option( $xml_parser, XML_OPTION_CASE_FOLDING, 1 );
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
xml_parse($xml_parser, $xml, TRUE)
	or die(sprintf("XML error: %s at line %d", 
		xml_error_string(xml_get_error_code($xml_parser)), 
		xml_get_current_line_number($xml_parser)));
xml_parser_free($xml_parser);

?>

For any who are interested, I did solve this.  It basically involved creating a function to get the image size without using getimagesize and using cUrl instead.  Here's the code for the function:

 

function dh_getimagesize($url) {

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
$result = curl_exec($ch); 
$img = imagecreatefromstring($result);

$imagesize['width']= imagesx($img);
$imagesize['height']= imagesy($img);

imagedestroy($img);

return $imagesize;
}

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.