2017-04-06 3 views
0

다른 서비스에서 데이터를 얻을 수 있습니다 :가장 좋은 방법은 내가 예를 들어, 배열을

$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 
); 

그리고 PHP에서 내가 뭐하는 거지 : 그것은 잘하지만, 정말 느리게 작동

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

. 어떻게 PHP로 더 좋은 방법이 있습니까? 비동기 데이터를 얻고 싶습니다.

내 다른 방법 - PHP 스크립트에서 jQuery와 Ajax 쿼리가 더 좋은 방법 일 수 있습니까?

+0

PHP는 단일 스레드입니다 (일부 다중 스레드 라이브러리가 있음에도 불구하고). 따라서 스크립트 내에서 비동기 적으로 매우 쉽게 가져올 수 없습니다. 다른 옵션은 해당 데이터 파일을 캐싱하고 오래된 경우에만 새로 고칩니다 (URL에서 헤더를 가져 와서 datetime을 찾으려고 할 수 있음). – fbas

+1

시도해보십시오. http://php.net/manual/en/function.curl-multi-exec.php – LiTe

+0

도움이 될 것입니다. http://stackoverflow.com/questions/15559157/understanding-php-curl-multi- exec –

답변

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 

?> 

이 점이 도움이 되었다면.

+0

PHP가 여러 서버 쿼리가 아닌 원격 서버의 응답을 기다려야 할 때 병목 현상이 여러 file_get_contents를 호출하고 있다고 생각합니다. – LiTe