Perpustakaan PHP untuk berinteraksi dengan webpagetest.org.
Membutuhkan PHP 7.1+
Untuk mendapatkan kunci, lihat Kunci API Permintaan.
<?php
use WidgetsBurritos WebPageTest WebPageTest ;
$ wpt = new WebPageTest ( ' YOUR_API_KEY ' );Untuk menentukan penangan koneksi alternatif:
<?php
use WidgetsBurritosWebPageTestWebPageTest;
$wpt = new WebPageTest('YOUR_API_KEY', $handler);
Untuk menentukan contoh hosting alternatif:
<?php
use WidgetsBurritosWebPageTestWebPageTest;
$wpt = new WebPageTest('YOUR_API_KEY', $handler, 'https://www.example.com');
Dianjurkan untuk menggunakan pustaka eksternal, seperti teko alih -alih kode status hardcoding.
<?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)
?>
^ Catatan: Protokol switching dan status pembayaran yang diperlukan cek bukanlah kesalahan. Pada 10/25/2017, webpagetest.org mengembalikan status "101 protokol switching" untuk tes yang tertunda dan status "402 pembayaran yang diperlukan" untuk tes yang dibatalkan.
<?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 ;
}
}
?> Perpustakaan secara otomatis mengisi parameter string Kueri k , f dan url . Secara opsional, Anda dapat menyediakan parameter tambahan dengan melewati array.
<?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 ;
}
}
?>Lihat dokumentasi API Tes Halaman Web untuk informasi lebih lanjut tentang parameter yang didukung.
<?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.
}
}
?>Anda dapat membatalkan tes hanya dengan menjalankan:
<?php
$ wpt -> cancelTest ( $ test_id );
?>