webpagetest
ve-composer-lock
ไลบรารี PHP สำหรับการโต้ตอบกับ webpagetest.org
ต้องใช้ 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');
ขอแนะนำให้ใช้ไลบรารีภายนอกเช่นกาน้ำชาแทนรหัสสถานะ 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)
?>
^ หมายเหตุ: โปรโตคอลการสลับและการตรวจสอบสถานะที่จำเป็นต้องใช้ไม่ใช่ความผิดพลาด ณ วันที่ 10/25/2017, WebPagetest.org ส่งคืนสถานะ "101 Switching Protocols" สำหรับการทดสอบที่รอดำเนินการและสถานะ "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 );
?>