Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Unexpected T_STRING" error in my PHP?

Tags:

php

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.

like image 721
Matt Avatar asked Jul 11 '26 05:07

Matt


2 Answers

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;
like image 190
driangle Avatar answered Jul 13 '26 20:07

driangle


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');
like image 37
Alex Turpin Avatar answered Jul 13 '26 20:07

Alex Turpin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!