【问题标题】:google api search results displaying from json format to php/htmlgoogle api 搜索结果从 json 格式显示为 php/html
【发布时间】:2014-10-04 13:49:30
【问题描述】:

我想在我的网站上显示我通过 goole json api url 收到的 actvivites 帖子结果,因此我使用了以下编码。但它不显示结果。请指教 .

    <?php
   $jsonurl = "https://www.googleapis.com/plus/v1/activities?orderBy=recent&query=%23fifa&alt=json&key={API KEY }";
    $json = file_get_contents($jsonurl);

    $json_output = json_encode($json);
    //echo json_encode($json);
    echo '<pre>';
    //echo json_encode($json);
    print_r($json_output);
    echo '</pre>';
    ?>
    <div class="datagrid ">


    <?php
    $json=array();
    foreach ($json_output["data"] as $json_result) {
        $json[] = array(
            'value' => $json_result["title"],
            'value' => $json_result["url"]

        );

      ?>

    <div ><table>


    <tbody>

        <TR>
        <TD><h3><?php echo $json_result["title"];?></h3></TD>
         <TD><h3><?php echo $json_result["url"];?></h3></TD>
        </TR>
     </tbody>
    </table>
    </div>

      <?php

    }


    ?>

    </div>

google api source

【问题讨论】:

  • 你错过了 foreach 的结束 }
  • 那么你的 echo 和 print_r 语句显示了什么
  • @user574632 : 它给出'假'
  • @ExplosionPills:它就在那里,只是没有为代码格式正确缩进。
  • 如果你得到 false,那么 file_get_contents FAILED 并返回一个布尔值 false。

标签: php json api google-api google-api-php-client


【解决方案1】:

我可以在您的代码中指出一些事情。有些人肯定需要一点帮助。

你有这两行。

$json = file_get_contents($jsonurl);

$json 已经是一个 json 编码的字符串,我想而不是下面的行。您实际上想将其解码为数组。

$json_output = json_encode($json);

$json_output = json_decode($json, true);

请注意,我使用 true 参数来确保它一直被解码,而不是 std 类变量的数组。但我想这取决于你想要的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    相关资源
    最近更新 更多