PHP:file_get_contents():SSL操作失败,代码为1

时间:2020-06-15 17:39:46

标签: php api ssl

我最近开始用PHP编程,并开始了我的新个人项目,这是一个基于PHP的新闻网站,它围绕特定主题生成头条新闻。我正在使用PHP 7.4.4,Bootstrap和一点CSS。一切进展顺利,解决了所有错误,通过搜索回答了任何问题。至少直到我真正开始将实际新闻插入我的网站为止。当我尝试使用api密钥(来自https://newsapi.org的API密钥)将实时新闻插入“体育新闻”页面时,页面顶部显示错误:

file_get_contents():SSL操作失败,代码为1。OpenSSL错误消息:error:14095126:SSL例程:ssl3_read_n:在/opt/lampp/htdocs/news/apps/views/news/sportNews.php中读取时出现意外的eof在第18行

该消息确实显示在我的屏幕上,但是即使反复尝试修复该错误,该错误仍然不会消失。

这是我的sportNews.php:

<?php $this->setSiteTitle('News'); ?>
<?php $this->start('body'); ?>
<head>
  <link href="css/newscss.css" rel="stylesheet"></link>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
</head>
<body>
  <?php
    //API_KEY filled in when running program
    $url = "https://newsapi.org/v2/everything?q=sports&apiKey=API_KEY";
    $response = file_get_contents($url);  //ERROR LINE
    $NewsData = json_decode($response);
  ?>
  <div class="jumbotron">
    <h1>Sports News</h1>
  </div>
  <div class="container-fluid">
    <?php
    foreach($NewsData->articles as $News)
    {
    ?>
    <div class="row">
      <div class="col-md-3">
        <img src="<?php echo $News->urlToImage ?>" alt="News thumbnail">
      </div>
      <div class="col-md-9">
        <h2>Title: <?php echo $News->title ?></h2>
        <h5>Description: <?php echo $News->description ?></h5>
        <p>Preview/Content: <?php echo $News->content ?></p>
        <h6>Author: <?php echo $News->author ?></h6>
        <h6>Published <?php echo $News->publishedAt ?></h6>
      </div>
      <?php
      }
      ?>
    </div>
  </div>
</body>
<?php $this->end(); ?>

我已经尝试过多次修复此错误,包括测试类似的堆栈溢出问题的答案,但都没有结果。经过彻底搜索,但仍然没有结果,我决定提一个问题。任何帮助表示赞赏

1 个答案:

答案 0 :(得分:1)

这种情况发生在您的客户无法建立版本时。当您使用较旧的PHP版本和较新的openssl版本时,会发生该错误。在较新的版本中,您需要进行一些制动更改,并且会收到该错误。

https://github.com/dask/gcsfs/issues/246

您可以尝试降级或升级您的openssl版本。该错误应该出现在1.1.1e版本中。因此,请首先检查您的openssl版本,看看是否可以升级它。

OpenSSL.SSL.Error: [('SSL routines', 'ssl3_read_n', 'unexpected eof while reading')] when updating / searching / installing conda packages

这是yum的另一个有用的帖子。