0

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.

4
  • You have put the $_GET in the wrong position, without looking at the API i'd assume it needs to be file_get_contents('https://whoismyrepresentative.com/getall_mems.php?zip='.$_GET['zip'].'&output=json'); Commented Jan 5, 2019 at 2:59
  • I tried your code also to no avail, i made the page live for reference purposes: jamieannpowell.com/who-is-my-rep Commented Jan 5, 2019 at 3:23
  • Change your form method to get <form action="search-results.php" method="get"> OR update your $_GET['zip'] to $_POST['zip'] Commented Jan 5, 2019 at 3:44
  • Abdur Rahman suggested that also, unfortunately that doesn't fix the problem. Commented Jan 5, 2019 at 3:54

4 Answers 4

1

This will work:

file_get_contents('https://whoismyrepresentative.com/getall_mems.php?zip=' . $_GET['zip'] . '&output=json');
Sign up to request clarification or add additional context in comments.

2 Comments

I tried your code a few times and it didn't work, I also tried, hand-typing to no avail...
@Jay I've checked your live code. It get me some parse error. " Parse error: syntax error, unexpected '<', expecting end of file in /home3/lovelife80/public_html/who-is-my-rep/search-results.php on line 22". Can you post your full code? So I can see what's on line 22. I think from there it can be fixed.
0

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);

?>

2 Comments

Thank you! I knew something was wrong with how i was formatting the url. I haven't worked with php in a while so i didn't know i had to assign the url and echo file_get_contents. Much apperciated!
I was multi tasking but I should of picked it up earlier lol if this fixed it make sure to mark the correct answer. :)
0

You set your form to make a post request. You can fix the code in two ways.

  1. Change form method to get, your existing code will work
  2. Or change the $_GET to $_POST as following

    file_get_contents('https://whoismyrepresentative.com/getall_mems.php?zip=' . $_POST['zip'] . 'php?zip=&output=json …');

1 Comment

Thanks, i tried that a few times and it's not working :( i wonder if it is the 'zip=' in the url that is causing the problem. 'zip' is also the name attribute for the form.
0

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;

?>

2 Comments

$ch = curl_init('https://whoismyrepresentative.com/getall_mems.php?zip=' . $_GET['zip']'); should be $ch = curl_init('https://whoismyrepresentative.com/getall_mems.php?zip=' . $_GET['zip']);
i tried doesn't work... this is the link for the members: whoismyrepresentative.com/getall_mems.php? there's no data...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.