【问题标题】:How do I include a variable in a multidimensional JSON string?如何在多维 JSON 字符串中包含变量?
【发布时间】:2023-03-30 06:59:01
【问题描述】:

我试图在多维 JSON 字符串中包含一个变量。我想我需要使用 json_encode 但我不知道该怎么做。

$clientID = 123456;
$body = "<h1 id='heading1'>This is just a standard html message.</h1>";

curl_setopt($ch, CURLOPT_POSTFIELDS, "{
    \"subject\": \"How Did We Do\",
    \"clientId\": $clientID,
    \"assignedUserId\": 2988,
    \"public\": false,
    \"activity\": [
    {
      \"public\": false,
      \"comment\": {
          \"body\": $body
      }
    }
    ]
}");

【问题讨论】:

  • 为什么要手动创建一个json字符串?
  • 对于 API 调用,我不知道其他方式。

标签: php json curl


【解决方案1】:

您应该创建一个数组并使用json_encode() 对其进行编码。

$array = [
    'subject' => 'How Did We Do',
    'clientId' => $clientID,
    'assignedUserId' => 2988,
    'public' => false,
    'activity' => [
        [
            'public' => false,
            'comment' => [
                'body' => $body
            ]
        ]
    ]
];

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

【讨论】:

  • 谢谢你,我要试试这个!
猜你喜欢
  • 2023-03-04
  • 2021-04-03
  • 2021-10-04
  • 2011-04-20
  • 1970-01-01
  • 2012-06-09
  • 2012-06-12
  • 2020-05-22
  • 1970-01-01
相关资源
最近更新 更多