CURL和PHP - 发布多个键/值对数组

问题描述 投票:0回答:3

我需要使用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创建数组。

任何帮助表示赞赏。

谢谢

php arrays forms post curl
3个回答
0
投票
$postStr = "config[api_key]=api_key&config[api_secret]=api_secret&";;
foreach ($postData as $index => $node) {
    foreach ($node as $key => $value) {
        $postStr .= "data[{$index}][{$key}]=$value&";
    }

}
$postStr = rtrim($postStr, '&');

使用下面的行

curl_setopt( $ch, CURLOPT_POSTFIELDS, $postStr );

您已经创建了两个包含数据的阵列,另一个具有配置。它将来会对你有所帮助。


0
投票

您可以像这样添加顶部结构:

$postArray = array(
    'api_key' => api_key,
    'api_secret' => api_secret,
    'tm' => $_POST['tm'],
);

curl_setopt($ch, CURLOPT_POSTFIELD, $postArray);

除此之外,您应该将“Content-Type”标题保留为默认设置,即删除此行:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data'));

0
投票

PHP的约定是用括号(例如name[])将输入名称映射到$_POST中的数组

如果你试图从html表单中复制帖子,你已经非常简单了,你只需要将你的帖子字段名称与原始表单中的名称重新匹配:

foreach ( $postData['tm'] as $id => $node ) {
    foreach ( $node as $key => $value ) {
        // eg name="tm[1][name]"
        $postArray['tm['.$id.']['.$key.']'] = $value;
    }
}

(假设$postData相当于$_POST

© www.soinside.com 2019 - 2024. All rights reserved.