Asif
Asif

Reputation: 3

Unable to fetch desired data from Google api through json_decode in PHP

I am unable to fetch desired data of title, score from the json_decode array, I have tried all the ways which are already discussed in stackoverflow. Can anyone help me..

$myKEY = "xyz";
$url_req= 'google api request here';
$results= checkPageSpeed($url_req_d);
$googleapi = json_decode($results,true); 

Google api send the data like this when var_dump($googleapi) and I need to fetch title and score values from the array. Please reply suggested code to extract title and score values i.e "xyz" and "73"

{
 "kind": "pagespeedonline#result",
 "id": "www xyz com/",
 "responseCode": 200,
 "title": "xyz",
 "ruleGroups": {
  "SPEED": {
   "score": 73
  }
 },
 "pageStats": {
  "numberResources": 67,
  "numberHosts": 15,
  "totalRequestBytes": "9354",
  "numberStaticResources": 48,
  "htmlResponseBytes": "129210",
  "textResponseBytes": "5647",
  "cssResponseBytes": "142839",
  "imageResponseBytes": "411466",
  "javascriptResponseBytes": "635453",
  "otherResponseBytes": "94639",
  "numberJsResources": 17,
  "numberCssResources": 6
 },  .........

Upvotes: 0

Views: 27

Answers (1)

Tom Udding
Tom Udding

Reputation: 2294

$googleapi['title'] and $googleapi['ruleGroups']['SPEED']['score'] should do the trick. Check the documentation for more information on how you can access elements from multidimensional arrays.

Upvotes: 1

Related Questions