2011-09-13 4 views
0

로컬 서버에 PHP 스크립트를 작성하여 영화 제목 배열을 반복하고 http://www.imdbapi.com/을 사용하여 연결된 메타 데이터를 가져옵니다.PHP 스크립트를 최적화하여 max_execution_time 제한을 방지하십시오.

for ($i=0; $i < count($files['Film']); $i++) { 
    // get data from IMDB based on file title 
    $imdb_data = json_decode(file_get_contents('http://www.imdbapi.com/?t='.urlencode($files['Film'][$i]['title'])), true); 

    // save poster image 
    $img_name = trim(strtolower($imdb_data['Title'])).'.jpg'; 
    file_put_contents('img/posters/'.$img_name, file_get_contents($imdb_data['Poster'])); 

    // set film data 
    $files['Film'][$i]['year'] = $imdb_data['Year']; 
    $files['Film'][$i]['runtime'] = $imdb_data['Runtime']; 
    $files['Film'][$i]['rating'] = $imdb_data['Rating']; 
    $files['Film'][$i]['summary'] = $imdb_data['Plot']; 
    $files['Film'][$i]['image'] = $img_name; 
    $files['Film'][$i]['location_id'] = $this->data['Film']['location_id']; 
} 

나는 file_put_contents를 시작하는 라인에서 php max_execution_time을 기록했다. 몇 가지 이미지가 다운로드되고 불완전한 이미지가 표시되어 이미지를 다운로드 할 때 시간 제한에 도달했다고 생각합니다.

이를 방지하기 위해 스크립트를 개선하려면 어떻게해야합니까? 스크립트를 최적화 할 수있는 근본적인 것이 있다면 제한 시간을 늘리는 방법을 정말 좋아하지 않습니다.

+0

'ini_set ('max_execution_time', xxx)'는 루프 전에 한 번만 호출해야합니다. 스크립트 실행의 전체 시간 인'set_time_limit'을 설정할 수도 있습니다. 하지만 이것은 PHP가 안전 모드에서 실행될 때 작동하지 않습니다. – atma

+0

cli에서 실행하면 시간 제한이 없습니다. – ajreal

답변

0

이 대부분 프로젝트 좀 달 전에 일하고 있었다 (비동기 MySQL의에서 데이터를로드)에서 복사 붙여 넣기입니다. 나는 당신의 필요에 조금 그것을 바꿨고, 나는 당신에게 몇 가지 코멘트를 달았고, 새로운 파일 (getData.php)을 만들어 냈다.

나는 동영상을 다운로드하는 것과 관련해서는 자세히 설명하지 않았지만 이미지를 다운로드하고 업로드합니다. 거의 동일한 과정으로 원하는대로 조정할 수 있기를 바랍니다.

디렉토리에 두 파일을 넣고이 전나무 파일의 index.php

을 실행이

getData.php 두 번째 파일입니다

<html> 
    <head> 
    <script type="text/javascript"> 

    var request = false; 
    try { 
     request = new XMLHttpRequest(); 
    } catch (trymicrosoft) {       
     try { 
     request = new ActiveXObject("Msxml2.XMLHTTP"); 
     } catch (othermicrosoft) { 
     try { 
      request = new ActiveXObject("Microsoft.XMLHTTP"); 
     } catch (failed) {     
      request = false;  
     } 
     } 
    } 

    if (!request) 
     alert("Error initializing XMLHttpRequest!"); 
    </script> 

    <script type="text/javascript"> 
     function getData() 
     { 

     //inform the user for to wait a little bit 
     var txt=document.getElementById("messageBox") 
     txt.innerHTML="Please wait for the image to download..."; 

      //we will call asychronously getData.php 
      var url = "getData.php"; 
      var params = "";//no url parameters 

      request.open("POST", url, true);//use post for connect harder to attack, lots of data transfer 

      //Some http headers must be set along with any POST request. 
      request.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8"); 
      request.setRequestHeader("Content-length", params.length); 
      request.setRequestHeader("Connection", "close"); 

      request.onreadystatechange = updatePage; 
      request.send(params); 

     }//////////////////// 

     //You're looking for a status code of 200 which simply means okay. 
     function updatePage() { 
     if (request.readyState == 4) { 
      if (request.status == 200) 
      { 
       //getData.php sends as the image size, get it 
       var r=request.responseText.split("$");//$ is a custom data separator I use 

       //inform the user 
       var txt=document.getElementById("messageBox") 
       txt.innerHTML="The file was downloaded! Its size is:"+r[0]+"x"+r[1]+" the name is:"+r[2]; 

       /*r is a javascript array, 0=width, 1=height, 2=file name 
       */ 

       //display the image 
       document.getElementById("imageBox").style.width = r[0] + 'px'; 
       document.getElementById("imageBox").style.height = r[1] + 'px'; 
       document.getElementById("imageBox").style.backgroundImage = "url("+r[2]+")"; 

      } 
      else{ 
      //alert("status is " + request.status); 
      } 
     } 
     } 

    </script> 
    </head> 
    <body onload="getData();"> 

    <div id='messageBox'></div> 
    <div id='imageBox'></div> 

<font color=red> 
    You see I am under the image but I am already loaded cause 
    I don't have to wait for the image to load cause it loads asychronously. 
    Whenever the image is ready the xmlhttp mechanism will let the page 
    know and the associated javascript function will update the dom and will 
    update the page without reloading it! 
    </font> 
    </body> 
    </html> 

의 index.php

<?PHP //make a name for the file $file_name = rand(1,9999).'.jpg'; //download the file file_put_contents($file_name, file_get_contents('http://chachatelier.fr/programmation/images/mozodojo-original-image.jpg')); //get the size of the image $size = getimagesize($file_name); //send asycnhronously the width, height, and the name echo $size[0],'$',$size[1],'$',$file_name; ?> 
0

AJAX를 사용하여 약간의 시간 간격으로 각 항목을로드 해보십시오.

내부 PHP 용으로 사용하는 대신 자바 스크립트에서 interval-called 함수를 사용하면 PHP 호출의 반환 된 내용을로드 할 수 있습니다.

관련 문제