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 );
?>