I'm trying to send JSON text with POST request using cURL. The JSON text is:
{"channel": "My channel", "username": "My username", "text": "My text"}
I'm using this code:
<?php
$data = array(
'username' => 'My username',
'channel' => 'My channel'
'text' => 'My text',
);
$data_json = json_encode($data);
$curl = curl_init('my-url');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_json))
);
$result = curl_exec($curl);
echo $result ;
?>
The code works but I want to add an "attachments" array in my JSON text, like this:
{"channel": "My channel", "username": "My username", "text": "My text", "attachments": [{"text":"Line 1","color":"#000000"},{"text":"Line 2","color":"#ffffff"}]}
So i tried to add this to my $data array:
'attachments' => '[{"text":"Line 1", "color":"#000000"}, {"text":"Line 2", "color":"#ffffff"}]',
But not working, the post is sent but my "attachments" is ignored ... I also tried to send POST directly from a Linux terminal with this code:
POST https://myurl.com
payload={"channel": "My channel", "username": "My username", "text": "My text", "attachments": [{"text":"Line 1","color":"#000000"},{"text":"Line 2","color":"#ffffff"}]}
And it's working ... I just don't understand why it's working with manual POST and not with php/curl...
Thanks for your answers.
PS: Sorry for my bad english, I'm french...