webpagetest
ve-composer-lock
用于与webpagetest.org互动的PHP库。
需要PHP 7.1+
要获取密钥,请参见请求API密钥。
<?php
use WidgetsBurritos WebPageTest WebPageTest ;
$ wpt = new WebPageTest ( ' YOUR_API_KEY ' );指定替代连接处理程序:
<?php
use WidgetsBurritosWebPageTestWebPageTest;
$wpt = new WebPageTest('YOUR_API_KEY', $handler);
指定替代托管实例:
<?php
use WidgetsBurritosWebPageTestWebPageTest;
$wpt = new WebPageTest('YOUR_API_KEY', $handler, 'https://www.example.com');
建议使用外部库,例如茶壶,而不是硬编码状态代码。
<?php
use TeapotStatusCode;
# Examples:
StatusCode::CONTINUING; // 100 (Test Started)
StatusCode::SWITCHING_PROTOCOLS; // 101 (Test Pending)
StatusCode::OK; // 200 (Test Complete)
StatusCode::BAD_REQUEST; // 400 (Test Not Found)
StatusCode::UNAUTHORIZED; // 401 (Test Request Not Found)
StatusCode::PAYMENT_REQUIRED; // 402 (Test Cancelled)
?>
^注意:切换协议和付款所需的状态检查不是错误的。截至2017年10月25日,WebPagetest.org返回待处理测试的“ 101切换协议”状态,并为取消测试返回“ 402付款”状态。
<?php
if ( $ response = $ wpt -> runTest ( ' https://www.google.com ' )) {
if ( $ response -> statusCode == StatusCode:: OK ) {
// All test info is available in $response->data.
$ test_id = $ response -> data -> testId ;
}
}
?>库自动填充k , f和url查询字符串参数。可选地,您可以通过传递数组来提供其他参数。
<?php
$ options = [
' label ' => ' My Test Label ' ,
' noimages ' => 1 ,
' mobile ' => 1 ,
];
if ( $ response = $ wpt -> runTest ( ' https://www.google.com ' , $ options )) {
if ( $ response -> statusCode == StatusCode:: OK ) {
// All test info is available in $response->data.
$ test_id = $ response -> data -> testId ;
}
}
?>有关支持参数的更多信息,请参见网页测试API文档。
<?php
if ( $ response = $ wpt -> getTestStatus ( $ test_id )) {
// All test info is available in $response->data.
if ( $ response -> statusCode == StatusCode:: OK ) {
// Test is complete.
}
else if ( $ response -> statusCode == StatusCode:: CONTINUING ) {
// Test is running.
}
else if ( $ response -> startCode == StatusCode:: SWITCHING_PROTOCOLS ) {
// Test is waiting to start.
}
else if ( $ response -> statusCode == StatusCode:: PAYMENT_REQUIRED ) {
// Test has been cancelled.
else {
// Test failed.
}
}
?> <?php
if ( $ response = $ wpt -> getTestResults ( $ test_id )) {
// All test result info is available in $response->data.
if ( $ response -> statusCode == StatusCode:: OK ) {
// Test is complete.
}
else if ( in_array ( $ response -> statusCode , [StatusCode:: CONTINUING , StatusCode:: SWITCHING_PROTOCOLS ])) {
// Test is not yet complete.
}
else {
// Test failed.
}
}
?> <?php
if ( $ response = $ wpt -> getLocations ()) {
if ( $ response -> statusCode == StatusCode:: OK ) {
// All locations info is available in $response->data.
}
}
?>您可以通过简单运行来取消测试:
<?php
$ wpt -> cancelTest ( $ test_id );
?>