Best way to get data from others services

时间:2017-04-06 16:49:24

标签: php

I have array, for example:

$links = array(
     'http://aaa.com/data.txt',
     'http://aaea.com/data.txt',
     'http://aada.com/data.txt',
     'http://agaa.com/data.txt',
     'http://ahaa.com/data.txt',
     'http://awha.com/data.txt',
     'http://aaeha.com/data.txt',
     //etc x100
);

And in PHP I am doing:

foreach ($links as $link) {
    $data = file_get_contents($link);
    //save data in database
}

It works fine, but terribly slow. How is better way for this with PHP? I would like get data asynchronous.

My other way - jQuery and Ajax queries from PHP script, but maybe exists better way?

1 个答案:

答案 0 :(得分:0)

我建议这样做。

<?php
$Links = array(
  'http://aaa.com/data.txt',
  'http://aaea.com/data.txt',
  'http://aada.com/data.txt',
  'http://agaa.com/data.txt',
  'http://ahaa.com/data.txt',
  'http://awha.com/data.txt',
  'http://aaeha.com/data.txt'
);
$TempData = '';
foreach ($Links as $Link) {
  $TempData .= file_get_contents($Link);
  $TempData .= '|';
}
$Data = rtrim($TempData, '|');

// save the $Data string and when you export the
// string from the db use this code to turn it into an array
//
// $Data = explode('|' $ExportedData);
// var_dump($Data);
//
// If you do it this way you will be preforming 1 sql
// statement instead of multiple saving site resources
// and making you code execute faster

?>

如果这有助于我知道。