Pagespeed Insights PHP、JSON、CURL 问题?
Pagespeed Insights PHP, JSON, CURL Issue?
我在使用 Pagespeed API 时遇到了一些问题。我正在使用 CURL 来处理 JSON,就像这样:
$ch = curl_init("https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=$url&key=KEY&strategy=mobile");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($ch);
$json = json_decode($curl_response);
可以从 Lighthouse 指标输出信息,即:
echo $json->lighthouseResult->audits->metrics->details->items[0]->firstContentfulPaint;
但是在 Lighthouse 之外使用任何东西我遇到了一个问题,因为很多键都是带连字符的,即:
"lighthouseResult": {
"requestedUrl": "https://codepen.io/",
"finalUrl": "https://codepen.io/",
"lighthouseVersion": "7.1.0",
"userAgent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/88.0.4324.202 Safari/537.36",
"fetchTime": "2021-03-25T12:50:30.855Z",
"environment": {
"networkUserAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4143.7 Safari/537.36 Chrome-Lighthouse",
"hostUserAgent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/88.0.4324.202 Safari/537.36",
"benchmarkIndex": 993
},
"runWarnings": [],
"configSettings": {
"emulatedFormFactor": "desktop",
"formFactor": "desktop",
"locale": "en-US",
"onlyCategories": [
"performance"
],
"channel": "lr"
},
"audits": {
"uses-rel-preload": {
"id": "uses-rel-preload",
"title": "Preload key requests",
"description": "Consider using `\u003clink rel=preload\u003e` to prioritize fetching resources that are currently requested later in page load. [Learn more](https://web.dev/uses-rel-preload/).",
"score": 0.48,
"scoreDisplayMode": "numeric",
"displayValue": "Potential savings of 950 ms",
"details": {
"type": "opportunity",
"items": [
{
"wastedMs": 950,
"url": "https://codepen.io/assets/packs/css/Homepage-Anon-8d383add.chunk.css"
},
{
"url": "https://cpwebassets.codepen.io/assets/packs/css/Homepage-Anon-8d383add.chunk.css",
"wastedMs": 500
}
],
"overallSavingsMs": 950,
"headings": [
{
"label": "URL",
"valueType": "url",
"key": "url"
},
{
"label": "Potential Savings",
"key": "wastedMs",
"valueType": "timespanMs"
}
]
},
"numericValue": 950
},
"uses-responsive-images": {
"id": "uses-responsive-images",
"title": "Properly size images",
"description": "Serve images that are appropriately-sized to save cellular data and improve load time. [Learn more](https://web.dev/uses-responsive-images/).",
"score": 0.65,
"scoreDisplayMode": "numeric",
"displayValue": "Potential savings of 611 KiB",
"details": {
"items": [
{
"totalBytes": 218372,
"wastedPercent": 72.994829780464528,
"url": "https://cpwebassets.codepen.io/assets/packs/editor-support-9cc33b6268c0a6c897033028084e1851.png",
"wastedBytes": 159400
},
{
"wastedBytes": 129352,
"wastedPercent": 72.994829780464528,
"totalBytes": 177207,
"url": "https://cpwebassets.codepen.io/assets/packs/editor-assets-6a081a7c8ff72da526846f3c8f8ae91c.png"
},
{
"totalBytes": 133650,
"wastedBytes": 87951,
"wastedPercent": 65.807290055077658,
"url": "https://cpwebassets.codepen.io/assets/packs/editor-embed-eae2ee445ef0f63ee8fc7f175858e9d9.png"
},
{
"url": "https://cpwebassets.codepen.io/assets/packs/editor-projects-a0d6a9e16cf1c2c46219ef3d49370823.png",
"wastedBytes": 83946,
"wastedPercent": 65.795548786132358,
"totalBytes": 127586
},
{
"wastedBytes": 73120,
"wastedPercent": 72.994829780464528,
"totalBytes": 100172,
"url": "https://cpwebassets.codepen.io/assets/packs/editor-privacy-e542bc22ce324502eb727577083eeb61.png"
},
{
"url": "https://cpwebassets.codepen.io/assets/packs/greensock-logo-9db91ff4c4a50faa1690d4f3d7ffca6e.png",
"wastedPercent": 93.017321373337452,
"wastedBytes": 50970,
"totalBytes": 54796
},
{
"wastedBytes": 34189,
"wastedPercent": 65.783751672863872,
"url": "https://cpwebassets.codepen.io/assets/packs/editor-collab-90815f9b83fcbdbd3ef7df4573ed9b57.png",
"totalBytes": 51972
},
{
"wastedPercent": 75,
"url": "https://cdn4.buysellads.net/uu/1/82455/1609889978-aws_carbon.jpg",
"totalBytes": 9457,
"wastedBytes": 7093
}
],
"type": "opportunity",
"overallSavingsBytes": 626021,
"overallSavingsMs": 480,
"headings": [
{
"key": "url",
"valueType": "thumbnail"
},
{
"valueType": "url",
"label": "URL",
"key": "url"
},
{
"key": "totalBytes",
"valueType": "bytes",
"label": "Resource Size"
},
{
"label": "Potential Savings",
"key": "wastedBytes",
"valueType": "bytes"
}
]
},
"numericValue": 480
},
},
}
我试过像这样更改查询:
$json->lighthouseResult->audits['uses-responsive-images']->displayValue;
$json->lighthouseResult->audits->['uses-responsive-images']->displayValue;
但无法得到结果或只是得到 PHP 错误。我可能错过了一些简单的东西,但我一辈子都找不到。
示例略有设计...但它展示了如何使用名称中的连字符来寻址对象。
//build a object with
$o = new stdClass();
$o->aa = new stdClass();
$o->aa->{'z-z-z'} = new stdClass();
$o->aa->{'z-z-z'}->bb = new stdClass();
$o->aa->{'z-z-z'}->bb->count = 100;
// check what it actually looks like
print_r($o);
// Address the count
echo $o->aa->{'z-z-z'}->bb->count;
结果
stdClass Object
(
[aa] => stdClass Object
(
[z-z-z] => stdClass Object
(
[bb] => stdClass Object
(
[count] => 100
)
)
)
)
100
仔细查看你的数据,我认为你迷路了,试试
$json->lighthouseResult
->{'uses-responsive-images'}
->{'uses-rel-preload'}
->displayValue
我在使用 Pagespeed API 时遇到了一些问题。我正在使用 CURL 来处理 JSON,就像这样:
$ch = curl_init("https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=$url&key=KEY&strategy=mobile");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($ch);
$json = json_decode($curl_response);
可以从 Lighthouse 指标输出信息,即:
echo $json->lighthouseResult->audits->metrics->details->items[0]->firstContentfulPaint;
但是在 Lighthouse 之外使用任何东西我遇到了一个问题,因为很多键都是带连字符的,即:
"lighthouseResult": {
"requestedUrl": "https://codepen.io/",
"finalUrl": "https://codepen.io/",
"lighthouseVersion": "7.1.0",
"userAgent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/88.0.4324.202 Safari/537.36",
"fetchTime": "2021-03-25T12:50:30.855Z",
"environment": {
"networkUserAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4143.7 Safari/537.36 Chrome-Lighthouse",
"hostUserAgent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/88.0.4324.202 Safari/537.36",
"benchmarkIndex": 993
},
"runWarnings": [],
"configSettings": {
"emulatedFormFactor": "desktop",
"formFactor": "desktop",
"locale": "en-US",
"onlyCategories": [
"performance"
],
"channel": "lr"
},
"audits": {
"uses-rel-preload": {
"id": "uses-rel-preload",
"title": "Preload key requests",
"description": "Consider using `\u003clink rel=preload\u003e` to prioritize fetching resources that are currently requested later in page load. [Learn more](https://web.dev/uses-rel-preload/).",
"score": 0.48,
"scoreDisplayMode": "numeric",
"displayValue": "Potential savings of 950 ms",
"details": {
"type": "opportunity",
"items": [
{
"wastedMs": 950,
"url": "https://codepen.io/assets/packs/css/Homepage-Anon-8d383add.chunk.css"
},
{
"url": "https://cpwebassets.codepen.io/assets/packs/css/Homepage-Anon-8d383add.chunk.css",
"wastedMs": 500
}
],
"overallSavingsMs": 950,
"headings": [
{
"label": "URL",
"valueType": "url",
"key": "url"
},
{
"label": "Potential Savings",
"key": "wastedMs",
"valueType": "timespanMs"
}
]
},
"numericValue": 950
},
"uses-responsive-images": {
"id": "uses-responsive-images",
"title": "Properly size images",
"description": "Serve images that are appropriately-sized to save cellular data and improve load time. [Learn more](https://web.dev/uses-responsive-images/).",
"score": 0.65,
"scoreDisplayMode": "numeric",
"displayValue": "Potential savings of 611 KiB",
"details": {
"items": [
{
"totalBytes": 218372,
"wastedPercent": 72.994829780464528,
"url": "https://cpwebassets.codepen.io/assets/packs/editor-support-9cc33b6268c0a6c897033028084e1851.png",
"wastedBytes": 159400
},
{
"wastedBytes": 129352,
"wastedPercent": 72.994829780464528,
"totalBytes": 177207,
"url": "https://cpwebassets.codepen.io/assets/packs/editor-assets-6a081a7c8ff72da526846f3c8f8ae91c.png"
},
{
"totalBytes": 133650,
"wastedBytes": 87951,
"wastedPercent": 65.807290055077658,
"url": "https://cpwebassets.codepen.io/assets/packs/editor-embed-eae2ee445ef0f63ee8fc7f175858e9d9.png"
},
{
"url": "https://cpwebassets.codepen.io/assets/packs/editor-projects-a0d6a9e16cf1c2c46219ef3d49370823.png",
"wastedBytes": 83946,
"wastedPercent": 65.795548786132358,
"totalBytes": 127586
},
{
"wastedBytes": 73120,
"wastedPercent": 72.994829780464528,
"totalBytes": 100172,
"url": "https://cpwebassets.codepen.io/assets/packs/editor-privacy-e542bc22ce324502eb727577083eeb61.png"
},
{
"url": "https://cpwebassets.codepen.io/assets/packs/greensock-logo-9db91ff4c4a50faa1690d4f3d7ffca6e.png",
"wastedPercent": 93.017321373337452,
"wastedBytes": 50970,
"totalBytes": 54796
},
{
"wastedBytes": 34189,
"wastedPercent": 65.783751672863872,
"url": "https://cpwebassets.codepen.io/assets/packs/editor-collab-90815f9b83fcbdbd3ef7df4573ed9b57.png",
"totalBytes": 51972
},
{
"wastedPercent": 75,
"url": "https://cdn4.buysellads.net/uu/1/82455/1609889978-aws_carbon.jpg",
"totalBytes": 9457,
"wastedBytes": 7093
}
],
"type": "opportunity",
"overallSavingsBytes": 626021,
"overallSavingsMs": 480,
"headings": [
{
"key": "url",
"valueType": "thumbnail"
},
{
"valueType": "url",
"label": "URL",
"key": "url"
},
{
"key": "totalBytes",
"valueType": "bytes",
"label": "Resource Size"
},
{
"label": "Potential Savings",
"key": "wastedBytes",
"valueType": "bytes"
}
]
},
"numericValue": 480
},
},
}
我试过像这样更改查询:
$json->lighthouseResult->audits['uses-responsive-images']->displayValue;
$json->lighthouseResult->audits->['uses-responsive-images']->displayValue;
但无法得到结果或只是得到 PHP 错误。我可能错过了一些简单的东西,但我一辈子都找不到。
示例略有设计...但它展示了如何使用名称中的连字符来寻址对象。
//build a object with
$o = new stdClass();
$o->aa = new stdClass();
$o->aa->{'z-z-z'} = new stdClass();
$o->aa->{'z-z-z'}->bb = new stdClass();
$o->aa->{'z-z-z'}->bb->count = 100;
// check what it actually looks like
print_r($o);
// Address the count
echo $o->aa->{'z-z-z'}->bb->count;
结果
stdClass Object
(
[aa] => stdClass Object
(
[z-z-z] => stdClass Object
(
[bb] => stdClass Object
(
[count] => 100
)
)
)
)
100
仔细查看你的数据,我认为你迷路了,试试
$json->lighthouseResult
->{'uses-responsive-images'}
->{'uses-rel-preload'}
->displayValue