Функция PHP с iCal Parser замедляет время загрузки страницы - PullRequest
0 голосов
/ 15 февраля 2019

У меня есть эта функция, которая использует анализатор iCal от Martin Thoma Ссылка Github Здесь

Эта функция выполняется очень медленно, около 12 секунд, чтобы получить даты событий из календарей iCal.

Как улучшить процесс функции?

Переменная исключения затем выводится в моей загрузке Javascript внизу страницы перед закрывающим тегом body.

I 'Мы пробовали страницу без этой функции, и она загружается очень быстро, поэтому я сузил ее до этой функции.

Возможно, я неправильно использую класс iCal, но я не знаю, как это обойти..

Пожалуйста, помогите

function process_ical($airbnb_ical_link,$google_ical_link,$booking_dot_com_ical_link,$homeaway_ical_link) {

    require 'class.iCalReader.php';

    $exclusions = '"2019-01-01"';

    if ($airbnb_ical_link != '') {

        $ical   = new ICal($airbnb_ical_link);
        $events = $ical->events();

        if (is_array($events) || is_object($events)) {

            foreach ($events as $event) {
                $formatedStart = $ical->iCalDateToUnixTimestamp($event['DTSTART']);
                $formatedEnd = $ical->iCalDateToUnixTimestamp($event['DTEND']);
                $str1 = date('Y-m-d',$formatedStart);
                $str2 = date('Y-m-d',$formatedEnd);
                $date_from = strtotime($str1);
                $date_to = strtotime($str2);
                $date_to = ($date_to - 86400); // Fix the extra day issue
                    for ($i=$date_from; $i<=$date_to; $i+=86400) {
                        $exclusions .= date(',"Y-m-d"', $i);
                    }
            }

        }
    }

    if ($google_ical_link != '') {

        $ical2   = new ICal($google_ical_link);
        $events2 = $ical2->events();

        if (is_array($events2) || is_object($events2)) {

            foreach ($events2 as $event2) {
                $formatedStart2 = $ical2->iCalDateToUnixTimestamp($event2['DTSTART']);
                $formatedEnd2 = $ical2->iCalDateToUnixTimestamp($event2['DTEND']);
                $strr1 = date('Y-m-d',$formatedStart2);
                $strr2 = date('Y-m-d',$formatedEnd2);
                $google_date_from = strtotime($strr1);
                $google_date_to = strtotime($strr2);
                $google_date_to = ($google_date_to - 86400); // Fix the extra day issue
                    for ($i=$google_date_from; $i<=$google_date_to; $i+=86400) {
                        $exclusions .= date(',"Y-m-d"', $i);
                    }
            }

        }

    }

    if ($booking_dot_com_ical_link != '') {

        $ical3   = new ICal($booking_dot_com_ical_link);
        $events3 = $ical3->events();

        if (is_array($events3) || is_object($events3)) {

            foreach ($events3 as $event3) {
                $formatedStart3 = $ical3->iCalDateToUnixTimestamp($event3['DTSTART']);
                $formatedEnd3 = $ical3->iCalDateToUnixTimestamp($event3['DTEND']);
                $str1_priceline = date('Y-m-d',$formatedStart3);
                $str2_priceline = date('Y-m-d',$formatedEnd3);
                $priceline_date_from = strtotime($str1_priceline);
                $priceline_date_to = strtotime($str2_priceline);
                $priceline_date_to = ($priceline_date_to - 86400); // Fix the extra day issue
                    for ($i=$priceline_date_from; $i<=$priceline_date_to; $i+=86400) {
                        $exclusions .= date(',"Y-m-d"', $i);
                    }
            }

        }
    }

    if ($homeaway_ical_link != '') {

        $ical4   = new ICal($homeaway_ical_link);
        $events4 = $ical4->events();

        if (is_array($events4) || is_object($events4)) {

            foreach ($events4 as $event4) {
                $formatedStart4 = $ical4->iCalDateToUnixTimestamp($event4['DTSTART']);
                $formatedEnd4 = $ical4->iCalDateToUnixTimestamp($event4['DTEND']);
                $str1_homeaway = date('Y-m-d',$formatedStart4);
                $str2_homeaway = date('Y-m-d',$formatedEnd4);
                $homeaway_date_from = strtotime($str1_homeaway);
                $homeaway_date_to = strtotime($str2_homeaway);
                $homeaway_date_to = ($homeaway_date_to - 86400); // Fix the extra day issue
                    for ($i=$homeaway_date_from; $i<=$homeaway_date_to; $i+=86400) {
                        $exclusions .= date(',"Y-m-d"', $i);
                    }
            }

        }
    }

    return $exclusions;

}

...