2017-02-16 17:28 阅读 57


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

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
                $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');
                            $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);


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    douyingzhan5905 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.

    点赞 评论 复制链接分享