dsentker/phpinsights

该包的最新版本(0.2.2)没有可用的许可证信息。

Google PageSpeed Insights 的 PHP 包装器

0.2.2 2017-12-06 09:47 UTC

This package is auto-updated.

Last update: 2024-09-12 04:17:10 UTC


README

这是一个用于Google PageSpeed Insights的简单易用的 API 包装器。JSON 响应被映射到对象,以便无痛苦地使用。

安装

  1. 从 Google 开发者控制台获取Page Speed Insights的 API 密钥。
  2. composer require dsentker/phpinsights
  3. 享受这个库带来的乐趣。

使用方法

简单使用

$url = 'http://example.com';

$caller = new \PhpInsights\InsightsCaller('your-google-api-key-here', 'de');
$response = $caller->getResponse($url, \PhpInsights\InsightsCaller::STRATEGY_MOBILE);
$result = $response->getMappedResult();

var_dump($result->getSpeedScore()); // 100 
var_dump($result->getUsabilityScore()); // 100 

使用并发请求

$urls = array(
    'http://example.com', 
    'http://example2.com', 
    'http://example3.com'
);

$caller = new \PhpInsights\InsightsCaller('your-google-api-key-here', 'fr');
$responses = $caller->getResponses($urls, \PhpInsights\InsightsCaller::STRATEGY_MOBILE);

foreach ($responses as $url => $response) {
    $result = $response->getMappedResult();

    var_dump($result->getSpeedScore()); // 100 
    var_dump($result->getUsabilityScore()); // 100 
}

结果详情

完整结果

/** @var \PhpInsights\Result\InsightsResult $result */
foreach($result->getFormattedResults()->getRuleResults() as $rule => $ruleResult) {
    
    /*
     * If the rule impact is zero, it means that the website has passed the test.
     */
    if($ruleResult->getRuleImpact() > 0) {
    
        var_dump($rule); // AvoidLandingPageRedirects
        var_dump($ruleResult->getLocalizedRuleName()); // "Zielseiten-Weiterleitungen vermeiden"
        
        /*
         * The getDetails() method is a wrapper to get the `summary` field as well as `Urlblocks` data. You
         * can use $ruleResult->getUrlBlocks() and $ruleResult->getSummary() instead. 
         */
        foreach($ruleResult->getDetails() as $block) {
            var_dump($block->toString()); // "Auf Ihrer Seite sind keine Weiterleitungen vorhanden"
        }
    
    }
    
}

按规则组查看结果详情

/** @var \PhpInsights\Result\InsightsResult $result */
foreach($result->getFormattedResults()->getRuleResultsByGroup(RuleGroup::GROUP_SPEED) as $rule => $ruleResult) {
    $ruleResult->getSummary()->toString();
}

截图

print $result->screenshot->getImageHtml(); // html image element
print $result->screenshot->getData(); // base64 screenshot representation 

测试

$ phpunit --bootstrap "path/to/phpinsights/src/autoload.php"

鸣谢

提交错误和功能请求

错误和功能请求在 GitHub 上跟踪。

待办事项

  • 编写更多测试
  • 提高我的英语水平

外部库

此库依赖于cweiske 的 JsonMapper,用于将 JSON 字段映射到 PHP 对象,以及Guzzle(惊喜!)。

版权和许可

PhpInsights 在 MIT 许可证(MIT)下授权使用。请参阅 LICENSE 以获取更多信息。