为PHP创建例外,因此页面不会崩溃

时间:2015-09-11 13:00:57

标签: php web-services error-handling exception-handling crash

我正在尝试为我的网站解决问题。它使用拥有多个订阅服务的第三方。

index页面显示了一些此类服务,但它只是页面的一小部分,但是当连接到第三方的服务器崩溃时,整个网站崩溃了。我使用Web服务从数据库中获取数据,并使用PHP来使用Web服务。

我认为我有正确的例外来捕获任何错误,所以如果Web服务已关闭,它不会导致我的网站崩溃。任何人都可以查看下面的代码,看看我可以做些什么来防止崩溃。

public function getIncomingReservations($outcome="")
{
    $html_code = '';
    $amount = 0;
    $reservationDetails = '';
    $reservationTitle = '';
    $reservationURL = '';
    $reservationDate = '';
    $noreservationsToShow = true;
    try
    {
        $reservations = $this->webService->getAllreservations();
        if(isset($reservations->Hotel))
        {
            if(!is_array($reservations->Hotel))
            {
                $reservations->Hotel = array($reservations->Hotel);
            }
            $html_code = '<dl>';
            foreach($reservations->Hotel as $reservation)
            {
                if($amount >= WebServicesWidgets::$MAX_incoming_reservations)
                {
                    break;
                }
                $reservationDetails = $this->webService->getreservationDetails($reservation->HotelId);
                if($reservationDetails->haseBusiness === true && $reservationDetails->isValidated === true)
                {
                    $noreservationsToShow = false;
                    $reservationTitle = $reservationDetails->shortName;
                    $reservationURL = $this->EWEBSITE_URL . $reservationDetails->HotelId;
                    $reservationDate = $reservationDetails->startDate;
                    $html_code .= '<!-- ' . $reservationDetails->HotelCode . ' | ' . $reservation->HotelId . ' -->';
                    $html_code .= '<dt><a href="'. $reservationURL . '">' . $reservationTitle . '</a></dt>';
                    $html_code .= '<dd>' . date('F j - g:i A', $reservationDate) . '</dd>';
                    ++$amount;
                }
            }
            $html_code .= '</dl>';
        }
        else
        {
            $html_code = WebServicesWidgets::$NO_reservationS;
        }
    }
    catch(Exception $e)
    {
        $html_code = WebServicesWidgets::$FAIL_MSG;
    }
    if($noreservationsToShow)
    {
        $html_code = WebServicesWidgets::$NO_reservationS;
    }
            if(empty($return)){
                echo($html_code);
            }
            else {
                return $html_code;
            }
}

0 个答案:

没有答案