2014-07-06 4 views
0

Donc, je suis venu à un léger problème dans la formulation de cette réponse api. Je suis en train de phrase les données de capture d'écran de cette réponse api? ..Phrasing réponse JSON en PHP

J'ai essayé le bas, mais en vain! .. Il semble juste être NULL? .. toute aide serait un grand merci

$jsonData = json_decode($response, true); 
$testt = $jsonData->screenshot->data; 

la réponse:

{ 
"kind": "pagespeedonline#result", 
"id": "http://stackoverflow.com/", 
"responseCode": 200, 
"title": "Stack Overflow", 
"score": 78, 
"pageStats": { 
    "numberResources": 31, 
    "numberHosts": 11, 
    "totalRequestBytes": "4412", 
    "numberStaticResources": 22, 
}, 
"formattedResults": { 
    "locale": "en_US", 
    "ruleResults": { 
    "AvoidLandingPageRedirects": { 
    "localizedRuleName": "Avoid landing page redirects", 
    "ruleImpact": 0.0, 
    "urlBlocks": [ 
    { 
     "header": { 
     "format": "Your page has no redirects. Learn more about avoiding landing page redirects.", 
     "args": [ 
     { 
     "type": "HYPERLINK", 
     "value": "https://developers.google.com/speed/docs/insights/AvoidRedirects" 
     } 
     ] 
     } 
    } 
    ] 
    }, 
    "EnableGzipCompression": { 
    "localizedRuleName": "Enable compression", 
    "ruleImpact": 0.0, 
    "urlBlocks": [ 
    { 
     "header": { 
     "format": "You have compression enabled. Learn more about enabling compression.", 
     "args": [ 
     { 
     "type": "HYPERLINK", 
     "value": "https://developers.google.com/speed/docs/insights/EnableCompression" 
     } 
     ] 
     } 
    } 
    ] 
    }, 
    "LeverageBrowserCaching": { 
    "localizedRuleName": "Leverage browser caching", 
    "ruleImpact": 0.0, 
    "urlBlocks": [ 
    { 
     "header": { 
     "format": "You have enabled browser caching. Learn more about browser caching recommendations.", 
     "args": [ 
     { 
     "type": "HYPERLINK" 
     } 
     ] 
     } 
    } 
    ] 
    }, 
    "MainResourceServerResponseTime": { 
    "localizedRuleName": "Reduce server response time", 
    "ruleImpact": 0.0, 
    "urlBlocks": [ 
    { 
     "header": { 
     "format": "Your server responded quickly. Learn more about server response time optimization.", 
     "args": [ 
     { 
     "type": "HYPERLINK", 
     "value": "https://developers.google.com/speed/docs/insights/Server" 
     } 
     ] 
     } 
    } 
    ] 
    }, 
    "MinifyCss": { 
    "localizedRuleName": "Minify CSS", 
    "ruleImpact": 0.0, 
    "urlBlocks": [ 
    { 
     "header": { 
     "format": "Your CSS is minified. Learn more about minifying CSS.", 
     "args": [ 
     { 
     "value": "https://developers.google.com/speed/docs/insights/MinifyResources" 
     } 
     ] 
     } 
    } 
    ] 
    }, 
    "MinifyHTML": { 
    "ruleImpact": 0.1443, 
    "urlBlocks": [ 
    { 
     "header": { 
     "format": "Compacting HTML code, including any inline JavaScript and CSS contained in it, can save many bytes of data and speed up download and parse times." 
     } 
    }, 
    { 
     "header": { 
     "format": "Minify HTML for the following resources to reduce their size by $2 ($3 reduction).", 
     "args": [ 
     { 
     "value": "https://developers.google.com/speed/docs/insights/MinifyResources" 
     }, 
     { 
     "value": "1.4KiB" 
     }, 
     { 
     "value": "5%" 
     } 
     ] 
     }, 
     "urls": [ 
     { 
     "result": { 
     "format": "Minifying $1 could save $2 ($3 reduction) after compression.", 
     "args": [ 
      { 
      "type": "URL", 
      "value": "http://stackoverflow.com/" 
      }, 
      { 
      "type": "BYTES", 
      "value": "1.4KiB" 
      }, 
      { 
      "value": "5%" 
      } 
     ] 
     } 
     } 
     ] 
    } 
    ] 
    }, 
    "MinifyJavaScript": { 
    "localizedRuleName": "Minify JavaScript", 
    "ruleImpact": 0.0, 
    "urlBlocks": [ 
    { 
     "header": { 
     "format": "Your JavaScript content is minified. Learn more about minifying JavaScript.", 
     "args": [ 
     { 
     "type": "HYPERLINK", 
     "value": "https://developers.google.com/speed/docs/insights/MinifyResources" 
     } 
     ] 
     } 
    } 
    ] 
    }, 
    "MinimizeRenderBlockingResources": { 
    "localizedRuleName": "Eliminate render-blocking JavaScript and CSS in above-the-fold content", 
    "urlBlocks": [ 
    { 
     "header": { 
     "args": [ 
     { 
     "type": "INT_LITERAL", 
     "value": "2" 
     }, 
     { 
     "type": "INT_LITERAL", 
     "value": "3" 
     } 
     ] 
     } 
    }, 
    { 
     "header": { 
     "format": "None of the above-the-fold content on your page could be rendered without waiting for the following resources to load. Try to defer or asynchronously load blocking resources, or inline the critical portions of those resources directly in the HTML." 
     } 
    }, 
    { 
     "header": { 
     "args": [ 
     { 
     "type": "HYPERLINK", 
     "value": "https://developers.google.com/speed/docs/insights/BlockingJS" 
     } 
     ] 
     }, 
     "urls": [ 
     { 
     "result": { 
     "format": "$1", 
     "args": [ 
      { 
      "type": "URL", 
      "value": "http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" 
      } 
     ] 
     } 
     }, 
     { 
     "result": { 
     "format": "$1", 
     "args": [ 
      { 
      "type": "URL", 
      "value": "http://cdn.sstatic.net/Js/stub.en.js?v=174f553de1d0" 
      } 
     ] 
     } 
     } 
     ] 
    }, 
    { 
     "header": { 
     "format": "Optimize CSS Delivery of the following:", 
     "args": [ 
     { 
     "type": "HYPERLINK", 
     "value": "https://developers.google.com/speed/docs/insights/OptimizeCSSDelivery" 
     } 
     ] 
     }, 
     "urls": [ 
     { 
     "result": { 
     "format": "$1", 
     "args": [ 
      { 
      "type": "URL", 
      "value": "http://cdn.sstatic.net/stackoverflow/all.css?v=55bbe6171ff2" 
      } 
     ] 
     } 
     }, 
     { 
     "result": { 
     "format": "$1", 
     "args": [ 
      { 
      "type": "URL", 
      "value": "http://cdn-careers.sstatic.net/careers/gethired/company.min.css?v=86ee8c8bfa3e" 
      } 
     ] 
     } 
     }, 
     { 
     "result": { 
     "format": "$1", 
     "args": [ 
      { 
      "type": "URL", 
      "value": "http://cdn-careers.sstatic.net/careers/gethired/sidebar.min.css?v=88f3fd5e2263" 
      } 
     ] 
     } 
     } 
     ] 
    } 
    ] 
    }, 
    "OptimizeImages": { 
    "localizedRuleName": "Optimize images", 
    "ruleImpact": 3.0802000000000005, 
    "urlBlocks": [ 
    { 
     "header": { 
     "format": "Properly formatting and compressing images can save many bytes of data." 
     } 
    }, 
    { 
     "header": { 
     "format": "Optimize the following images to reduce their size by $2 ($3 reduction).", 
     "args": [ 
     { 
     "type": "HYPERLINK", 
     "value": "https://developers.google.com/speed/docs/insights/OptimizeImages" 
     }, 
     { 
     "type": "BYTES", 
     "value": "30KiB" 
     }, 
     { 
     "type": "PERCENTAGE", 
     "value": "42%" 
     } 
     ] 
     }, 
     "urls": [ 
     { 
     "result": { 
     "format": "Losslessly compressing $1 could save $2 ($3 reduction).", 
     "args": [ 
      { 
      "type": "URL", 
      "value": "http://cdn-careers.sstatic.net/careers/gethired/img/careers4-ad-header-so-crop.png?v=f4ca4345dc81" 
      }, 
      { 
      "type": "BYTES", 
      "value": "16.6KiB" 
      }, 
      { 
      "type": "PERCENTAGE", 
      "value": "73%" 
      } 
     ] 
     } 
     }, 
     { 
     "result": { 
     "format": "Losslessly compressing $1 could save $2 ($3 reduction).", 
     "args": [ 
      { 
      "type": "URL", 
      "value": "http://cdn.sstatic.net/img/share-sprite-new.png?v=204b1e0e421b" 
      }, 
      { 
      "type": "BYTES", 
      "value": "2.9KiB" 
      }, 
      { 
      "type": "PERCENTAGE", 
      "value": "19%" 
      } 
     ] 
     } 
     }, 
     { 
     "result": { 
     "format": "Losslessly compressing $1 could save $2 ($3 reduction).", 
     "args": [ 
      { 
      "type": "URL", 
      "value": "http://cdn.sstatic.net/Img/sprite-herobox.png?v=d173774f3a9f" 
      }, 
      { 
      "type": "BYTES", 
      "value": "2.8KiB" 
      }, 
      { 
      "type": "PERCENTAGE", 
      "value": "43%" 
      } 
     ] 
     } 
     }, 
     { 
     "result": { 
     "format": "Losslessly compressing $1 could save $2 ($3 reduction).", 
     "args": [ 
      { 
      "type": "URL", 
      "value": "http://i.stack.imgur.com/Tfe0K.png" 
      }, 
      { 
      "type": "BYTES", 
      "value": "2.6KiB" 
      }, 
      { 
      "type": "PERCENTAGE", 
      "value": "76%" 
      } 
     ] 
     } 
     }, 
     { 
     "result": { 
     "format": "Losslessly compressing $1 could save $2 ($3 reduction).", 
     "args": [ 
      { 
      "type": "URL", 
      "value": "http://i.stack.imgur.com/vobok.png" 
      }, 
      { 
      "type": "BYTES", 
      "value": "2.6KiB" 
      }, 
      { 
      "type": "PERCENTAGE", 
      "value": "76%" 
      } 
     ] 
     } 
     }, 
     { 
     "result": { 
     "format": "Losslessly compressing $1 could save $2 ($3 reduction).", 
     "args": [ 
      { 
      "type": "URL", 
      "value": "http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=3c6263c3453b" 
      }, 
      { 
      "type": "BYTES", 
      "value": "1.5KiB" 
      }, 
      { 
      "type": "PERCENTAGE", 
      "value": "9%" 
      } 
     ] 
     } 
     }, 
     { 
     "result": { 
     "format": "Losslessly compressing $1 could save $2 ($3 reduction).", 
     "args": [ 
      { 
      "type": "URL", 
      "value": "http://i.stack.imgur.com/G1dzB.png" 
      }, 
      { 
      "type": "BYTES", 
      "value": "962B" 
      }, 
      { 
      "type": "PERCENTAGE", 
      "value": "50%" 
      } 
     ] 
     } 
     } 
     ] 
    } 
    ] 
    }, 
    "PrioritizeVisibleContent": { 
    "localizedRuleName": "Prioritize visible content", 
    "ruleImpact": 2.0, 
    "urlBlocks": [ 
    { 
     "header": { 
     "format": "Your page requires additional network round trips to render the above-the-fold content. For best performance, reduce the amount of HTML needed to render above-the-fold content." 
     } 
    }, 
    { 
     "header": { 
     "format": "The entire HTML response was not sufficient to render the above-the-fold content. This usually indicates that additional resources, loaded after HTML parsing, were required to render above-the-fold content. Prioritize visible content that is needed for rendering above-the-fold by including it directly in the HTML response.", 
     "args": [ 
     { 
     "type": "HYPERLINK", 
     "value": "https://developers.google.com/speed/docs/insights/PrioritizeVisibleContent" 
     } 
     ] 
     }, 
     "urls": [ 
     { 
     "result": { 
     "format": "Only about $1 of the final above-the-fold content could be rendered with the full HTML response $2.", 
     "args": [ 
      { 
      "type": "PERCENTAGE", 
      "value": "26%" 
      }, 
      { 
      "type": "SNAPSHOT_RECT", 
      "value": "snapshot:5" 
      } 
     ] 
     } 
     } 
     ] 
    } 
    ] 
    } 
    } 
}, 
"version": { 
    "major": 1, 
    "minor": 15 
}, 
"screenshot": { 
    "data": "***************8This is the data i need!!*********", 
    "height": 240, 
    "mime_type": "image/jpeg", 
    "width": 320 
} 
} 
+1

paramétrer 'true' dans json_decode fait un tableau associatif de données – charlietfl

Répondre

2

Vous définissez le paramètre dans assocjson_decode sur true, donc il ret urn un tableau! Il vous suffit de supprimer true comme ceci:

$jsonData = json_decode($response); 
$testt = $jsonData->screenshot->data; 

Bien sûr, vous pouvez changer true à false aswell:

$jsonData = json_decode($response, false); 
$testt = $jsonData->screenshot->data; 

Plus d'informations à ce sujet dans le PHP-Manual for json_decode

1

utiliser comme

$jsonData = json_decode($aa, true); 
echo $jsonData['screenshot']['data']; 

working code

1

D'abord, il y a une erreur dans votre JSON en ligne 11 qui arrête json_deocde() retournant un tableau valide (vous devez supprimer la virgule de fin):

"numberStaticResources": 22, 

Deuxièmement, vous êtes obtenir un tableau de json_decode() de sorte que vous devez utiliser la syntaxe correcte:

$jsonData = json_decode($response, true); 
$testt = $jsonData['screenshot']['data'];