【问题标题】:"Unexpected T_STRING" error in my PHP?我的 PHP 中出现“意外的 T_STRING”错误?
【发布时间】:2012-02-04 03:28:08
【问题描述】:

我尝试使用的一些 PHP 代码有问题。我不断收到“意外的 T_STRING”错误?这是我的测试页面上的代码:

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

    ?>

我想做的是从这个 National Weather Service XML 文件中提取天气数据,并以类似于 NWS 在该页面上的方式显示它。但是,上面的代码返回此消息:“解析错误:语法错误,第 1 行 http://www.weather.gov/xml/current_obs/KTPA.xml 中的意外 T_STRING”。这是我可以解决的问题吗?我是 PHP 新手,如果您能提供任何帮助,我们将不胜感激。

【问题讨论】:

    标签: php


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      不确定您是否了解include 的用途。要获取文件的内容,请使用file_get_contents。因此:

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

      【讨论】:

        【解决方案3】:

        首先这个脚本应该在 html 和 doctype 标签之前运行,你没有用 head 和 body 完成你的 html 代码这些应该始终是你的 html 网页的基础。

        其次,$xmlstr 指向哪里,你从来没有创建过变量 $xmlstr,而且你包含的网页是 xml,所以你也不会这样做。 如果您使用自己设置的网络服务器,您可以启用 cUrl,您可以在其中解析 xml 源代码,将其保存到变量中并使用数组和 for 循环逐步显示它。 您可以将解析后的数据保存在一个数组中,例如:

        使用 cUrl 你可以得到一个输出,你可以将它保存在一个变量中,比如说 $output。 从那里开始,您将不得不逐步回显您的解析方式,因此首先您将执行 echo $output;所以你知道使用 cUrl 时源的实际样子,下一步是在该 url 中找到特定数据并使用explode(创建数组)和子字符串解析你的方式,你可以将它们保存到一个数组中,如: $city[$cityName][$temp] 或类似的东西

        【讨论】:

        • 我只是使用这里提供的示例代码:us2.php.net/manual/en/simplexml.examples-basic.php
        • 您不要将 XML 文件保存到变量中,最好的办法确实是 file_gets_content({URL HERE});另一个选项是如上所述的 cUrl。这是你第一次做网站吗
        • 我有一个网站 www.suncoaststormwatch.com,但我从来没有用 PHP 做过任何事情。
        • 快速评论您的网页设置溢出:隐藏;在你的 CSS 中。 php 确实有点难,我建议你从一些基础开始。在 php 文档中查找 file_get_contents:php.net/manual/en/function.file-get-contents.php 这是 cUrl 的更短更简单的版本,应该如下所示:$xmlstr = file_get_content("weather.gov/xml/current_obs/KTPA.xml");,删除包含行并放置此脚本在 doctype 上方,您可以临时删除 html,因为您只回显 php 内容而没有 html。
        • 如果你想添加 html 内容,你应该让它看起来像: weather.gov/xml/current_obs/KTPA.xml"); $current_observation=new SimpleXMLElement($xmlstr); ?> html> 天气 此处的一些 HTML 文本 location[0]->plot; echo $current_observation->天气[0]->绘图;回声 $current_observation->温度字符串[0]->绘图;回声 $current_observation->wind_string[0]->绘图;?>
        【解决方案4】:

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

        可能的原因:

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

        解决方案:

        使用 PHP "fopen()"(或等效的)来读取和解析您感兴趣的文件。

        更好,如果文件是 XML,那么使用 PHP XML 来解析它:

        【讨论】:

          【解决方案5】:

          我会尝试为您要解析的变量分配一些东西,因为仅仅包含该文件是行不通的,因为它不是一个可以解释并包含在输出中的 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;
          
              ?>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-05-17
            • 1970-01-01
            • 2023-03-20
            • 1970-01-01
            相关资源
            最近更新 更多