&引用;意外的T“U字符串”;PHP中的错误?

&引用;意外的T“U字符串”;PHP中的错误?,php,Php,我尝试使用的一些PHP代码有问题。我一直收到“意外的T_字符串”错误?这是我的测试页面上的代码: <!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.wea

我尝试使用的一些PHP代码有问题。我一直收到“意外的T_字符串”错误?这是我的测试页面上的代码:

   <!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;

    ?>

位置[0]->绘图;
echo$current_observation->weather[0]->绘图;
echo$current\u observation->temperature\u string[0]->绘图;
echo$current\u observation->wind\u string[0]->绘图;
?>

我想做的是从这个国家气象服务XML文件中提取天气数据,并以类似NWS在该页面上所做的方式显示它。但是,上面的代码返回以下消息:“Parse error:syntax error,第1行中出现意外的T_字符串”。这是我能解决的问题吗?我是PHP新手,因此如果您能提供任何帮助,我们将不胜感激。

当您在PHP源代码中包含任何非法引用的文本字符串时,您将收到一个T_字符串错误

可能原因:

不能在PHP源文件中“包含”非PHP文本

解决方案:

使用PHP“fopen()”(或等效工具)读取和解析您感兴趣的文件

更好的方法是,如果文件是XML,则使用PHP XML对其进行解析:


包含函数用于包含其他php脚本。要获取xml文件的内容,必须使用
file\u get\u 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;

不确定您是否理解包含的目的。要获取文件的内容,请使用。因此:

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

首先,这个脚本应该在html和doctype标记之前运行,你没有用头和体来完成你的html代码,这些应该是你的html网页的基础

其次,$xmlstr指向何处,您从未创建过变量$xmlstr,并且您包含的网页是xml,因此您也不会这样做。 如果您使用自己的setupwebserver,那么可以启用cUrl,您可以在其中解析xml源代码,将其保存到变量中,并使用数组和for循环逐步显示它。 将解析后的数据保存在如下数组中:

使用cUrl,您可以得到一个可以保存在变量中的输出,比如$output。 从那时起,您必须一步一步地回显您的解析方式,因此首先要执行echo$output;因此,您知道使用cUrl时源代码的实际外观,下一步是在该url中查找特定数据,并使用explode(创建数组)和子字符串解析到该点,您可以将这些数据保存到如下数组中:
$city[$cityName][$temp]或类似的东西

我会尝试为您想要解析的变量分配一些东西,因为仅仅包含文件是不起作用的,因为它不是一个可以解释并包含在输出中的PHP脚本

<!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;

    ?>

位置[0]->绘图;
echo$current_observation->weather[0]->绘图;
echo$current\u observation->temperature\u string[0]->绘图;
echo$current\u observation->wind\u string[0]->绘图;
?>

我只是在使用这里提供的示例代码:您不需要将XML文件保存到变量中,最好的做法是确实使用file\u gets\u content({URL here});另一个选项是如上所述的cUrl。这是你第一次创建网站吗?我有一个网站,www.suncaststormwatch.com,但我从未使用过PHP;在你的css中。php确实有点难,我建议您从一些基础知识开始。查看php文档中的file\u get\u内容:这是cUrl的较短、更简单的版本,应该是这样的:$xmlstr=file\u get\u content(“);删除include行,并将此脚本放在doctype上方,您可以临时删除html,因为您只回显php内容而不回显html。如果要向其中添加html内容,应使其看起来像: