• 生成config文件到内存中


    <?php
    
    $site_code='property';
            $common = [ 'API_HOST_V3'=>'http://10.1.118.12:83/apiv3.json' ,'SITE_CODE'=>'CP'];
            $api_v3 = 'http://10.1.118.12:83/apiv3.json';
            $data = [['api'=>"config/lists", 'data'=>['site_code'=>' '.$site_code.' ']]];
            //$data = [['api'=>"appMemberAuth/lists", 'data'=>['SITE_CODE'=>'CP']]];
            $data_string = json_encode($data);
            $header = [
                'Content-Type: application/json',
                'Content-Length: ' . strlen($data_string)
            ];
            $ch = curl_init($api_v3);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_TIMEOUT,5);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
            $result = curl_exec($ch);
            $result = json_decode($result,true);
            $result_data = $result['data'][0]['data'];
            $result_lists = json_decode($result_data);
            $cfg_file_path = RUNTIME_PATH.'/config.php';
            if(!file_exists($cfg_file_path))
            {
                            
                file_put_contents($cfg_file_path, "<?php".PHP_EOL, FILE_APPEND);
                file_put_contents($cfg_file_path, "    return array(".PHP_EOL, FILE_APPEND);
                
                foreach ($result_lists->data->lists as $key=>$item)
                {
                    $config_str = "";
                    if($item->value==='true')
                    {
                        $config_str = $config_str . "'". $item->key ."'";
                        $config_str = $config_str . "=>  true ,".PHP_EOL;
                    }else if($item->value==='false')
                    {
                        $config_str = $config_str . "'". $item->key ."'";
                        $config_str = $config_str . "=>  false ,".PHP_EOL;
                    }else if($item->data_type==='array')
                    {
                        if(is_array(json_decode($item->value,true)))
                        {    
                            $config_str = $config_str . "'". $item->key ."'";
                            $config_str = $config_str . "=> [".PHP_EOL;
                           foreach(json_decode($item->value,true) as $k=>$v)
                           { 
                                    $config_str = $config_str . "'". $k ."'";
                                     if(is_array($v))
                                    {
                                        $config_str = $config_str . "=>[".PHP_EOL;
                                        foreach($v as $kk=>$vv)
                                        {
                                            $config_str = $config_str . "'". $kk ."'";
                                                
                                            $config_str = $config_str . "=>'" .$vv ."',".PHP_EOL;
                                        }
                                        $config_str = $config_str . "],".PHP_EOL;
                                    }else{
                                        $config_str = $config_str . "=>'" .$v ." ',".PHP_EOL;
                                    }    
                           }
                           $config_str = $config_str . "],".PHP_EOL;
                        }
                        $data_arr[]=$item->value;
    
                    }else if(is_numeric($item->value))
                    {
                        $config_str = $config_str . "'". $item->key ."'";
                        $config_str = $config_str . "=>  " .$item->value .",".PHP_EOL;
                    }else{
                        $config_str = $config_str . "'". $item->key ."'";
                        $config_str = $config_str . "=>  '" .$item->value ."',".PHP_EOL;
                    }
                    
                    file_put_contents($cfg_file_path, $config_str, FILE_APPEND);
                }
                
                file_put_contents($cfg_file_path, ");", FILE_APPEND);
            }
             return  array_merge(file_exists($cfg_file_path) ? require_once $cfg_file_path : [], $common);
  • 相关阅读:
    Eclipse Ctrl C卡的解决方法
    oracle函数 逗号分隔的字符串的每个字串两边加上单引号
    EXT自定义颜色表单域控件
    Ext JS开发经验累积
    mysql连接远程数据库
    Hibernate使用连接池
    oracle转mysql数据库 (hibernate+spring)
    解决html中img标签src中含中文出错的问题
    ckeditor自定义图片上传,结合EXT JS
    Web自动化框架搭建——前言
  • 原文地址:https://www.cnblogs.com/stj123/p/12354275.html
Copyright © 2020-2023  润新知