Skip to content
Advertisement

Pagespeed Insights PHP, JSON, CURL Issue?

I’m having a few issues with the Pagespeed API. I’m using CURL to handle the JSON like so:

$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);

Outputting the info is fine from the Lighthouse metrics, ie:

echo $json->lighthouseResult->audits->metrics->details->items[0]->firstContentfulPaint;

But using anything outside Lighthouse I’m having an issue as many keys are hyphenated, ie:

  "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=preloadu003e` 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
            },
           
          },
        
        }

I’ve tried altering the query like:

$json->lighthouseResult->audits['uses-responsive-images']->displayValue;

$json->lighthouseResult->audits->['uses-responsive-images']->displayValue;

But can’t get a result or just get a PHP error. I’m probably missing something easy but for the life of me I can’t find it.

Advertisement

Answer

Slightly contrived example…but it shows how to address an object with hyphens in the name.

//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;

RESULT

stdClass Object
(
    [aa] => stdClass Object
        (
            [z-z-z] => stdClass Object
                (
                    [bb] => stdClass Object
                        (
                            [count] => 100
                        )
                )
        )
)
100

And looking closer at your data i think you got lost somewhere, try

$json->lighthouseResult
    ->{'uses-responsive-images'}
    ->{'uses-rel-preload'}
    ->displayValue
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement