I’m having a problem with some PHP code I’m trying to use. I keep getting an “unexpected T_STRING” error? This is the code on my test page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php
include 'http://www.weather.gov/xml/current_obs/KTPA.xml';
$current_observation = new SimpleXMLElement($xmlstr);
echo $current_observation->location[0]->plot;
echo $current_observation->weather[0]->plot;
echo $current_observation->temperature_string[0]->plot;
echo $current_observation->wind_string[0]->plot;
?>
What I’m wanting to do is pull weather data from this National Weather Service XML file, and display it in a way similar to how the NWS is doing it on that page. However, the above code returns this message: “Parse error: syntax error, unexpected T_STRING in http://www.weather.gov/xml/current_obs/KTPA.xml on line 1″. Is this something I can fix? I’m new to PHP, so any help you can provide will be appreciated.
First of all this script should be run before the html and doctype tags, you did not finish your html code with head and body Those should always be the basic for your html webpage.
Second of all where does $xmlstr points to, you have never created the variable $xmlstr, and the webpage you include is xml so that one doesn’t do it either.
If you are using your own set up webserver you could enable cUrl where you can parse the xml sourcecode, saving it into variables and showing it step by step using an array and a for-loop.
You would save the parsed data in an array like:
with cUrl you get an output which you can save in a variable, lets say $output.
from there on you would have to echo your way parsing step by step, so first you would do echo $output; so you know what the source actually looks like when using cUrl, next step is to find the specific data in that url and parse your way to that point using explode (creates arrays) and substrings, and you can save those into an array like:
$city[$cityName][$temp] or someting like that