执行php多个curl时显示进度条

I am fetching address coordinates from addresses. A user can upload a CSV file which has 100 or 1000 of records, and after that I am fetching coordinate data using

$cityclean   = str_replace(" ", "+", $address);
$details_url = "http://maps.googleapis.com/maps/api/geocode/json?address=" . $cityclean . "&sensor=false";
$ch          = curl_init();
curl_setopt($ch, CURLOPT_URL, $details_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$geoloc = json_decode(curl_exec($ch), true);
$lat    = $geoloc['results'][0]['geometry']['location']['lat'];
$lng    = $geoloc['results'][0]['geometry']['location']['lng'];

I want to update a progress bar when every record is fetched.

For example, suppose I have 100 records in the CSV. Every time I get coordinates, I want to increase the progress bar. In other words, I want the user to be able to see how many coordinates have been successfully fetched.

I am following the example here, but it's not working correctly. The progress bar is not running.

Here is my complete code:

    <table >
    <form action="<?php
    echo $_SERVER["PHP_SELF"];
    ?>" method="post" enctype="multipart/form-data">

    <tr>
    <td width="20%">Select file</td>
    <td width="80%"><input type="file" name="file" id="file" /></td>
    </tr>

    <tr>
    <td width="20%">Or Enter Address:</td>
    <td width="80%"><input type="text" name="address" id="address" /></td>
    </tr>


    <tr>
    <td width="20%">Email address to send geo-Locationfile:</td>
    <td width="80%"><input type="text" name="email" id="email" /></td>
    </tr>

    <tr>
    <td></td>
    <td><input type="submit" name="submit" /></td>
    </tr>

    </form>
    </table>
    <h2>NOTES:</h2>
     1.)You can fetch only 10 rows from CSV file, Because this is a demo.
     <br>
     2.)If you provide address, Then you are not able to download or email generated geoCode file .
     <br><br><br><br><br><br>
     <h2>Results:-</h2>
    <br>
    <?php
    if (isset($_POST["submit"])) {
require_once 'class.ProgressBar.php';
echo '<div style="width: 300px;">';
 //$p->render();
 echo '</div>';
    if (isset($_FILES["file"])) {

    //if there was an error uploading the file
    if ($_FILES["file"]["error"] > 0) {
        //echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        $data = $_POST["address"];

        $address = trim($data);
        if ($address != "") {
            $cityclean   = str_replace(" ", "+", $address);
            $details_url = "http://maps.googleapis.com/maps/api/geocode/json?address=" . $cityclean . "&sensor=false";
            $ch          = curl_init();
            curl_setopt($ch, CURLOPT_URL, $details_url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $geoloc = json_decode(curl_exec($ch), true);
            $lat    = $geoloc['results'][0]['geometry']['location']['lat'];
            $lng    = $geoloc['results'][0]['geometry']['location']['lng'];
            echo $address . "------Latitude:      " . $lat . "  ----  Longitude: " . $lng;
        } else {
            echo "Please enter a address, Or upload a CSV file.";
        }

    } else {
        //Print file details
        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        $name    = $_FILES['file']['name'];
        $ext     = strtolower(end(explode('.', $_FILES['file']['name'])));
        $type    = $_FILES['file']['type'];
        $tmpName = $_FILES['file']['tmp_name'];

        // check the file is a csv
        if ($ext === 'csv') {
            if (($handle = fopen($tmpName, 'r')) !== FALSE) {
                // necessary if a large csv file
                set_time_limit(0);
                $row  = 1;
                $file = fopen('ids.txt', 'w');
               $num_records =sizeof(fgetcsv($handle, 1000, ','));
              // $calc = 100/$num_records;
                $calc=100/20;
                $x=1;
                $p = new ProgressBar();
                $p->render();
                while ((($data = fgetcsv($handle, 1000, ',')) !== FALSE) && $row != 20) {
                    // number of fields in the csv
                    $address     = implode(",", $data);
                     $p->setProgressBarProgress($x); 
                    $cityclean   = str_replace(" ", "+", $address);
                    $details_url = "http://maps.googleapis.com/maps/api/geocode/json?address=" . $cityclean . "&sensor=false";
                    $ch  = curl_init();
                    curl_setopt($ch, CURLOPT_URL, $details_url);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                    $geoloc = json_decode(curl_exec($ch), true);
                    $lat    = $geoloc['results'][0]['geometry']['location']['lat'];
                    $lng    = $geoloc['results'][0]['geometry']['location']['lng'];
                  //  echo $address . "------Latitude:      " . $lat . "  ----  Longitude: " . $lng;
                    fwrite($file, $address . "------Latitude:      " . $lat . "  ----  Longitude: " . $lng . "
");


                   // echo "<br/>";
                   // echo $details_url;
                    //show progress bar
                    echo $x;echo "<br/>";
                    $x += $calc;
                    //$x++;
                    $row++;
                }
                fclose($file);
                fclose($handle);

                echo "
 To download generated file <a href='ids.txt'>Click Here</a> . 
";
               /* $file      = "ids.txt";
                $email2    = $_POST["email"];
                $file_size = filesize($file);
                $handle    = fopen($file, "r");
                $content   = fread($handle, $file_size);
                fclose($handle);
                $filename = "geoCode.txt";
                $content  = chunk_split(base64_encode($content));

                // a random hash will be necessary to send mixed content
                $separator = md5(time());

                $message = "Please find your geo location file as an attachment.";

                // carriage return type (we use a PHP end of line constant)
                $eol = PHP_EOL;

                // main header (multipart mandatory)
                $headers = "From: name <test@test.com>" . $eol;
                $headers .= "MIME-Version: 1.0" . $eol;
                $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol . $eol;
                $headers .= "Content-Transfer-Encoding: 7bit" . $eol;
                $headers .= "This is a MIME encoded message." . $eol . $eol;

                // message
                $headers .= "--" . $separator . $eol;
                $headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
                $headers .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
                $headers .= $message . $eol . $eol;

                // attachment
                $headers .= "--" . $separator . $eol;
                $headers .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
                $headers .= "Content-Transfer-Encoding: base64" . $eol;
                $headers .= "Content-Disposition: attachment" . $eol . $eol;
                $headers .= $content . $eol . $eol;
                $headers .= "--" . $separator . "--";

                //SEND Mail
                if (mail($email2, "geoCode", "", $headers)) {
                    echo "mail send to " . $email2 . " ... OK"; // or use booleans here
                } else {
                    echo "mail send ... ERROR!";
                }*/

            }

        }
    }
} else {

}
    }

    ?>

Here is the link for CSV:

https://drive.google.com/open?id=0Bx3FBqTEy_0MaHp1QVhiNkVTLU0

As is it you can not show a progress bar... When you submit the form the post is sent to the php which runs your code on the server alone and only when it finishes runing it sends the html content back to your browser.

So... there is no way this php script could behave as you want.

One thing you could try if accurate progress is that important...

You could submit the form by ajax to a php script (in the server).

The php script would read the whole csv file and send it back in a json array to the caller js on the client (your browser).

the js would parse the array "line by line" and for each line it would send a single ajax request to another php script that would store that single record and replay "ok" (server).

The caller js script (browser) would update the progress bar and make another single call...

keep doing this untill the end of the array.