当前有一个设置为读取CSV文件的文件。CSV文件包含1600个api查询。然后,每个api查询返回更多需要运行的查询。我在windows 10上使用Xamppv3.2.2,运行phpv5.6.15。当通过我的浏览器运行该文件时,它在超时之前对CSV中的第一个800+记录运行良好。当我现在重新运行文件时,我会得到一个错误:“站点无法到达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);
}
}
?>发布于 2017-02-16 17:46:40
通常,当您试图连接到的站点无法建立该连接时,会发生ERR_CONNECTION_RESET错误。这种情况通常是由于防火墙阻塞、ISP缓存问题等原因造成的。
但是,在您的情况下,我觉得您所连接的站点正在自动关闭连接尝试,因为您要做的是循环并连续访问该API站点1600次。
API站点允许最初的800次尝试,但在那之后,它开始担心您可能是一个恶意脚本,试图破坏它。就像一个典型的DOS (拒绝服务)尝试的例子。
您应该检查客户端在固定时间内对API站点的尝试次数是否有任何限制(例如,每24小时有500次点击),或者您应该在每次访问站点之后或每次对站点的每次点击次数X次之后,尝试睡眠N秒。
https://stackoverflow.com/questions/42280868
复制相似问题