简体   繁体   中英

file_get_contents - “Use of undefined constant code - assumed 'code'”

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?

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>
<? 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM