tarod Posted September 3, 2008 Share Posted September 3, 2008 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 More sharing options...
discomatt Posted September 3, 2008 Share Posted September 3, 2008 And the error you're getting is? Link to comment https://forums.phpfreaks.com/topic/122588-solved-xml_parse-error-in-512/#findComment-632971 Share on other sites More sharing options...
Mchl Posted September 3, 2008 Share Posted September 3, 2008 Check this part of manual as there are several changes between PHP4 and PHP5 that could cripple your script. Link to comment https://forums.phpfreaks.com/topic/122588-solved-xml_parse-error-in-512/#findComment-632981 Share on other sites More sharing options...
tarod Posted September 3, 2008 Author Share Posted September 3, 2008 The error message this script displays is "XML error: Invalid document end at line 1." Link to comment https://forums.phpfreaks.com/topic/122588-solved-xml_parse-error-in-512/#findComment-632986 Share on other sites More sharing options...
tarod Posted September 3, 2008 Author Share Posted September 3, 2008 Mchl, I've read through the link you provided but don't see any sections that directly relate to this code. Can you point out any specific functions that I should take a closer look at with this script? Link to comment https://forums.phpfreaks.com/topic/122588-solved-xml_parse-error-in-512/#findComment-633001 Share on other sites More sharing options...
DarkWater Posted September 3, 2008 Share Posted September 3, 2008 That seems awfully slow and convoluted...try using SimpleXML. Link to comment https://forums.phpfreaks.com/topic/122588-solved-xml_parse-error-in-512/#findComment-633022 Share on other sites More sharing options...
Mchl Posted September 3, 2008 Share Posted September 3, 2008 I can't point exact reason for this. This could be for example register_globals enabled on PHP4 and disabled on PHP5 or something along these lines... Link to comment https://forums.phpfreaks.com/topic/122588-solved-xml_parse-error-in-512/#findComment-633027 Share on other sites More sharing options...
tarod Posted September 4, 2008 Author Share Posted September 4, 2008 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); ?> Link to comment https://forums.phpfreaks.com/topic/122588-solved-xml_parse-error-in-512/#findComment-633875 Share on other sites More sharing options...
tarod Posted September 4, 2008 Author Share Posted September 4, 2008 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; } Link to comment https://forums.phpfreaks.com/topic/122588-solved-xml_parse-error-in-512/#findComment-633981 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.