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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作