doumingchen3628 2017-02-16 17:28
浏览 69
已采纳

Xampp ERR_CONNECTION_RESET为1个文件

Currently have a file that is set to read a CSV file. The CSV file contains 1600 api queries. Then each api query then returns more queries that need to be run. I am using Xampp v3.2.2 on windows 10 and running php v5.6.15. When running the file through my browser it ran fine for the first 800+ records in the CSV before timing out. When I rerun the file now I get an error "Site can't be reach ERR_CONNECTION_RESET". Not sure what could be causing this. abbreviated version of the code is included below

<?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条回答 默认 最新

  • douyingzhan5905 2017-02-16 17:46
    关注

    Usually ERR_CONNECTION_RESET is an error that occurs when the site you are trying to connect to is unable to establish that connection. This usually happens due to reasons like firewall blocking on issues with ISP cache etc.

    However in your case, I feel that the site you are connecting to is voluntarily closing connection attempts because what you are trying to do is loop over and hit that API site 1600 times continuously.

    The API site is allowing the first 800-odd attempts but after that it gets worried that you are perhaps a malicious script trying to harm it. Like a classical example of DOS (Denial Of Service) attempt.

    You should check if there is any restriction to the number of attempts a client can make to the API site with a fixed time (like say 500 hits every 24 hours) or you should try to sleep N seconds after each hit to the site or after each X number of hits to the site.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器