【问题标题】:file_get_contents - "Use of undefined constant code - assumed 'code'"file_get_contents - “使用未定义的常量代码 - 假定为‘代码’”
【发布时间】:2016-05-26 21:45:51
【问题描述】:

我正在尝试为我正在制作的网站添加一个基本的 new-grabber,但我终其一生都无法弄清楚导致此错误的原因。我抓取的文件是纯文本文件,完全可以访问。

我之前看过它发布过,并且 OP 正在调用类似的内容: $var = $data[str] 而不是 $var = $data['src']

但我并没有在名称中调用任何带有“代码”的东西。 我在运行代码时收到此错误:

HTTP 请求失败。第 123 行的错误 8:使用未定义的常量 代码 - 假定文件 /usr/local/lib/php/head.php 中的“代码”

下面是我的整个文件:

<?
    $e_news = file_get_contents("http://cinemattson.com/templates/flickfeed/news.txt");
    if (!$e_news === true) {
          $error = error_get_last();
          echo "HTTP request failed. Error " . $error['type'] . " on line " . $error['line'] . ": " . $error['message'] . " in file " . $error['file'] . "<br>";
    } else {
          echo "Everything went better than expected";
    }
    if ($e_news === true) { 
        $news = explode("|", $e_news);?>
        <h4>News &nbsp;&nbsp;&nbsp;&nbsp; - <? echo (!empty($news) ? $news[1] : "v0.0.1");?>&nbsp;&nbsp;<small><? echo (!empty($news) ? $news[0] : "5/22/2016");?></small></h4>
        <p><? echo (!empty($news) ? $news[2] : "Loading news failed, or there is currently no news.");?></p>
<? 
    } else {
        echo "<h4>News failed to load</h4>";
    }
?>

你们知道我在这里遗漏了什么或做错了什么吗?

【问题讨论】:

  • 问题在别处。
  • 还有一行和一个文件。你确定它们就是你给我们看的吗?
  • 执行print_r(error_get_last()); 找出导致它的文件和行号。
  • 警告 file_get_contents() 可能返回布尔值 FALSE,但也可能返回非布尔值,其计算结果为 FALSE。请阅读有关布尔值的部分以获取更多信息。使用 === 运算符测试此函数的返回值。 - php.net/file_get_contents
  • 我猜这是我的主机 000webhost 的问题,但我在另一个文件中对 vimeo.com 进行了类似的调用,这很好。

标签: php httprequest file-get-contents


【解决方案1】:

正如@John Stirling 已经建议的那样,“问题出在其他地方”。

更准确地说,报告的 Error 8 on line 123... etc 与之前在其他地方发生的错误有关。

你当前的代码有责任让这个错误现在出现,因为你写道:

$e_news = file_get_contents("http://cinemattson.com/templates/flickfeed/news.txt");
if (!$e_news === true) {
      $error = error_get_last();

这样,会发生以下情况:

  • 每次file_get_contents() 成功,$e_news 都会获取其内容。
  • 那么$e_news === true 为FALSE(即使此内容为空,因为您使用了===),而if (!$e_news === true) 始终为TRUE。
  • 所以现在没有错误,而您的error_get_last() 可以跟踪之前在其他地方发生的最后一个错误...

事实上,为了让您的代码按预期工作,您应该这样做:

$e_news = file_get_contents("http://cinemattson.com/templates/flickfeed/news.txt");
if ($e_news === false) {
    $error = error_get_last();
    echo "HTTP request failed. Error " . $error['type'] . " on line " . $error['line'] . ": " . $error['message'] . " in file " . $error['file'] . "<br>";
    echo "<h4>News failed to load</h4>";
} else {
    echo "Everything went better than expected";
    $news = explode("|", $e_news);?>
    <h4>News &nbsp;&nbsp;&nbsp;&nbsp; - <? echo (!empty($news) ? $news[1] : "v0.0.1");?>&nbsp;&nbsp;<small><? echo (!empty($news) ? $news[0] : "5/22/2016");?></small></h4>
    <p><? echo (!empty($news) ? $news[2] : "Loading news failed, or there is currently no news.");?></p>
<? 
}

【讨论】:

    猜你喜欢
    • 2015-07-11
    • 2014-05-07
    • 2014-04-30
    • 2013-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多