与谁是我的代表API(whoismyrepresentative.com)一起使用,并且get命令不会呈现来自索引页面上输入字段的邮政编码要求的结果。语法是否正确,我需要回显命令吗?
我创建的索引页:
<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页面,我在其中调用GET命令:
<?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 …"];*/
?>
该命令应输出json。
答案 0 :(得分:1)
这将起作用:
function Checker() {
var checkb1 = document.getElementById("adult");
if (checkb1.checked){
var numbcop1 = document.getElementById(numb1);
} else{
var numbcop1 = 0;
}
var checkb2 = document.getElementById("ado");
if (checkb2.checked){
var numbcop2 = document.getElementById(numb2);
} else{
var numbcop2 = 0;
}
var checkb3 = document.getElementById("child");
if (checkb3.checked){
var numbcop3 = document.getElementById(numb3);
} else {
var numbcop3 = 0;
}
var checkb4 = document.getElementById("school");
if (checkb4.checked){
var numbcop4 = document.getElementById(numb4);
} else {
var numbcop4 = 0;
}
var checkb5 = document.getElementById("transl");
if (checkb5.checked){
var numbcop5 = document.getElementById(numb5);
} else{
var numbcop5 = 0;
}
}
答案 1 :(得分:0)
您将表格设置为发布请求。 您可以通过两种方式修复代码。
或将$ _GET更改为$ _POST,如下所示:
file_get_contents('https://whoismyrepresentative.com/getall_mems.php?zip='。$ _POST ['zip']。'php?zip =&output = json…');
答案 2 :(得分:0)
使用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;
?>
答案 3 :(得分:0)
查看您的实时网站和API后,您的问题归结为2个错误
要使用GET,您需要更新HTML表单方法以获取-
<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>
您的PHP也会出现问题,即您没有使用正确的参数调用正确的URL,也没有分配和回显正确的响应。这会将JSON返回到$json_rep
,并将json解码为数组$rep_array
,使您可以遍历代表并执行所需的操作。
<?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);
?>