【问题标题】:Why am I not getting my json response?为什么我没有收到我的 json 响应?
【发布时间】:2013-09-08 23:24:13
【问题描述】:

好的,我认为这应该生成一个 json 响应,但没有打印出来。我错过了什么?我正在尝试将此输入与 url 请求一起发送,然后得到一个 json 响应来打印。

<?php

// Customize this (get ID/token values in your SmartyStreets account)
$authId = urlencode("id");
$authToken = urlencode("id");

// Address input
$input1 = urlencode($_POST["street"]);
$input2 = urlencode($_POST["city"]);
$input3 = urlencode($_POST["state"]);
$input4 = urlencode($_POST["postcode"]);

// Build the URL
$req = "https://api.smartystreets.com/street-address/?street={$input1}&city={$input2}&state={$input3}&postcode={$input4}&auth-id={$authId}&auth-token={$authToken}";

// GET request and turn into associative array
$result = json_decode(file_get_contents($req));



?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
</head>
<body>
<div id="panel">
     <form action="smarty-geocode.php" name="" method="post">
          <input id="addresses" type="hidden" value="">
          Street Address
          <input id="street" type="textbox" value="" class="address">
          <br />
          City
          <input id="city" type="textbox" value="" class="address">
          <br />
          State
          <input id="state" type="textbox" value="" class="address">
          <br />
          Zipcode
          <input id="postcode" type="textbox" value="" class="address">
          <br />
          Country
          <input id="country" type="textbox" value="" class="address">
          <br />
          <input type="submit" value="submit" id="#mySubmitButton" />
     </form>
</div>
<div id="map-canvas" style="height:40%;top:30px;"></div>
<div>
<?php
echo "<pre>";
print_r($result);
echo "</pre>";
?>
</div>
</body>
</html>

【问题讨论】:

  • 在您在这里提出问题之前,请三思而后行,是否符合质量标准。我们应该如何帮助您?你不能指望我在smartystreets.com 上创建一个帐户。因此,您需要将该 api 请求的响应数据放入问题中。
  • 至少提供工作输入值、这些生成的 req-url 以及该 url 上的响应。您还应该提供 $result 的 var 转储。还有一些吹毛求疵,你正在做的 json_decode 并没有把它变成一个数组;)
  • hek2mgl - 这就是我的问题。我不确定如何在这里获取响应数据。我期待它会生成一个响应,它通过请求变量打印它。然而,这并没有发生。

标签: php json forms smartystreets


【解决方案1】:

我没有足够的信息来调试您的 URL 的输出 - 但是我认为您应该能够通过将其添加到页面底部来自己解决这个问题:-

<?php var_dump(file_get_contents($req)); ?>

除此之外,您的任何表单元素都没有名称属性,因此实际上没有任何内容被发布到您的脚本中。这是您的表单的修改版本,其中包括每个字段的名称属性:-

     <form action="smarty-geocode.php" name="" method="post">
      <input id="addresses" name="addresses" type="hidden" value="">
      Street Address
      <input id="street" name="street" type="textbox" value="" class="address">
      <br />
      City
      <input id="city" name="city" type="textbox" value="" class="address">
      <br />
      State
      <input id="state" name="state" type="textbox" value="" class="address">
      <br />
      Zipcode
      <input id="postcode" name="postcode" type="textbox" value="" class="address">
      <br />
      Country
      <input id="country" name="country" type="textbox" value="" class="address">
      <br />
      <input type="submit" value="submit" id="#mySubmitButton" />
 </form>

希望这一点,结合您对 file_get_contents 命令输出的调试将帮助您解决您遇到的问题。我假设您正在用 Smarty Streets 中的有效 authId 和 authToken 替换这些值:-

$authId = urlencode("id");
$authToken = urlencode("id");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多