Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am trying to take the form input, send its values to this URL, and retrieve the JSON response.

Should this code work? I'm fairly new to this stuff, so my apologies if things are not quite right.

<?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>
share|improve this question
1  
we don't like to review code that hasn't been tested. you should make sure that the code runs and does what you want it to do, then we can help you make the code so it is more efficient. –  Malachi Dec 22 '13 at 16:16
3  
This question appears to be off-topic because it is about code that potentially doesn't work. –  Malachi Dec 26 '13 at 15:53
add comment

closed as off-topic by Malachi, Simon André Forsberg, Jamal Dec 26 '13 at 17:26

  • This question does not appear to be a code review request within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

The general idea is correct, but:

  1. Shouldn't you use something like "token" in $authToken = urlencode("id"); instead of "id"?
  2. To build the URL query parameters it's easier to use the http_build_query() function.
  3. To use file_get_contents() with HTTPS make sure you have the openssl extension installed.
  4. The form contains a "country" field that is not used in the URL, is this intended?
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.