當前有一個文件設置爲讀取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);
}
}
?>
我打電話給的api是在幾個網站上,所以沒有一個網址被擊中1600次。有可能Windows可能將我的腳本視爲惡意,因爲它會發出幾千個外撥電話?而且這是我自己的系統阻止它? – jppower175
嗯....你試圖從瀏覽器或CLI運行這個? – raidenace
從我的瀏覽器運行它 – jppower175