【问题标题】:Fatal error: Cannot redeclare connectt() (previously declared in致命错误:无法重新声明 connectt()(之前在
【发布时间】:2018-06-05 17:12:41
【问题描述】:

我尝试了一些帖子,但没有任何东西可以解决我的错误,有人可以看到我哪里出错了吗? 它显示:致命错误:无法重新声明 connectt()(之前在

中声明
<?php
$jsonContents = file_get_contents('data/data.json');
$data_array = json_decode($jsonContents, true);
foreach($data_array as $key=> $value) {
    $domain = $value['domain'];
    function connectt($domain, $port, $timeOut = 5)
    {
        $fp = fsockopen($domain, $port, $errno, $errstr, $timeOut);
        if (!$fp) {
            $jsonContents = file_get_contents('data/data.json');
            $data_array = json_decode($jsonContents, true);
            foreach ($data_array as $key => $value) {
                $domain = $value['domain'];
                $data_array = json_decode($jsonContents, true);
                $jsonContents = file_get_contents('data/data.json');
                if ($value['domain'] == $domain) {
                    $data_array[$key]['ssl'] = "Not Valid";
                }
            };
            $json = json_encode($data_array);
            file_put_contents('data/data.json', $json);
        } else {
            $jsonContents = file_get_contents('data/data.json');
            $data_array = json_decode($jsonContents, true);
            foreach ($data_array as $key => $value) {
                $domain = $value['domain'];
                $jsonContents = file_get_contents('data/data.json');
                $data_array = json_decode($jsonContents, true);
                if ($value['domain'] == $domain) {
                    $data_array[$key]['ssl'] = "Valid";
                }
                $json = json_encode($data_array);
                file_put_contents('data/data.json', $json);
            }
        }
    }
    $port = 443;
    connectt($domain, $port);
}

【问题讨论】:

  • 函数声明应该在foreach循环之外

标签: php


【解决方案1】:

请从 foreach 循环中删除 connectt() 函数。这是你应该工作的最终代码

   <?php
   $jsonContents = file_get_contents('data/data.json');
   $data_array = json_decode($jsonContents, true);

       function connectt($domain, $port, $timeOut = 5)
       {
           $fp = fsockopen($domain, $port, $errno, $errstr, $timeOut);
           if (!$fp) {
               $jsonContents = file_get_contents('data/data.json');
               $data_array = json_decode($jsonContents, true);
               foreach ($data_array as $key => $value) {
                   $domain = $value['domain'];
                   $data_array = json_decode($jsonContents, true);
                   $jsonContents = file_get_contents('data/data.json');
                   if ($value['domain'] == $domain) {
                       $data_array[$key]['ssl'] = "Not Valid";
                   }
               };
               $json = json_encode($data_array);
               file_put_contents('data/data.json', $json);
           } else {
               $jsonContents = file_get_contents('data/data.json');
               $data_array = json_decode($jsonContents, true);
               foreach ($data_array as $key => $value) {
                   $domain = $value['domain'];
                   $jsonContents = file_get_contents('data/data.json');
                   $data_array = json_decode($jsonContents, true);
                   if ($value['domain'] == $domain) {
                       $data_array[$key]['ssl'] = "Valid";
                   }
                   $json = json_encode($data_array);
                   file_put_contents('data/data.json', $json);
               }
           }
       }
   foreach($data_array as $key=> $value) {
       $domain = $value['domain'];
       $port = 443;
       connectt($domain, $port);
   }

【讨论】:

  • 嗯,它工作了一半,但有一个重要错误,当我把“有效选项”所有“无效”选项更改为有效时
  • 你能看出问题吗?
  • 这是因为您的函数中存在另一个问题。你想做什么,为什么每次都打电话给file_get_contents...
  • 哦,对了,这是一个复制粘贴错误,让你受够了
猜你喜欢
  • 2012-06-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 2010-12-29
  • 2015-08-29
  • 2012-06-18
  • 1970-01-01
相关资源
最近更新 更多