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.
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With