【问题标题】:Post Data PHP array in an array在数组中发布数据 PHP 数组
【发布时间】:2018-01-26 13:41:24
【问题描述】:

我认为这个问题是由于我对 PHP 中的数组缺乏了解。

通常我尝试使用 curl 在 php 中发送发布请求,但是我希望 postbody 看起来像这样:

{
   "deliveryAddress":[
      {
         "ID":"5",
         "address":"example@example2.com",
         "method":"EMAIL",
         "checkbox":true,
         "flashlight":false
      },
      {
         "ID":"7",
         "address":"example45@example3.com",
         "method":"EMAIL",
         "checkbox":true,
         "flashlight":false
      }
   ]
}

大概是它在 API 中的样子,所以如果我把它放到像 Fiddler 这样的程序中,它工作得非常好。然而,把它变成 PHP 中的 postbody 我有更多的困难。这是我迄今为止最好的尝试:

$postData = array(
    'deliveryAddress' => array(
    'ID'=>  '5',
    'address'=>  'example@example2.com',
    'method'=>  'EMAIL',
    'checkbox'=>  true,
    'flashlight'=>  false,

    'ID'=>  '7',
    'address'=>  'example45@example3.com',
    'method'=>  'EMAIL',
    'checkbox'=>  true,
    'flashlight'=>  false,



    )

);

$url = "ServerIamSendingItTo";
$ch = curl_init();

$headers = array(

    'Content-Type: application/json',
    'Authorization: BASIC (mybase64encodedpass)=='
);





curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_VERBOSE, 1);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));

curl_setopt($ch, CURLOPT_URL, $url);



$result = curl_exec($ch);

$ch_error = curl_error($ch);

if ($ch_error) {

   echo"ERROR";
   curl_close($ch);

} else {


     var_dump($result);

}
curl_close($ch);

显然我做错了 postdata,但我不确定如何构建它。任何帮助都会很棒。谢谢。

【问题讨论】:

  • 你想要json,所以使用json_encode函数
  • @moped 我正在使用 json_encode 函数。 'curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));'

标签: php arrays curl postdata


【解决方案1】:

DeliveryAddress 是一个对象数组(JSON 编码后)

因此,如果您想根据您作为示例编写的 JSON 发布数据,那么 PHP 数组必须以这种方式构建:

$postData = array(
    'deliveryAddress' => array(

        array (
            'ID'=>  '5',
            'address'=>  'example@example2.com',
            'method'=>  'EMAIL',
            'checkbox'=>  true,
            'flashlight'=>  false
            ),

        array (
            'ID'=>  '7',
            'address'=>  'example45@example3.com',
            'method'=>  'EMAIL',
            'checkbox'=>  true,
            'flashlight'=>  false
        )

    )
);

请注意,在“PHP 端”deliveryAddress 现在是一个关联数组数组(曾经json_encoded 将转入一个对象数组)。

【讨论】:

  • 所以我假设在此之后我需要做一些类似 $postData = json_encode($postData); ?因为现在我得到一个意外的“=>”(T_DOUBLE_ARROW),期待“(”(指向送货地址或紧随其后的行)
  • @Lain 当您将数组传递给 curl 时,您已经在编码 $postData:在您的代码中,您有 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
  • 你是对的,我的错。但它仍然给了我在那条评论中提到的意外 => 错误。它指向第一个 ID 正上方的行。那只是说 array =>( 这是正确的语法吗?因为你做的其他数组: Array( 而不是 array => (
  • @Lain,对不起。我的答案中的代码有错字!我刚刚修好了
猜你喜欢
  • 2015-06-12
  • 1970-01-01
  • 2010-09-30
  • 2023-03-15
  • 1970-01-01
  • 2016-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多