【发布时间】:2014-06-16 11:21:40
【问题描述】:
我需要使用 curl 发布表单。
这是表格。
<form action="post.php" method="POST">
<input name="tm[0][name]" value="name" type="hidden" maxlength="255">
<input name="tm[0][mobile]" value="mobile" type="hidden" maxlength="10">
<input name="tm[0][email]" value="email" type="hidden" maxlength="255">
<input name="tm[0][address]" value="address" type="hidden">
<input name="tm[0][pincode]" value="pincode" type="hidden" maxlength="6">
<input name="tm[0][refCode]" value="refCode" type="hidden" maxlength="255">
<input name="tm[0][partnerRef]" value="partnerRef" type="hidden" maxlength="255">
<input name="tm[1][name]" value="name" type="hidden" maxlength="255">
<input name="tm[1][mobile]" value="mobile" type="hidden" maxlength="10">
<input name="tm[1][email]" value="email" type="hidden" maxlength="255">
<input name="tm[1][address]" value="address" type="hidden">
<input name="tm[1][pincode]" value="pincode" type="hidden" maxlength="6">
<input name="tm[1][refCode]" value="refCode" type="hidden" maxlength="255">
<input name="tm[1][partnerRef]" value="partnerRef" type="hidden" maxlength="255">
<input type="submit" name="yt0" value="Submit">
</form>
这是我的代码:
$ch = curl_init();
$postArray = array(
'api_key' => api_key,
'api_secret' => api_secret
);
$data = array();
foreach( $postData as $key => $node ) {
$postArray[ $key ] = array(
'name' => $node[ 'name' ],
'mobile' => $node[ 'mobile' ],
'email' => $node[ 'email' ],
'address' => $node[ 'address' ],
'pincode' => $node[ 'pincode' ],
'refCode' => $node[ 'refCode' ],
'partnerRef' => $node[ 'partnerRef' ]
);
}
curl_setopt( $ch, CURLOPT_URL, 'check.php' );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-type: multipart/form-data' ) );
curl_setopt( $ch, CURLOPT_POST => true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $postArray );
$response = curl_exec( $ch );
我不确定如何制作数组,因为每个数组的键都是相同的。
我想为CURLOPT_POSTFILEDS 创建数组。
感谢任何帮助。
谢谢
【问题讨论】:
-
请出示您当前的 CURL 代码
-
有什么问题?只需将
$_POST传递给CURLOPT_POSTFILEDS选项 -
@Popnoodles 请看我的编辑。
-
啊,好的,这是一个 PHP 表单到数组的问题,CURL 部分实际上并不相关。
-
@web-nomad 为什么这么复杂?为什么不简单地传递
$_POST['tm']?
标签: php arrays forms post curl