dqsa17330 2016-05-25 20:07
浏览 129
已采纳

file_get_contents - “使用未定义的常量代码 - 假设'代码'”

I'm trying to add a basic new-grabber for a site I'm making and cannot figure out for the life of me what is causing this error. The file I'm grabbing is a plain-text file, completely accessible.

I've seen it posted before and the OP is calling something like: $var = $data[str] instead of $var = $data['src']

But I am not calling anything with "code" in the name. I receive this error upon running my code:

HTTP request failed. Error 8 on line 123: Use of undefined constant code - assumed 'code' in file /usr/local/lib/php/head.php

Here is my entire file below:

<?
    $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>";
    }
?>

Do you guys know what I'm missing or doing wrong here?

  • 写回答

1条回答 默认 最新

  • dsd119120 2016-05-26 13:45
    关注

    As already suggested by @John Stirling, "the issue is elsewhere".

    More precisely the reported Error 8 on line 123... etc is related to an error that happened previously, elsewhere.

    And your current code is responsible to make this error appear now because you wrote:

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

    This way, the following happens:

    • Each time file_get_contents() is successfull, $e_news gets its content.
    • Then $e_news === true is FALSE (even if this content is empty, because you used ===), and if (!$e_news === true) is always TRUE.
    • So there is no error now, and your error_get_last() gets the trace of the last error that previously happened, elsewhere...

    In fact, for your code to work as expected you should rather do something like this:

    $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>
    <? 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动