【问题标题】:CURL and PHP - posting multiple array of key/value pairsCURL 和 PHP - 发布多个键/值对数组
【发布时间】: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


【解决方案1】:
$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 );

您已经创建了两个数组,一个包含数据,另一个包含配置。以后对你有帮助。

【讨论】:

    【解决方案2】:

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

    $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'));
    

    【讨论】:

      【解决方案3】:

      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

      【讨论】:

      • 你能举个例子吗?我试过了,还是不行。
      • 只需用上面的 foreach 替换原始请求中的 foreach 循环
      猜你喜欢
      • 2011-04-15
      • 2012-07-27
      • 2011-04-07
      • 2019-05-12
      • 2014-02-14
      • 2011-04-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多