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?

2
  • 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. Commented Nov 21, 2022 at 7: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))); Commented Nov 21, 2022 at 7:16

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.