PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
sigmorpho
Forum Newbie
Posts: 3 Joined: Mon Aug 29, 2005 4:45 pm
Location: New York
Post
by sigmorpho » Mon Aug 29, 2005 4:48 pm
I have this code below to parse a weather service feed. I get a unclosed token error. I'm wondering what I'm doing wrong. Any hlep would be greatly appreciated.
Code: Select all
<?php
$file = "http://xml.customweather.com/xml?client=havensclub_test&client_password=t3mp&product=expanded_forecast&id=70317&metric=true";
$depth = array();
$wd = array(); //weekday
$ht = array(); //high temp
$lt = array(); //low temp
$ic = array(); //icon
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}
$p = xml_parser_create();
while ($data = fread($fp, 8192)) {
if (!xml_parse_into_struct($p, $data, $vals, $index)) {
die(sprintf("XMLnew error: %s at line %d",
xml_error_string(xml_get_error_code($p)),
xml_get_current_line_number($p)));
}
xml_parser_free($p);
/*
echo "<br>";
echo "Index array:\n";
print_r($index);
echo "<br>";
echo "\nVals array:\n";
print_r($vals);
echo "<br>";
*/
$wd[0] = $vals[2]["attributes"]["WEEKDAY"];
$wd[1] = $vals[4]["attributes"]["WEEKDAY"];
$wd[2] = $vals[6]["attributes"]["WEEKDAY"];
$ht[0] = round($vals[2]["attributes"]["HIGH_TEMP"]);
$ht[1] = round($vals[4]["attributes"]["HIGH_TEMP"]);
$ht[2] = round($vals[6]["attributes"]["HIGH_TEMP"]);
$lt[0] = round($vals[2]["attributes"]["LOW_TEMP"]);
$lt[1] = round($vals[4]["attributes"]["LOW_TEMP"]);
$lt[2] = round($vals[6]["attributes"]["LOW_TEMP"]);
$ic[0] = "images/weather/" . $vals[2]["attributes"]["ICON"] . ".gif";
$ic[1] = "images/weather/" . $vals[4]["attributes"]["ICON"] . ".gif";
$ic[2] = "images/weather/" . $vals[6]["attributes"]["ICON"] . ".gif";
}
xml_parser_free($xml_parser);
?>
feyd | Please use Code: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Joe
Forum Regular
Posts: 939 Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow
Post
by Joe » Tue Aug 30, 2005 7:51 am
What line is the error on?. It should tell you...
sigmorpho
Forum Newbie
Posts: 3 Joined: Mon Aug 29, 2005 4:45 pm
Location: New York
Post
by sigmorpho » Tue Aug 30, 2005 12:33 pm
This is the exact error
'XMLnew error: unclosed token at line 12'
raghavan20
DevNet Resident
Posts: 1451 Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:
Post
by raghavan20 » Tue Aug 30, 2005 3:29 pm
Code: Select all
<?xml version="1.0" encoding="ISO-8859-1" ?>
- <report gmt_date="1808302005" unix_date="1125425137.000000" timezone="-6" state="" state_name="" country="MX" country_name="Mexico" localtime="Tue, 30 Aug 2005 14:15:40 CST" localupdatetime="Tue, 30 Aug 2005 12:05:37 CST" metric="true">
- <location city="70317" city_name="Puerto Vallarta" latitude="20.61" longitude="-105.22" state="">
<forecast day_sequence="1" day_of_week="4" weekday="Wednesday" daylight="D" date="083105" iso8601="2005-08-31T12:00:00.00-06:00" high_temp="28.68" low_temp="25.18" sky_desc="16" sky="Mostly cloudy" precip_desc="20" precip="Thunderstorms" temp_desc="10" temp="Warm" air_desc="33" air="Humid" description="Thunderstorms. Mostly cloudy. Warm." uv_index="3" uv="Low" wind_speed="4.08" wind_dir="230" wind_short="SW" wind_long="Southwest" humidity="81" dew_point="23.15" comfort="33.87" rainfall="2.20" snowfall="*" precip_prob="60" icon="22" icon_name="tstorms" beaufort="1" beaufort_desc="Light air" />
These are the first 12 lines of the XML code.
I think it does not understand <report .................
/>
may be it expects </report> itself
try a new xml file with just one report tag with </report> and read it thru your php code.
lets see wot happens.
sigmorpho
Forum Newbie
Posts: 3 Joined: Mon Aug 29, 2005 4:45 pm
Location: New York
Post
by sigmorpho » Tue Aug 30, 2005 4:00 pm
I copied the feed to a local xml file and it works fine. It seems to be something in my code and not the xml feed.