// 設置日誌級別為E_ALL,報告所有錯誤和警告
error_reporting(E_ALL);
這樣PHP將會捕獲並記錄所有錯誤和警告信息。
// 將錯誤信息寫入指定日誌文件
error_log("Error: Something went wrong!", 3, "/path/to/logfile.log");
上述代碼將錯誤信息記錄到路徑為/path/to/logfile.log的日誌文件中。
class CustomSoapServer extends SoapServer {
public function __doRequest($request, $location, $action, $version, $one_way = 0) {
try {
// 服務具體業務邏輯
// ...
throw new SoapFault('Server', 'Something went wrong!');
} catch (SoapFault $fault) {
// 記錄錯誤日誌,或發送告警
error_log($fault->getMessage());
// 返回自定義錯誤響應
return $this->fault($fault->getCode(), $fault->getMessage());
}
}
}
// 創建自定義SOAP服務對象
$server = new CustomSoapServer("wsdlFile.wsdl");
通過這種方式,可以捕獲服務中的異常並將其記錄,方便後續跟踪和處理。
function checkWebService($url) {
$timeout = 10; // 設置超時時間為10秒
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, $timeout);
$response = curl_exec($handle);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
curl_close($handle);
}
// 定期檢測服務狀態
if (checkWebService(" http://example.com/webservice ")) {
echo "Web service is running fine.";
} else {
echo "Web service is down.";
}
此代碼通過獲取HTTP響應碼判斷服務是否正常,方便實現自動化監控。