2012-05-04 6 views
0

많은 URL에서 컬을 사용하여 내용을 검색하는 페이지가 있습니다. 검색된 특정 수의 URL을 찾은 후 컬이 null을 반환한다는 것을 발견했습니다. 특정 세션에서 말아 올릴 자원에 대한 제한이 있습니까?PHP 컬에 대한 리소스 제한

콘텐츠를 가져온 후 각 URL을 검색하기 전에 curl_init()을 검색하고 curl_close()을 검색합니다. 나는 이것이 자원을 즉시 풀어줄 것이 한계에 도달하는 상황을 막을 것이라고 생각했다. curl_init() 번을 너무 자주 호출하는 데 문제가 있습니까?

다음은 URL을 검색하는 PHP 코드 샘플입니다.

<?php 
    function get_curl() { 
     // Initialize a new curl resource 
     $ch = curl_init(); 

     // Get only the content not the headers 
     curl_setopt($ch, CURLOPT_HEADER, 0); 

     // Return the value instead of printing the response to browser 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

     // Use a user agent to mimic a browser 
     curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0'); 

     return $ch; 

     // Remember to always close the session and free all resources 
    } 

    // Make sure curl is installed 
    $curl_exists = function_exists('curl_init'); 
    if (!$curl_exists) { 
     return; 
    } 

    $todayfeast = date("Ymd"); 

    $ctitleff = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading_lt&lang=FR&content=FR'; 
    $creadff = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading&lang=FR&content=FR'; 

    $ch = get_curl(); 
    curl_setopt($ch, CURLOPT_URL, $ctitleff);   
    $titleff = curl_exec($ch); 
    curl_close($ch); 

    $ch = get_curl(); 
    curl_setopt($ch, CURLOPT_URL, $creadff);  
    $readff = curl_exec($ch); 
    curl_close($ch); 

    $ctitlepf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading_lt&lang=FR&content=PS'; 
    $creadpf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&&type=reading&lang=FR&content=PS'; 

    $ch = get_curl(); 
    curl_setopt($ch, CURLOPT_URL, $ctitlepf);  
    $titlepf = curl_exec($ch); 
    curl_close($ch); 

    $ch = get_curl(); 
    curl_setopt($ch, CURLOPT_URL, $creadpf); 
    $readpf = curl_exec($ch); 
    curl_close($ch); 

    $titlesf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading_lt&lang=FR&content=SR'; 
    $readsf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading&lang=FR&content=SR'; 

    $ch = get_curl(); 
    curl_setopt($ch, CURLOPT_URL, $titlesf); 
    $titlesf = curl_exec($ch); 
    curl_close($ch); 

    $ch = get_curl(); 
    curl_setopt($ch, CURLOPT_URL, $readsf); 
    $readsf = curl_exec($ch); 
    curl_close($ch); 

    $titlegf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading_lt&lang=FR&content=GSP'; 
    $readgf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading&lang=FR&content=GSP'; 

    $ch = get_curl(); 
    curl_setopt($ch, CURLOPT_URL, $titlegf); 
    $titlegf = curl_exec($ch); 
    curl_close($ch); 

    $ch = get_curl(); 
    curl_setopt($ch, CURLOPT_URL, $readgf); 
    $readgf = curl_exec($ch); 
    curl_close($ch); 

    // Send info 
    $params = array(
     'titleff' => $titleff, 
     'readff' => $readff, 
     'titlepf' => $titlepf, 
     'readpf' => $readpf, 
     'titlesf' => $titlesf, 
     'readsf' => $readsf, 
     'titlegf' => $titlegf, 
     'readgf' => $readgf, 
    ); 

    echo json_encode($params); 

?> 

편집

나는 아약스 통해 PHP 스크립트를 호출 할 때 문제는 발생합니다. 내가 대신이 파일을 가지고 있고, 작동 <?php require("filename.php") ?> 내 메인 페이지에 포함하는 경우

<script> 
    $('#readingf').ready(function(){ 
    $.ajax({ 
     url: "loadfrench.php", 
     type: "GET", 
     data: { }, 
     cache: false, 
     async: true, 
     dataType: 'json', 
     success: function (response) { 
      if (response != '') 
      { 
      $('#titleff').html(response.titleff); 
      $('#readff').html(response.readff); 
      $('#titlepf').html(response.titlepf); 
      $('#readpf').html(response.readpf); 
      $('#titlesf').html(response.titlesf); 
      $('#readsf').html(response.readsf); 
      $('#titlegf').html(response.titlegf); 
      $('#readgf').html(response.readgf); 
      alert ('titleff '+response.titleff+' readff '+response.readff); 
      } 
     }, 
     error: function (request, status, error) { 
      alert ("status "+status+" error "+error+"responseText "+request.responseText); 
     }, 
    });  

    }); 
