tcopestake / w3capi
此包的最新版本(v1.0.0)没有可用的许可证信息。
W3C验证API的PHP包装器/客户端
v1.0.0
2013-07-07 05:06 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-28 15:00:32 UTC
README
W3C验证API的PHP包装器/客户端
快速免责声明
我不确定实际的W3C API有多稳定,因为它被标记为实验性的并且可能会更改;如果某些功能停止工作,请不要感到惊讶。您可以在此处了解更多信息:http://validator.w3.org/docs/api.html
请注意这个通知
如果您希望对一批文档进行程序调用验证器,请确保您的脚本在请求之间至少休眠 1 秒。标记验证服务对所有人均为免费、公开的服务,您的尊重会受到赞赏。谢谢。
在通过验证器实例进行多次调用时,这会为您自动完成(见下文)。
要求
- PHP 5.3+
- * cURL
- * SimpleXML
* 可以通过提供替代接口来抽象化
安装
此包可以通过手动或Composer安装。
Composer
将以下内容添加到您的项目的 composer.json 中
"repositories": [
{
"type": "vcs",
"url": "https://github.com/tcopestake/w3capi"
}
],
和
"require": {
"tcopestake/w3capi": "*"
},
手动
下载并解压文件。
如果您的项目还没有合适的自动加载器,您可以通过包含 src/bootstrap.php 来使用提供的自动加载器。
include('path/to/src/bootstrap.php');
用法
验证器
验证文档
$validator = new \W3CAPI\Validator;
try {
$validator->validate('http://url.test.com/a-html-page');
} catch(\W3CAPI\Exceptions\SoapClientBadResponse $e) {
echo "Most likely, W3C reported a server error.";
} catch(\W3CAPI\ValidatorUnknownError $e) {
echo "Something went wrong, but we don't know what.";
}
如果成功,可以从实例中检索信息,例如文档是否通过验证
echo ($validator->isValid()) ? 'Valid' : 'Invalid';
以及语义验证错误的信息
if($validator->getErrorCount() > 0) {
foreach($validator->getErrors() as $error) {
/*
* $error will be an array
* containing the error message, etc.
*
*/
}
}
以及验证警告的信息
if($validator->getWarningCount() > 0) {
foreach($validator->getWarnings() as $warning) {
/*
* $warning will be an array
* containing the warning message, etc.
*
*/
}
}
如果您希望验证另一个文档,验证器实例可以轻松重用
try {
$validator->validate('http://another.url.com/shame.css');
} catch(\W3CAPI\Exceptions\SoapClientBadResponse $e) {
echo "Most likely, W3C reported a server error.";
} catch(\W3CAPI\ValidatorUnknownError $e) {
echo "Something went wrong, but we don't know what.";
}
请注意,在后续调用时,验证器类将通过必要时延迟发送请求来强制执行W3C的“1秒”规则。
测试
有单元测试。
可能的开发(?)
- 支持通过POST上传验证文档。
- 更多文档(特别是关于依赖注入的文档)。
- 重新审视XML解析器。
- 在类之间增加更多透明度,例如检索HTTP头的能力
- 同伴的压力迫使我切换到PSR-2,但有时我的肌肉记忆会忘记这一点。