sustainableweb/website-carbon-calculator
Composer 安装命令:
composer require sustainableweb/website-carbon-calculator
包简介
Lightweight and framework-agnostic PHP library to calculate web page carbon emissions based on real-time parameters. No external API required — ideal for custom integrations, CLI tools, or backend automation.
README 文档
README
The internet consumes a lot of electricity. 1021 TWh per year to be precise. To give you some perspective, that’s more than the entire United Kingdom.
From data centers to transmission networks to the billions of connected devices that we hold in our hands, it is all consuming electricity, and in turn producing carbon emissions equal to or greater than the global aviation industry. Yikes!
The Website Carbon Calculator empowers website owners to assess and minimize their carbon footprint. Drawing inspiration from the acclaimed Website Carbon Calculator algorithm 2.0 and leveraging The Green Web Foundation's co2.js, it allows you to calculate the CO₂ impact and performance of any url.
Installation
$ composer require sustainableweb/website-carbon-calculator
API
Estimate web page carbon footprint and performance
View the Google documentation to generate a Google Pagespeed Api Key
use SustainableWeb\WebsiteCarbonCalculator\WebsiteCarbonCalculator; $websiteCarbonCalculator = new WebsiteCarbonCalculator('GooglePagespeedApiKey'); $websiteCarbonCalculator->calculateByURL('https://www.websitecarbon.com/how-does-it-work/', ['strategy'=>'desktop'])
Output
{
"url": "https://www.websitecarbon.com",
"isGreenHost": true,
"bytesTransferred": 135289,
"networkRequests": 16,
"performanceScore": 0.94,
"loadingExperience": "FAST",
"domSize": 308,
"speedIndex": 934,
"firstMeaningfulPaint": 491,
"interactive": 692,
"bootupTime": 94,
"serverResponseTime": 150,
"mainthreadWork": 762,
"energy": 0.000171,
"co2PerPageview": 0.0739
}
For better performance, detect hosting energy type and store it in a database to avoid repetitive call to The Green Web Foundation API.
Detect hosting energy type
use SustainableWeb\WebsiteCarbonCalculator\WebsiteCarbonCalculator; $isGreenHost = WebsiteCarbonCalculator::isGreenHost('https://www.websitecarbon.com') $websiteCarbonCalculator->calculateByURL('https://www.websitecarbon.com/how-does-it-work/', ['isGreenHost'=>$isGreenHost, 'strategy'=>'desktop'])
How does it work
Calculating the carbon emissions of website is somewhat of a challenge, but using five key pieces of data we can make a pretty good estimate:
- Data transfer over the wire
- Energy intensity of web data
- Energy source used by the data center
- Carbon intensity of electricity
- Website traffic
Under the hood, it uses Google pagespeed api and The green web foundation api
Website Carbon Calculator
This project is widely inspired by the Website Carbon Calculator algorithm 2.0 and The Green Web Foundation co2.js
Glossary
- strategy : mobile/desktop
- isGreenHost : server uses sustainable energy
- networkRequests : https://web.dev/performance-scoring/
- performanceScore : https://web.dev/resource-summary/
- domSize : https://web.dev/dom-size/
- speedIndex : https://web.dev/speed-index/
- firstContentfulPaint : https://web.dev/articles/fcp
- largestContentfulPaint : https://web.dev/articles/lcp
- interactive : https://web.dev/interactive/
- bootupTime : https://web.dev/bootup-time/
- serverResponseTime : https://web.dev/time-to-first-byte/
- mainthreadWork : https://web.dev/mainthread-work-breakdown/
- co2PerPageview : estimated grams of CO2 produced every time someone visits the web page
Tests
Run tests
$ pagespeedApiKey=xxxxxx php vendor/bin/phpunit
Changelog
2.0.1
- remove warnings thrown when Google API response is not complete
sustainableweb/website-carbon-calculator 适用场景与选型建议
sustainableweb/website-carbon-calculator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 9, 最近一次更新时间为 2025 年 05 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 sustainableweb/website-carbon-calculator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sustainableweb/website-carbon-calculator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 19
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-05-30