</script> 

.

<?php 
    function get_curl() { 
     // Initialize a new curl resource 
     $ch = curl_init(); 

     // Get only the content not the headers 
     curl_setopt($ch, CURLOPT_HEADER, 0); 

     // Return the value instead of printing the response to browser 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

     // Use a user agent to mimic a browser 
     curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0'); 

     return $ch; 

     // Remember to always close the session and free all resources 
    } 

    // Make sure curl is installed 
    $curl_exists = function_exists('curl_init'); 
    if (!$curl_exists) { 
     return; 
    } 

    $ch = get_curl(); 

    $todayfeast = date("Ymd"); 
    /* $titleff = $readff = $titlepf = $readpf = $titlesf = $readsf = $titlegf = $readgf = "blank"; */ 

    $ctitleff = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading_lt&lang=FR&content=FR'; 
    $creadff = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading&lang=FR&content=FR'; 

    curl_setopt($ch, CURLOPT_URL, $ctitleff);   
    $titleff = curl_exec($ch); 

    curl_setopt($ch, CURLOPT_URL, $creadff);  
    $readff = curl_exec($ch); 

    $ctitlepf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading_lt&lang=FR&content=PS'; 
    $creadpf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&&type=reading&lang=FR&content=PS'; 

    curl_setopt($ch, CURLOPT_URL, $ctitlepf);  
    $titlepf = curl_exec($ch); 

    curl_setopt($ch, CURLOPT_URL, $creadpf); 
    $readpf = curl_exec($ch); 

    $titlesf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading_lt&lang=FR&content=SR'; 
    $readsf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading&lang=FR&content=SR'; 

    curl_setopt($ch, CURLOPT_URL, $titlesf); 
    $titlesf = curl_exec($ch); 

    curl_setopt($ch, CURLOPT_URL, $readsf); 
    $readsf = curl_exec($ch); 

    $titlegf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading_lt&lang=FR&content=GSP'; 
    $readgf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading&lang=FR&content=GSP'; 

    curl_setopt($ch, CURLOPT_URL, $titlegf); 
    $titlegf = curl_exec($ch); 

    curl_setopt($ch, CURLOPT_URL, $readgf); 
    $readgf = curl_exec($ch); 

    curl_close($ch); 

    // Send info 
    $params = array(
     'titleff' => $titleff, 
     'readff' => $readff, 
     'titlepf' => $titlepf, 
     'readpf' => $readpf, 
     'titlesf' => $titlesf, 
     'readsf' => $readsf, 
     'titlegf' => $titlegf, 
     'readgf' => $readgf, 
    ); 

    echo json_encode($params); 

?> 


<?php 
    $curl_exists = function_exists('curl_init'); 

    // Make sure curl is installed 
    if ($curl_exists) { 

     // Initialize a new curl resource 
     $ch = curl_init(); 

     // Get only the content not the headers 
     curl_setopt($ch, CURLOPT_HEADER, 0); 

     // Return the value instead of printing the response to browser 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

     // Use a user agent to mimic a browser 
     curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0'); 

    } 

?> 

<div id="readfre" style="overflow:hidden;"> 
<div id="readingf" class="tabber" style="width:100%; margin-top: 10px; margin-right:0px;"> 

<?php 

    $todayfeast = date("Ymd"); 

    $ctitleff = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading_lt&lang=FR&content=FR'; 
    $creadff = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading&lang=FR&content=FR'; 

    if ($curl_exists) { 
     curl_setopt($ch, CURLOPT_URL, $ctitleff);   
     $titleff = curl_exec($ch); 
    } 

    if ($curl_exists) { 
     curl_setopt($ch, CURLOPT_URL, $creadff);  
     $readff = curl_exec($ch); 
    } 

?> 
    <div id="readf" class="tabbertab"> 
     <h2>PremiËre Lecture</h2> 
     <div id='titleff' class='rtitle'> 
     <?php echo $titleff; ?> 
     </div> 
     <div id='readff' class='rtext'> 
     <?php echo $readff; ?> 
     </div> 
    </div> 

<?php 

    $ctitlepf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading_lt&lang=FR&content=PS'; 
    $creadpf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&&type=reading&lang=FR&content=PS'; 

    if ($curl_exists) { 
     curl_setopt($ch, CURLOPT_URL, $ctitlepf);  
     $titlepf = curl_exec($ch); 
    } 

    if ($curl_exists) { 
     curl_setopt($ch, CURLOPT_URL, $creadpf); 
     $readpf = curl_exec($ch); 
    } 
?> 
    <div id="readp" class="tabbertab"> 
     <h2>Psaume</h2> 
     <div id='titlepf' class='rtitle'> 
     <?php echo $titlepf; ?> 
     </div> 
     <div id='readpf' class='rtext'> 
     <?php echo $readpf; ?> 
     </div> 
    </div> 

    <?php 

     $titlesf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading_lt&lang=FR&content=SR'; 
     $readsf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading&lang=FR&content=SR'; 

     if ($curl_exists) { 
      curl_setopt($ch, CURLOPT_URL, $titlesf); 
      $titlesf = curl_exec($ch); 
     } 

     if ($curl_exists) { 
      curl_setopt($ch, CURLOPT_URL, $readsf); 
      $readsf = curl_exec($ch); 
     } 

    ?> 
     <div id="reads" class="tabbertab"> 
      <h2>DeuxiËme Lecture</h2> 
      <div id='titlesf' class='rtitle'> 
      <?php echo $titlesf; ?> 
      </div> 
      <div id='readsf' class='rtext'> 
      <?php echo $readsf; ?> 
      </div> 
     </div> 

    <?php 

     $titlegf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading_lt&lang=FR&content=GSP'; 
     $readgf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading&lang=FR&content=GSP'; 

     if ($curl_exists) { 
      curl_setopt($ch, CURLOPT_URL, $titlegf); 
      $titlegf = curl_exec($ch); 
     } 

     if ($curl_exists) { 
      curl_setopt($ch, CURLOPT_URL, $readgf); 
      $readgf = curl_exec($ch); 
     } 

    ?> 
     <div id="readg" class="tabbertab"> 
      <h2>Evangile</h2> 
      <div id='titlegf' class='rtitle'> 
      <?php echo $titlegf; ?> 
      </div> 
      <div id='readgf' class='rtext'> 
      <?php echo $readgf; ?> 
      </div> 
     </div> 

    <?php 

     // Remember to always close the session and free all resources 
     if ($curl_exists) { 
      curl_close($ch); 
     } 
    ?> 

    </div> 
    </div> 
+0

멀티 컬을 보았습니까? 다운로드 프로세스가 상당히 빨라지므로 문제가 해결 될 수도 있습니다. –

+1

['curl_multi_exec()'] (http://php.net/manual/en/function.curl-multi-exec.php)을 보시면 스크립트를 훨씬 빨리 작성할 수 있습니다. '나는 이것이 자원을 즉시 자유롭게 할 것이라고 생각했다. '- 이것은 PHP의 경우에는 드물다. [read this] (http://uk.php.net/manual/en/features.gc.php) – DaveRandom

+0

" 특정 수의 URL을 검색 한 후 컬은 null을 리턴합니다. " - 매번 같은 번호인가요? (즉, 오류가 발생하는 동일한 URL 임). 각 컬 요청을 개별적으로 시도하여 모두가 고립되어 작동하는지 확인하십시오. – Ing

답변

1

그런 일이 정확히 왜 내가 말할 수는 없지만, 같은 서버에서 가져 오는되어보고, 나는 새로운 컬 핸들마다 시간을 만들 것입니다. 단순히 같은 것을 사용하고 CURLOPT_URL 만 변경하면됩니다. 그런 식으로, 서버는 대신에 새로운마다 만드는, TCP 연결을 다시 사용합니다 :

$ch = get_curl(); 

curl_setopt($ch, CURLOPT_URL, $ctitleff);   
$titleff = curl_exec($ch); 

curl_setopt($ch, CURLOPT_URL, $creadff);  
$readff = curl_exec($ch); 

... 

curl_close($ch);  

그것이 대답 확실하지,하지만 그것은 또한 :

, 당신이 할 수 있습니다 시도하는 뭔가 한번보세요 curl-multi-exec()

+0

확인. 나는 이것에 대해 궁금해하고 있었다. – user823527