【问题标题】:How to use the JSON data via PHP from Google Insight API如何通过来自 Google Insight API 的 PHP 使用 JSON 数据
【发布时间】:2014-06-23 02:38:58
【问题描述】:

我想通过 PHP 页面使用来自 Google Insight API 的数据。 (这里是documentation

例如,我如何使用 Google Insight 返回的“numberJsResources”?

$jsonString = curl "https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=http://code.google.com/speed/page-speed/&key=MyAPIKey";

$obj = json_decode($jsonString);

echo $obj->responseCode;

【问题讨论】:

    标签: php json google-api


    【解决方案1】:

    您错误地使用了 curl。

    $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);
    echo $obj['pageStats']['numberJsResources'];
    

    手册:http://www.php.net/manual/en/book.curl.php

    【讨论】:

    • 我得到 Parse error: syntax error, unexpected '"googleapis.com/pa' (T_CONSTANT_ENCAPSED_STRING) 在与我使用 curl 的行相对应的行
    • 好吧,那么完整的代码应该是?: $ch = curl_init("googleapis.com/pagespeedonline/v1/runPagespeed?url=http://…); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $res = curl_exec($ch); echo $res ; $obj = json_decode($res, true); $pageStats = $obj['pageStats']; echo $pageStats['numberJsResources'];
    • 很难从评论中读出来,但我认为是的,而且在你发表评论之前我已经再次编辑了:)
    • 是的,我试过了。我没有在屏幕上打印任何内容。我试着看看为什么。
    • 尝试打印 obj 数组。 print_r($obj);
    猜你喜欢
    • 2011-02-11
    • 1970-01-01
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-09
    • 2020-02-09
    • 2020-04-23
    相关资源
    最近更新 更多