zoukankan      html  css  js  c++  java
  • 生成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);
  • 相关阅读:
    创建了对嵌入的互操作程序集间接引用,无法嵌入互操作类型
    演练:Office 编程(C# 和 Visual Basic)
    【MySQL案件】ERROR 1418
    Quartus II 11.0破发点(不同的是低版本号)
    Arduino 数码管LED驱动器 阵列方法
    HDU1312 Red and Black 解读
    UVA714- Copying Books(最大最小化)
    最大流量dinci模板
    Python 实现类似PHP的strip_tags功能,并能够定义他们自己的一套保留标记
    从源代码分析modelDriven拦截器和params拦截器和拦截器prepare 和paramsPrepareParamsStack拦截器栈(让你的Struts2代码更简洁——如何培养框架设计能力
  • 原文地址:https://www.cnblogs.com/stj123/p/12354275.html
Copyright © 2011-2022 走看看