【问题标题】:json_encode with object in arrayjson_encode 与数组中的对象
【发布时间】:2018-01-10 15:04:18
【问题描述】:

我正在尝试在 Wordpress 中创建一个函数,该函数通过 Curl 将带有 JSON 的数据发送到 Slack webhook。我更习惯于在 JavaScript 中构造数组和对象的方式,所以在 PHP 编码为 JSON 之前在 PHP 中执行此操作对我来说有点混乱。

这是该编码的 PHP 的外观:

$data = json_encode(array(
     "username"=> "Email Notifier",
     "mrkdwn"=> true,
     "icon_emoji"=> ":email:",
     "text" => "*Name:* {$posted_data["your-name"]}\n*Email:* {$posted_data["your-email"]}\n*Subject:* {$posted_data["your-subject"]}\n*Message:*\n >{$posted_data["your-message"]}",
     ));

  // Finally send the data to Zapier or your other webhook endpoint
       $ch = curl_init("https://hooks.slack.com/services/Txxxxxx/Bxxxxxx/xxxxxxxxxxxxxxx"); // replace with your Zapier webook
       curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
       curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
       curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,5); //Optional timeout value
       curl_setopt($ch, CURLOPT_TIMEOUT, 5); //Optional timeout value
       $result = curl_exec($ch);
       curl_close($ch);

   return $result;

这会将以下 JSON 成功发送到 Slack webhook:

{
   "username": "Email Notifier",
   "mrkdwn": true,
   "icon_emoji": ":email:",
   "text": "whatever" //generated by using PHP variables
}

我现在希望能够在名为 attachment 的数组中输出 text 属性和其他属性,如下所示:

{
   "username": "Email Notifier",
   "mrkdwn": true,
   "icon_emoji": ":email:",
   "text": "You have received a new message!",
   "attachment": [
      { 
         "text": "whatever", //generated by using PHP variables
         "fallback": "Required plain-text summary of the attachment.",
         "color": "#36a64f",
      }
   ]
}

但我不确定如何使用 PHP 语法解决这个问题。有什么想法吗?

【问题讨论】:

  • 请注意双引号中的双引号 ... 因为这会导致连接问题。您需要在 $posted_data['your-name'] 上使用单引号而不是双引号。
  • array('attachment' => array(array('text' => 'whatever', ...)))
  • 阅读PHP arrays。他们并不难。基本上,PHP 数组的工作方式类似于使用数组表示法处理的 Javascript 对象。 json_encode() 将正确的编码处理为 JSON。
  • 如果您有所需的 json 样本,请说 print_r(json_decode($sample)); 并查看您需要的数组结构

标签: php arrays json object


【解决方案1】:

试试这个,就像@deceze 说的那样。

$data = json_encode(array(
  "username"=> "Email Notifier",
  "mrkdwn"=> true,
  "icon_emoji"=> ":email:",
  "text" => "*Name:* {$posted_data["your-name"]}\n*Email:* {$posted_data["your-email"]}\n*Subject:* {$posted_data["your-subject"]}\n*Message:*\n >{$posted_data["your-message"]}",
  "attachment" => array(
    array( 
      "text" => "whatever",
      "fallback" => "Required plain-text summary of the attachment.",
      "color" => "#36a64f",
    ),
  ),
));

// Finally send the data to Zapier or your other webhook endpoint
    $ch = curl_init("https://hooks.slack.com/services/Txxxxxx/Bxxxxxx/xxxxxxxxxxxxxxx"); // replace with your Zapier webook
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,5); //Optional timeout value
    curl_setopt($ch, CURLOPT_TIMEOUT, 5); //Optional timeout value
    $result = curl_exec($ch);
    curl_close($ch);

return $result;

也可以看看PHP官方文档json_encode()上的这个部分

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-30
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    • 2014-05-17
    • 2012-07-29
    • 2014-10-25
    • 2021-10-02
    相关资源
    最近更新 更多