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

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?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试为我正在制作的网站添加一个基本的新抓取工具,但无法弄清楚 我的生活是什么导致了这个错误。 我正在抓取的文件是一个纯文本文件,完全可以访问。

我之前看过它,OP调用的内容如下: $ var = $ data [str] 而不是 $ var = $ data ['src']

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

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

这里 是我的整个文件:

 &lt;?
 $ e_news = file_get_contents(“http://cinemattson.com/templates/flickfeed/news.txt”); \  n if(!$ e_news === true){
 $ error = error_get_last(); 
 echo“HTTP请求失败。错误”。  $ error ['type']。  “在线”。  $ error ['line']。  “:”。  $ error ['message']。  “在档案中”。  $ error ['file']。  “&lt; br&gt;”; 
}其他{
 echo“一切都比预期更好”; 
} 
 if($ e_news === true){
 $ news = explode(“|”,$  e_news);?&gt; 
&lt; h4&gt;新闻&amp; nbsp;&amp; nbsp;&amp; nbsp;&amp; nbsp;  - &lt;?  echo(!empty($ news)?$ news [1]:“v0.0.1”);?&gt;&amp; nbsp;&amp; nbsp;&lt; small&gt;&lt;?  echo(!empty($ news)?$ news [0]:“2016/5/22”);?&gt;&lt; / small&gt;&lt; / h4&gt; 
&lt; p&gt;&lt;?  echo(!empty($ news)?$ news [2]:“加载新闻失败,或者目前没有新闻。”);?&gt;&lt; / p&gt; 
&lt;?  
}否则{
 echo“&lt; h4&gt;新闻无法加载&lt; / h4&gt;”; 
} 
?&gt; 
   
 
 

你们 伙计们知道我在这里缺少什么或做错了吗?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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>
    <? 
    }
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题