PHP - foreach循环立即运行所有数组?

时间:2016-04-22 07:03:58

标签: php arrays foreach instant

我试图在循环中使用" foreach"循环一些函数。和数组。但获取信息需要很长时间。是否可以同时运行所有数组循环元素?

ini_set("max_execution_time", 0);
function bhttpGet($burl)
{
    global $data;
    global $makaronas;
    global $makaronas2;
    $data = array("clientType" => "Android", "os" => "21", "screenType" => "xxhdpi", "version" => "3");                                                                    
    $data_string = json_encode($data);  
    $bch = curl_init();  

    curl_setopt($bch,CURLOPT_URL,$burl);
    curl_setopt($bch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($bch,CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($bch,CURLOPT_HEADER, false);
    curl_setopt($bch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($bch, CURLOPT_HEADER, 1);
    curl_setopt($bch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($bch, CURLOPT_HTTPHEADER, array(
    'Accept-Language: en',
    'Content-Type: application/json; charset=UTF-8',
    'User-Agent: Dalvik/2.1.0 (Linux; U; Android 5.0.2; HTC One Build/LRX22G)',
    'Host: mobileapi.turkishairlines.com',
    'Connection: Keep-Alive',
    'Accept-Encoding: gzip',
    'Cookie: JSESSIONID=5096E068E5B2E11E4EC96A6B725C5BB1; AWSELB=A3F31BC902ED4282BBCBA2D31E20E7210081D104D0AC59E7291F2B1692E6E27BB90F1D78BBC7CFBAD0D14DA4FFA5A59F223573F1C428362BCD544E72A28094F07742E08B3292F74F8BE17859D515D4392E4D9C052C'
    ));

    $result = curl_exec($bch);
    curl_close($bch);
    preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $result, $matches);
    $cookies = array();
    foreach($matches[1] as $item) {
        parse_str($item, $cookie);
        $cookies = array_merge($cookies, $cookie);
    }
    $makaronas = $cookies['JSESSIONID'];
    $makaronas2 = $cookies['AWSELB'];
}

function httpGet($url){
    global $makaronas3;
    global $makaronas4;
    $ch = curl_init();  

    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Accept-Language: en',
    'User-Agent: Dalvik/2.1.0 (Linux; U; Android 5.0.2; HTC One Build/LRX22G)',
    'Host: mobileapi.turkishairlines.com',
    'Connection: Keep-Alive',
    'Accept-Encoding: gzip',
    "Cookie: JSESSIONID={$makaronas3}; AWSELB={$makaronas4}"

    ));
    $output2=curl_exec($ch);
    curl_close($ch);
 }

 function chttpGet($kurl){
    global $makaronas3;
    global $makaronas4;
    global $output3;
    $kch = curl_init();  

    curl_setopt($kch,CURLOPT_URL,$kurl);
    curl_setopt($kch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($kch,CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($kch, CURLOPT_HTTPHEADER, array(
    'Accept-Language: en',
    'User-Agent: Dalvik/2.1.0 (Linux; U; Android 5.0.2; HTC One Build/LRX22G)',
    'Host: mobileapi.turkishairlines.com',
    'Connection: Keep-Alive',
    'Accept-Encoding: gzip',
    "Cookie: JSESSIONID={$makaronas3}; AWSELB={$makaronas4}"
    ));

    $output3=curl_exec($kch);
    curl_close($kch);
}

function between($src,$start,$end){
    $txt=explode($start,$src);
    $txt2=explode($end,$txt[1]);
    return trim($txt2[0]);
  }
$departuredatearray= array("01.08.2016","02.08.2016","03.08.2016","04.08.2016","05.08.2016","06.08.2016","07.08.2016","08.08.2016","09.08.2016","10.08.2016","11.08.2016","12.08.2016","13.08.2016","14.08.2016","15.08.2016","16.08.2016","17.08.2016","18.08.2016","19.08.2016","20.08.2016","21.08.2016","22.08.2016","23.08.2016","24.08.2016","25.08.2016","26.08.2016","27.08.2016","28.08.2016","29.08.2016","30.08.2016","31.08.2016");

    foreach ($departuredatearray as $lopas) {
    $initialize="https://mobileapi.turkishairlines.com/init.json";
    $initialize2 = "https://mobileapi.turkishairlines.com/booking/internationalFlights.json?cabinType=economy&adultCount=1&childCount=0&infantCount=0&isOneWay=true&departureAirportCode=VNO&arrivalAirportCode=IST&flightDate={$lopas}&isReturn=false";
    $initialize3 = "https://mobileapi.turkishairlines.com/booking/internationalFare.json?departureFlightIndex=0&returnFlightIndex=-1&isDeparturePromotion=false&isReturnPromotion=false";
$mano=bhttpget($initialize);
echo "$mano";
$makaronas3=$makaronas;
$makaronas4=$makaronas2;
$mano2=httpget($initialize2);

echo "$mano2";
$mano4 = chttpget($initialize3);

echo "$mano4";
echo "<BR>";
echo "<table border='1' align='center' style='text-align:center;'>";
    echo "<tr>";
        echo "<td>flight Number</td>";
        echo "<td>departure Airport Code</td>";
        echo "<td>arrival Airport Code</td>";
        echo "<td>departure Date</td>";
        echo "<td>departure Time</td>";
        echo "<td>arrival Date</td>";
        echo "<td>arrival Time</td>";
        echo "<td>duration</td>";
        echo "<td>total Price</td>";
        echo "<td>Curency</td>";
    echo "</tr>";
    echo "<tr>";
        echo "<td>";
        echo between($output3, '"flightNumber":"', '","departureAirportCode');
        echo "</td>";
        echo "<td>";
        echo between($output3, 'departureAirportCode":"', '","departureAirportName');
        echo "</td>";
        echo "<td>";
        echo between($output3, 'arrivalAirportCode":"', '","arrivalAirportName');
            echo "</td>";          
            echo "<td>";
            echo between($output3, 'departureDate":"', '","formattedDepartureDate');
            echo "</td>";   
            echo "<td>";
            echo between($output3, 'departureTime":"', '","arrivalDate');
            echo "</td>";   
            echo "<td>";
            echo between($output3, 'arrivalDate":"', '","formattedArrivalDate');
            echo "</td>"; 
            echo "<td>";
            echo between($output3, 'arrivalTime":"', '","duration');
            echo "</td>";  
            echo "<td>";
            echo between($output3, 'duration":"', '","stopTimeAfterArrival');
            echo "</td>";
            echo "<td>";
            echo between($output3, 'totalPrice":"', '","currency":"');
            echo "</td>";
            echo "<td>";
            echo between($output3, 'currency":"', '","totalTax');
            echo "</td>";

echo "</table>";
};

2 个答案:

答案 0 :(得分:2)

此处的问题是foreach的每次迭代在HTTP请求上被阻止的时间。这是API延迟的问题,通常通过同时处理HTTP请求的部分来处理。

例如,如果每个HTTP请求需要3秒钟才能完成,并且您可以同时处理10个HTTP请求,那么您已使脚本 大致 快10倍

 100 Requests w/o concurrency (3 seconds each, 1 at a time) = 300 seconds
 100 Requests w/ concurrency (3 seconds each, 10 at a time) =  30 seconds

有一种很好的方法可以并行化curl_multi*函数套件的HTTP请求。它内置于cURL库,非常易于使用。

教程: http://www.phpied.com/simultaneuos-http-requests-in-php-with-curl/

PHP手册: http://php.net/manual/en/function.curl-multi-exec.php

请务必注意,您可以并且应该同时执行的HTTP请求数量存在实际限制。 curl_multi*默认为10个并发HTTP请求,这是一个非常好的限制。必须通过关心,尊重和责任来增加此限制,否则会严重损害其他服务。

要增加此限制(例如,如果您对自己的服务器发出请求),可以将curl_multi_setoptCURLMOPT_MAXCONNECTS选项一起使用

$new_limit = 15;
curl_multi_setopt ( $mh, CURLMOPT_MAXCONNECTS, $new_limit );

另请注意,除非您的服务器/互联网连接出现问题,否则您可以更快地发出单个请求。并发性不会帮助你更快地提出个别请求(只是认为我已经明确了)。

如果您愿意,也可以使用线程,但我认为内置cURL方法将是最快,最简单,并且在您现有代码的基础上最易于维护

答案 1 :(得分:0)

在@edhurtig开始的同一思路中,你可能想看一下PHP中的threading。我认为你错误地说了问题。你需要做几个独立的请求,它们不需要是串行的,这在foreach循环中发生。通过为每个请求打开一个新线程,然后并行执行它们,可以进行并行API调用。