【问题标题】:Odd array key/value variable assigning in CURL posting在 CURL 发布中分配奇数数组键/值变量
【发布时间】:2010-06-24 18:16:56
【问题描述】:

假设我有一个数组,我打印它:

  print_r($myArray);
  Array
  (

        [post] => 333434kj
        [test] => wOVvc
        [tytytyty] => xyzsalasjf

  )

这个数组被分配给一个 CURL Post:

curl_setopt($ch, CURLOPT_POSTFIELDS, "field1=".$f1."&field2=".$f2."&something=True");

由于“field1”等于“post”并且$f1等于“333434kj”等,我很难弄清楚如何将键和值实现为变量,如[post], [test] 和 [tytytyty] 每次运行时都会更改值。如何让每个键成为变量,每个值成为变量?

【问题讨论】:

    标签: php arrays curl


    【解决方案1】:

    看看http://php.net/http_build_query

    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($myArray) . "&something=True");
    

    如果您愿意,也可以只传递数组:

    $myArray['something'] = 'True';
    curl_setopt($ch, CURLOPT_POSTFIELDS, $myArray);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-02
      • 2017-07-02
      • 2022-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多