简体   繁体   中英

Send JSON via PHP POST (cURL)

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

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"
        )
    )
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM