2017-02-16 45 views
0

當前有一個文件設置爲讀取CSV文件。 CSV文件包含1600個api查詢。然後,每個API查詢都會返回更多需要運行的查詢。我在Windows 10上使用Xampp v3.2.2並運行php v5.6.15。當通過我的瀏覽器運行文件時,在超時前CSV中的前800條記錄運行正常。當我現在重新運行文件時,出現錯誤「網站無法到達ERR_CONNECTION_RESET」。不知道是什麼原因造成的。代碼的縮寫版本包括以下1個文件的Xampp ERR_CONNECTION_RESET

<?php 
error_reporting(E_ERROR | E_PARSE); 
set_time_limit (28800); 


$csv = array_map('str_getcsv', file('file.csv')); 
for($i = 0; $i < count($csv); $i++){ 
    if($csv[$i][2] == 1){ //this item in csv is flag to check if this item has been run yet 
     if($csv[$i][3] != 'NULL' && trim($csv[$i][3]) != ''){ // check to make sure there is a URL 
      $return = file_get_contents(trim($csv[$i][3])); //Get the contents that links to new api calls 
      if($return){ 
       $isCall = array(); // array to store all new calls 
       $data = array(); // array to store all data to put in csv 
       $doc = new DOMDocument('1.0'); // create new DOM object 
       $doc->loadHTML($return); // load page string into DOM object 
       $links = $doc->getElementsByTagName('a'); // get all <a> tags on the page 
       if($links->length > 0){ // if there is at least one <a> tag on page 
        for($j = 0; $j < $links->length; $j++){ // loop through <a> tags 
         $isCall[]= $links->item($j)->getAttribute('href'); // get href attribute from <a> tag and push into array 

        } 

        for($x = 0; $x < count($isCall); $x++){ // loop through all the calls and search for data 
         $string = file_get_contents($isCall[$x]); 
         if($string) { 
          $thispage = new DOMDocument('1.0'); 
          $thispage->loadHTML($string); 
          $pagedata = $thispage->getElementsByTagName('div'); 
          if ($pagedata->length > 0) { 
           for($j = 0; $j < $pagedata->length; $j++) { 
            $data[] = $pagedata->item($j)->C14N(); 
           } 
          } 
         } 
         if(count($data) >= 5) break; // limiting to 5 data points to be added to csv 
        } 

       } 
       if(!empty($data)) $csv[$i] = array_merge($csv[$i], $data); // if we have data points lets add them to the row in the csv 
      } 

     } 

     $csv[$i][2] = 2; // set the flag to 2 

     $fp = fopen('file.csv', 'w'); // write the contents to the csv each time through loops so if it fails we start at last completed record 

     foreach ($csv as $f) { 
      fputcsv($fp, $f); 
     } 

     fclose($fp); 
    } 


} 
?> 

回答

1

通常ERR_CONNECTION_RESET是發生,當你試圖連接到網站無法建立連接的錯誤。這通常是由於防火牆阻止ISP緩存等問題導致的。

但是在您的情況下,我覺得您連接的網站會自動關閉連接嘗試,因爲您要做的是循環並連續訪問該API網站1600次。

該API網站允許前800多次嘗試,但在此之後,它擔心您可能是一個試圖傷害它的惡意腳本。就像DOS(拒絕服務)嘗試的典型例子。

您應該檢查客戶在固定時間內可以對API網站進行的嘗試次數是否有限制(例如每24小時打500次),或者每次打到N後嘗試睡眠N秒該網站或在每個X次點擊網站之後。

+0

我打電話給的api是在幾個網站上,所以沒有一個網址被擊中1600次。有可能Windows可能將我的腳本視爲惡意,因爲它會發出幾千個外撥電話?而且這是我自己的系統阻止它? – jppower175

+0

嗯....你試圖從瀏覽器或CLI運行這個? – raidenace

+0

從我的瀏覽器運行它 – jppower175