我想将数组提交给cURL
<form action="post.php" method="post">
<input name="comment[]" value="oh"/><br>
<input name="comment[]" value="wow"/><br>
<input name="comment[]" value="like"/><br>
<input type="submit" />
</form>
我希望结果发送如下:
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "0=oh&1=wow&2=like");
$hasil = curl_exec ($ch);
curl_close ($ch);
post.php文件:
$inputs = $_POST['comment']; print_r($inputs);
和结果:
Array
(
[0] => oh
[1] => wow
[2] => like
)
如何将结果发送给cURL?
答案 0 :(得分:0)
根据发布的值构建查询字符串:
$query_string = http_build_query($_POST['comment']);
并通过curl提交:
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $query_string);
$hasil = curl_exec ($ch);
curl_close ($ch);
答案 1 :(得分:0)
有一个函数http_build_query()可以帮你完成这项工作。
e.g:
$querystring = http_build_query($inputs);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $querystring);
$hasil = curl_exec ($ch);
curl_close ($ch);
答案 2 :(得分:0)
您的实际问题是
如何使用cURL发布数据数组?
这个问题已经回答here。
您可以使用函数http_build_query()从索引的comment
数组中构建一个URL编码字符串。
这段代码可以解决问题。
curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query($inputs))
您可能需要将Content-Type标头设置为multipart / form-data,如curl_setopt documentation中所述。