PHP CURL Request for 10K Times Same URL
I have an Excel files approx. 1,29 MB and it has 11500 lines of data.
In my PHP code first i read whole files and put in arrays via PHPExcel/IOFactory library.
After the putting, in foreach loop i start to create $postvars for cUrl.
gonderi_referans=REF&alici=Sarah&alici_telefon=55544448885&alici_adres=Krg&alici_ulke=DE
In foreach loop the code will create 11500 times $postvars and make curl request.
$url = "https://$_SERVER[HTTP_HOST]/api572.php"; $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_POST, 1); curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars); curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,30); curl_setopt($ch,CURLOPT_TIMEOUT, 20); $response =json_decode(curl_exec($ch), true);
This operation takes approx 5 minutes. I want to do it in a faster way. How can i do it?
Code read excel files in a 10 seconds, curl request it takes lots of times.
Try to use some batch-processing to minimize the number of sent requests, totally basic POC:
excel-reader.php
:
<?php // Dummy data START $dummyExcelRows = []; for ($i = 0; $i <= 11500; $i++) { $dummyExcelRows[] = "gonderi_referans=REF&alici=Sarah&alici_telefon=55544448885&alici_adres=Krg&alici_ulke=DE&id=$i"; } // Dummy data END // Serialize multiple URI into one string START $data = []; foreach ($dummyExcelRows as $postvars) { $data[] = $postvars; } $dataToSend = ['payload' => json_encode($data)]; // Serialize multiple URI into one string END $url = "http://$_SERVER[HTTP_HOST]/test/api572.php"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $dataToSend); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ch, CURLOPT_TIMEOUT, 20); // $response =json_decode(curl_exec($ch), true); $response = curl_exec($ch); var_dump($response);
api572.php
:
<?php $decoded = json_decode($_POST['payload']); foreach ($decoded as $row) { echo "Do something with $row <br/>"; echo '<pre>'; parse_str($row, $fakePost); print_r($fakePost); echo '</pre>'; }