0

I would like so send post parameter via curl. I tried this:

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(
    array(
       "content"            =>    "Hi",
       "message_type"       =>    "outgoing",
       "content_type"       =>    "input_select",
       "content_attributes" =>  '{ "items": [{ "title": "Option1", "value": "Option 1" }, { "title": "Option2", "value": "Option 2" }]}',
       "private"            =>    false
    )
));

But this way does not work. If works like this:

curl_setopt($ch, CURLOPT_POSTFIELDS, 
        '{
            "content": "Hi",
            "message_type" : "outgoing",
            "content_type": "input_select",
            "content_attributes": {
                "items": [
                    { "title": "Option1", "value": "Option1" },
                    { "title": "Option2", "value": "Option2" }
                ]
            },
            "private":false
        }'
    );

Would it be also possible to do it like the way with array (my first try). If yes, where is my mistake?

Trombone0904
  • 4,132
  • 8
  • 51
  • 104
  • `http_build_query` creates a URL query string format. If you want to create JSON out of your existing data object, then that is of course a case for `json_encode`. – CBroe Nov 21 '22 at 07:10
  • Agreed with @CBroe. Try like this: `curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array("content"=>"Hi","message_type"=>"outgoing","content_type"=>"input_select","content_attributes" =>["items"=>[[ "title" =>"Option1", "value" =>"Option 1"],[ "title" =>"Option2", "value": "Option 2"]]],"private" => false)));` – Alive to die - Anant Nov 21 '22 at 07:16

0 Answers0