-1

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...

alexandregv
  • 109
  • 1
  • 2
  • 4
  • I'm too lazy to check the rules to see if I can answer in French or not so I'll stick to English for now. What do you mean by the fact that the "attachments" data are ignored? Do you mean that they they do not exist in $_POST? Or are they empty? – Technoh Mar 27 '16 at 02:31
  • How can I check it ? – alexandregv Mar 27 '16 at 02:42
  • You could `var_dump` the complete `$_POST` on the receiving end, to see the exact JSON string. – Technoh Mar 27 '16 at 02:44
  • Possible duplicate of [Curl and PHP - how can I pass a json through curl by PUT,POST,GET](http://stackoverflow.com/questions/21271140/curl-and-php-how-can-i-pass-a-json-through-curl-by-put-post-get) – psiyumm Mar 27 '16 at 02:48

1 Answers1

0

You are doing double json_encode.

First, compare these two:

$json1 = json_encode([
  'firstname' => 'Jon',
  'lastname' => 'Doe',
  'hp' => [
    'cc' => '+1',
    'number' => '12345678'
  ]
);
//You should get
//{"firstname":"Jon","lastname":"Doe","hp":{"cc":"+1","number":"12345678"}}

And this:

$json2 = json_encode([
  'firstname' => 'Jon',
  'lastname' => 'Doe',
  'hp' => "['cc' => '+1', 'number' => '12345678']"
]);
//You should get
//{"firstname":"Jon","lastname":"Doe","hp":"['cc' => '+1', 'number' => '12345678']"}

Do you see the problem? You are setting your attachments key as a json encoded string, hence when it is sent to the target server, it will be seen as a string '[{"text":"Line 1", "color":"#000000"}, {"text":"Line 2", "color":"#ffffff"}]' instead of the expected array of text-color pairs. Nothing fancy here, it's purely a careless mistake :)

So to fix this, instead of sending json encoded string in the attachments key, use this instead:

$data = array(
    'username'    => 'TriiNoxYs',
    'channel'     => 'My channel'
    'text'        => 'My text',
    'attachments' => array(
        array(
            "text" => "Line 1",
            "color" => "#000000",
        ),
        array(
            "text" => "Line 2",
            "color" => "#ffffff"
        )
    )
);
Lionel Chan
  • 7,894
  • 5
  • 40
  • 69