所以我在处理这个api响应方面遇到了一个小问题。 我试图用这个api响应来表达屏幕截图数据吗?..
我已经尝试了以下但无济于事!..它似乎只是NULL?.. 任何帮助都会非常感谢
$jsonData = json_decode($response, true);
$testt = $jsonData->screenshot->data;
回复:
{
"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
}
}
答案 0 :(得分:2)
您在assoc
json_decode
上设置了true
参数,因此会返回一个数组!
您只需删除true
,就像这样:
$jsonData = json_decode($response);
$testt = $jsonData->screenshot->data;
当然,您可以将true
更改为false
以及:
$jsonData = json_decode($response, false);
$testt = $jsonData->screenshot->data;
中有关此内容的详情
答案 1 :(得分:1)
答案 2 :(得分:1)
首先,第11行的JSON中存在一个错误,它阻止json_deocde()返回一个有效的数组(你应该删除尾随的逗号):
"numberStaticResources": 22,
其次,您从json_decode()获取数组,因此您需要使用正确的语法:
$jsonData = json_decode($response, true);
$testt = $jsonData['screenshot']['data'];