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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么