How to use the JSON data response via PHP from Google Insight API

161 views
Skip to first unread message

Lucien Dubois

unread,
Jun 23, 2014, 7:28:12 AM6/23/14
to pagespeed-ins...@googlegroups.com
Hi,
I try to use the response from the Google Insights API.
I got

$ch = curl_init("https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=http://code.google.com/speed/page-speed/&key=MyAPIKey");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
$obj = json_decode($res, true);
var_dump($obj); // return NULL ?
echo $obj['pageStats']['numberJsResources']; // doesn't echo anything
print_r($obj['pageStats']['numberJsResources']); 
// doesn't echo anything
But it doesn't return anything. Where am I wrong?

Libo Song

unread,
Jun 23, 2014, 8:42:53 AM6/23/14
to pagespeed-ins...@googlegroups.com
I just copy & pasted your code into my server (replaced the API key), it works for me.
Remove the ['numberJsResources'] from your last statement, here I get:
print_r($obj['pageStats'])
16Array
(
    [numberResources] => 51
    [numberHosts] => 17
    [totalRequestBytes] => 8352
    [numberStaticResources] => 28
    [htmlResponseBytes] => 197612
    [cssResponseBytes] => 129291
    [imageResponseBytes] => 269834
    [javascriptResponseBytes] => 1161486
    [otherResponseBytes] => 166150
    [numberJsResources] => 16
    [numberCssResources] => 5
)



--
You received this message because you are subscribed to the Google Groups "pagespeed-insights-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pagespeed-insights-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pagespeed-insights-discuss/fcfa524b-274b-445a-8975-1a72ca4b21b2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Lucien Dubois

unread,
Jun 23, 2014, 8:52:08 AM6/23/14
to pagespeed-ins...@googlegroups.com
Ok yes! You made me realize it works on web server but not on local server. Thank you.
To unsubscribe from this group and stop receiving emails from it, send an email to pagespeed-insights-discuss+unsub...@googlegroups.com.

Carlos Lizaga Anadon

unread,
Jun 23, 2014, 9:46:41 AM6/23/14
to pagespeed-ins...@googlegroups.com
I doesn't work on local due to same origin policy, here you have some info:


Regards.
Reply all
Reply to author
Forward
0 new messages