Reputation: 91
Working with the who is my representative API (whoismyrepresentative.com ) and the get command doesn't render the results from an input field on the index page asking for a zipcode. Is the syntax incorrect, do I need to echo the command?
Index page I created:
<section class="form-one">
<form action="search-results.php" method="post">
<label for="zipcode"> Input Zipcode</label>
<input id="zipcode" type="number" name="zip" size="5">
<input type="submit" name="">
</form>
</section>
Search-Results.php page where i call the GET Command:
<?php
file_get_contents('https://whoismyrepresentative.com/getall_mems.php?zip=&output=json'.$_GET['zip'].'php?zip=&output=json …');
/*echo $_GET["https://whoismyrepresentative.com/getall_mems.php?zip=&output=json …"];*/
?>
The command should output json.
Upvotes: 0
Views: 99
Reputation: 548
Use php cURL
<?php
$ch = curl_init('https://whoismyrepresentative.com/getall_mems.php?zip=' . $_POST['zip']);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json'
);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
//execute
$result = curl_exec($ch);
//close connection
curl_close($ch);
echo $result;
?>
Upvotes: 0
Reputation: 1600
After looking at your live site and the API, your issue is coming down to 2 errors
To use GET you need to update your HTML form method to get -
<section class="form-one">
<form action="search-results.php" method="get">
<label for="zipcode"> Input Zipcode</label>
<input id="zipcode" type="number" name="zip" size="5">
<input type="submit" name="">
</form>
</section>
You also then have issues in your PHP where you are not calling the correct URL with correct parameters and are not assigning and echoing the correct response. This returns the JSON to $json_rep
and decodes the json into an array $rep_array
allowing you to then loop through the reps and do what you need to do.
<?php
$json_rep = file_get_contents('https://whoismyrepresentative.com/getall_mems.php?zip='.$_GET['zip'].'&output=json');
$rep_array = json_decode($json_rep);
var_dump($rep_array);
?>
Upvotes: 0
Reputation: 783
You set your form to make a post request. You can fix the code in two ways.
Or change the $_GET to $_POST as following
file_get_contents('https://whoismyrepresentative.com/getall_mems.php?zip=' . $_POST['zip'] . 'php?zip=&output=json …');
Upvotes: 0
Reputation: 750
This will work:
file_get_contents('https://whoismyrepresentative.com/getall_mems.php?zip=' . $_GET['zip'] . '&output=json');
Upvotes: 1