我的PHP中出现“意外的T_STRING”错误?

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

You'll get a T_STRING error the moment you include any illegally quoted text strings in your PHP source.

PROBABLE CAUSE:

You can't "include" non-PHP text in a PHP source file.

SOLUTION:

Use PHP "fopen()" (or equivalent) to read and parse the file your interested in.

Better, if the file is XML, then use PHP XML to parse it:

The include function is used to include other php scripts. To get the contents of the xml file you'll have to use file_get_contents

$xmlstr =  file_get_contents('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;

Not sure you understand the purpose of include. To get the contents of a file, use file_get_contents. As such:

$xmlstr = file_gets_contents('http://www.weather.gov/xml/current_obs/KTPA.xml');

I would try to assign something to the variable you want to parse, because just inluding the file won't work as it's not a PHP script that can be interpreted as well and included in the output.

<!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
    $xmlstr = simplexml_load_file('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;

    ?>