doubi1818
2017-09-15 22:40
浏览 91
已采纳

Laravel - 将.txt文件保存到mySQL数据库

I have been struggling to create something that can save txt files into a mySQL database. I have managed to create something that saves JSON files but not txt files.

Here is the txt file in question: http://celestrak.com/NORAD/elements/sbas.txt. This txt file contains a few satellites with their data. Each satellite has exactly three lines, no exceptions. So, for example here is one satellite:

AOR-E (EGNOS/PRN 120)   
1 24307U 96053A   17257.68868765 -.00000150  00000-0  00000-0 0  9992
2 24307   2.8040  77.2609 0004175 104.1816  44.8421  1.00271450 76939

The first lines tells us the satellite name. The next two lines give us some parameters, which always start with the numbers 1 and 2. This format will not change - the name on line 0 and the two lines after it, which start in 1 or 2.

What I want to be able to do is to create a row for each satellite - with the columns object_name for line 0, tle_line1 for line 1 and tle_line2 for line 2.

I have managed to create something that saves data from a JSON format into the SQL database. Maybe some can be deviated from that?

I am using Laravel and Guzzle for the HTTP requests:

$api = new Client([
    'base_uri' => 'http://celestrak.com',
]); 
$response = $api->get('jsonlocater');
$data = json_decode($response->getBody()->getContents(), true);
    foreach ($data as $attributes) {
    $attributes = array_change_key_case($attributes, CASE_LOWER);
    Satellites::create($attributes);
}

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

我一直在努力创建可以将txt文件保存到mySQL数据库的东西。 我设法创建了一些节省JSON文件而不是txt文件的东西。

以下是有问题的txt文件: http:/ /celestrak.com/NORAD/elements/sbas.txt 。 这个txt文件包含一些带有数据的卫星。 每颗卫星都有三条线,没有例外。 所以,例如这里有一颗卫星:

  AOR-E(EGNOS / PRN 120)
1 24307U 96053A 17257.68868765 -.00000150 00000-0 00000-0 0 9992 
2  24307 2.8040 77.2609 0004175 104.1816 44.8421 1.00271450 76939 
   
 
 

第一行告诉我们卫星名称。 接下来的两行给出了一些参数,这些参数总是以数字 1 2 开头。 此格式不会更改 - 行 0 上的名称以及后面的两行,它们以 1 2 开头。

我希望能够为每个卫星创建一行 - 使用 object_name 0 , 行 1 tle_line1 和行 2 tle_line2

我设法创建了一些将JSON格式的数据保存到SQL数据库中的东西。 也许有些人会偏离它?

我正在使用Laravel和Guzzle来处理HTTP请求:

  $ api = new Client([
'base_uri'=>  ;'http://celestrak.com',
]);  
 $ response = $ api-> get('jsonlocater'); 
 $ data = json_decode($ response-> getBody() - > getContents(),true); 
 foreach($ data as $ 属性){
 $ attributes = array_change_key_case($ attributes,CASE_LOWER); 
 Satellites :: create($ attributes); 
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duan0531 2017-09-15 22:58
    已采纳

    First of all, I'm not sure what is your response format but using vanilla PHP you may do something like the following to fetch the contents in array:

    $url = 'http://celestrak.com/NORAD/elements/sbas.txt';
    $lines = file($url, FILE_IGNORE_NEW_LINES);
    $arrays = array_map(function($array) {
        $columns = ['object_name', 'tle_line1', 'tle_line2'];
        return array_combine($columns, array_map('trim', $array));
    }, array_chunk($lines, 3));
    

    Now, if you dd($arrays) the result then you'll get something like the following:

    enter image description here

    From this result, you should be easily able to create entries in your database. Each array in image should be an entry/row in your database table. For example:

    \DB::table('table_name')->insert($arrays);
    

    Note that, if you've timestamps (created_at & updated_at) in your table then you've to add those fields in each array when generating the arrays.

    点赞 打赏 评论

相关推荐