// 设置日志级别为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响应码判断服务是否正常,方便实现自动化监控。