wtiservice/wti-service

v2.0.1 2023-09-07 10:27 UTC

This package is not auto-updated.

Last update: 2024-09-20 12:43:34 UTC


README

WpiService 类是一个PHP库,允许您使用Google PageSpeed Insights API检查网站的性能。此库提供了一种方便的方式来分析网站的性能指标和检索特定的性能数据。

安装

要在您的PHP项目中使用此库,请按照以下步骤操作

  1. 如果尚未安装,请使用Composer安装Guzzle HTTP客户端

    composer require guzzlehttp/guzzle
  2. 在您的项目中安装 WpiService 包。

    composer require wpiservice/wpi-service
  3. 在您的代码中创建 WpiService 类的一个实例。

use wpiservice\WpiService\WpiService;

$wpiService = new WpiService();

使用方法

性能分析

您可以使用 Performance 方法分析网站的性能。此方法需要两个参数:要分析的网站的URL以及用于认证和访问API的Google PageSpeed API密钥。

$url = 'https://example.com';
$apiKey = 'your-api-key';

$result = $wpiService->Performance($url, $apiKey);

if (isset($result['error'])) {
    // Handle the error.
} else {
    // Use the performance data.
}

获取特定性能指标

您还可以使用 PerformanceMetric 方法从Google PageSpeed Insights数据中获取特定的性能指标。此方法需要两个参数:从API获取的性能数据以及要检索的特定性能指标名称。

$performanceData = $wpiService->Performance($url, $apiKey);
$metricName = 'first-contentful-paint';

$metricData = $wpiService->PerformanceMetric($performanceData, $metricName);

if (isset($metricData['error'])) {
    // Handle the error.
} else {
    // Use the specific performance metric data.
}

$metricName 的有效值包括

  • 'first-contentful-paint'
  • 'largest-contentful-paint'
  • 'total-blocking-time'
  • 'cumulative-layout-shift'

错误处理

PerformancePerformanceMetric 方法都返回一个包含性能数据或错误信息的关联数组。如果请求或指标检索失败,您应检查返回数据中是否存在 'error' 键以优雅地处理错误。

许可证

此库根据MIT许可证授权。

贡献

欢迎贡献!如果您想为此项目做出贡献,请打开一个问题或提交一个拉取请求。

请随意自定义此README,包括您的姓名、联系方式和任何特定于您项目的其他详细信息。提供清晰和全面的文档将使其他人更容易使用您的PHP类。