I use curl_multi to scrap several site, and I hope it will run asynchronously or parallel so it will not other to finish first.
I use jquery to trigger xmlhttprequest to php.
But what I get it still run synchronous, I dont know what is wrong whether my code or my PC specification (I run local server use core-i5 and 4GB memory)
This is my jquery code
function ajaxSearchFlight(agent)
{
var depart_from = $("#depart_from").val();
var arrive_to = $("#arrive_to").val();
var depart_date = $("#depart_date").val();
var adult = $("#adult").val();
var child = $("#child").val();
var infant = $("#infant").val();
depart_date = new Date(depart_date);
var year = depart_date.getFullYear();
var month = depart_date.getMonth() + 1;
var date = depart_date.getDate();
if(month < 10)
{
month = "0"+month;
}
if(date < 10)
{
date = "0"+date;
}
depart_date = year+"-"+month+"-"+date;
if(agent == "all")
{
findflight_tesflight1(depart_from, arrive_to, depart_date, adult, child, infant);
findflight_tesflight2(depart_from, arrive_to, depart_date, adult, child, infant);
}
}
function findflight_tesflight1(depart_from, arrive_to, depart_date, adult, child, infant)
{
var xhr = new XMLHttpRequest();
xhr.open('GET', '<?php echo Url::toRoute("flight/getliveflighttesflight1"); ?>?depart_from='+depart_from+'&arrive_to='+arrive_to+'&date_format_start='+depart_date+'&adults_pass='+adult+'&children_pass='+child+'&infants_pass='+infant, true);
xhr.onreadystatechange = function() {
if(this.readyState == 4)
{
if(this.status == 200)
{
var data = JSON.parse(this.responseText);
}
}
};
xhr.send();
}
function findflight_tesflight2(depart_from, arrive_to, depart_date, adult, child, infant)
{
var xhr = new XMLHttpRequest();
xhr.open('GET', '<?php echo Url::toRoute("flight/getliveflighttesflight2"); ?>?depart_from='+depart_from+'&arrive_to='+arrive_to+'&date_format_start='+depart_date+'&adults_pass='+adult+'&children_pass='+child+'&infants_pass='+infant, true);
xhr.onreadystatechange = function() {
if(this.readyState == 4)
{
if(this.status == 200)
{
var data = JSON.parse(this.responseText);
}
}
};
xhr.send();
}
and this is PHP code
class FlightController extends \yii\web\Controller
{
protected $mh;
public function beforeAction($event)
{
$this->mh = curl_multi_init();
return parent::beforeAction($event);
}
public function actionGetliveflighttesflight1($depart_from, $arrive_to, $date_format_start, $adult, $child, $infant)
{
$url = 'https://api.flight2.com/en/flight/';
$response_tes = "";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_multi_add_handle($this->mh,$ch);
do {
while(($execrun = curl_multi_exec($this->mh, $running)) == CURLM_CALL_MULTI_PERFORM);
if($execrun != CURLM_OK)
break;
while($done = curl_multi_info_read($this->mh)) {
$info = curl_getinfo($done['handle']);
if ($info['http_code'] == 200) {
$output = curl_multi_getcontent($done['handle']);
$response_tes = $output;
curl_multi_remove_handle($this->mh, $done['handle']);
} else {
}
}
} while ($running);
try {
return $response_tes;
} catch (Exception $e) {
}
}
public function actionGetliveflighttesflight2($depart_from, $arrive_to, $date_format_start, $adult, $child, $infant)
{
$url = 'https://www.flight1.com/ajax?d='.$depart_from.'&a='.$arrive_to.'&date='.$date_format_start.'&adult='.$adult.'&child='.$child.'&infant='.$infant;
$response_tes = "";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_multi_add_handle($this->mh,$ch);
do {
while(($execrun = curl_multi_exec($this->mh, $running)) == CURLM_CALL_MULTI_PERFORM);
if($execrun != CURLM_OK)
break;
while($done = curl_multi_info_read($this->mh)) {
$info = curl_getinfo($done['handle']);
if ($info['http_code'] == 200) {
$output = curl_multi_getcontent($done['handle']);
$response_tes = $output;
curl_multi_remove_handle($this->mh, $done['handle']);
} else {
}
}
} while ($running);
try {
return $response_tes;
} catch (Exception $e) {
}
}
}
Please help, I need to make it async.