PHP cURL表单数据:多个变量,名称相同,差异.价值观 [英] PHP cURL Form data: Multiple variables, same name, diff. values
问题描述
我正在使用PHP cUrl查询表单.它执行POST查询,因此我正在使用关联数组.表单如下所示:
I am working with PHP cUrl to query a form. It does a POST query, so I am using an associative array. This is what the form looks like:
<form action="form.php" method="POST">
...
<input type="hidden" name="var" value="value1">
<input type="hidden" name="var" value="value2">
<input type="hidden" name="var" value="value3">
<input type="hidden" name="var" value="value4">
<input type="hidden" name="var" value="value5">
...
</form>
在执行cUrl查询时,我具有以下代码:
While doing my cUrl query, I have the following code:
$postfields = array();
$postfields ["var"] = "value1";
$postfields ["var"] = "value2";
$postfields ["var"] = "value3";
$postfields ["var"] = "value4";
$postfields ["var"] = "value5";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_REFERER, $referer);
$result = curl_exec ($ch);
curl_close ($ch);
很明显,在这种情况下,PHP覆盖了之前的4个"var"赋值,并且只有value5被作为参数传递,并且我收到一条错误消息,提示我缺少value1..value4.我尝试将"var"设置为数组,但这也会提示我一个错误.
Obviously in this case, PHP overwrites the previous 4 "var" assignments and only value5 gets passed as a parameter and I get an error saying I am missing value1..value4. I tried making "var" an array but that also prompts me an error.
我在俯视什么吗?谢谢
推荐答案
第一个问题是表单.您应将type ="POST"设置为method ="POST".通过在name属性中使用[],您的隐藏字段也应该是一个数组.请尝试以下操作:
The first problem is with the form. You have type="POST" when it should be method="POST". Your hidden fields should also be an array by using [] in the name attribute. Try the following instead:
<?php
if (isset($_POST['submit']))
{
var_dump($_POST);
}
?>
<form action="" method="POST">
...
<input type="hidden" name="var[]" value="value1">
<input type="hidden" name="var[]" value="value2">
<input type="hidden" name="var[]" value="value3">
<input type="hidden" name="var[]" value="value4">
<input type="hidden" name="var[]" value="value5">
<input type="submit" name="submit" value="submit">
...
</form>
如果运行该命令,将会看到这些值现在位于数组中.要将其复制到您的CURL请求中,您可以执行以下操作:
If you run that, you will see that the values are now in an array. To replicate this in your CURL request, you would do:
$postfields = array();
...
$postfields["var"][] = "value1";
$postfields["var"][] = "value2";
$postfields["var"][] = "value3";
$postfields["var"][] = "value4";
$postfields["var"][] = "value5";
这篇关于PHP cURL表单数据:多个变量,名称相同,差异.价值观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!