0

This is the Request body that I need to send- (Curl POST request in PHP)

$data = {
    "paramOne" : "f733787d-5649",
    "paramTwo": {
        "format": "123XD"
    },
    "paramThree": [
      {"type":"cn", "value":"Test User Manish 1"},
      {"type":"c", "value":"US"}
    ]
};

I'm trying to use it at this line of my Curl Request in PHP-

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

The nested params are messing up the format in which I'm trying to send.

I already tried using http_build_query but then paramThree is causing problems. I'm looking for the changes that I need to make to the format of $data before I use http_build_query on it.

1 Answer 1

1

It's JSON. You can either post it as a string (enclosed in quotes) or make an array first, convert it to JSON and then post it. Like this:

$array = [
    'paramOne' => 'f733787d-5649',
    'paramTwo' => [
        'format' => '123XD'
        ],
    'paramThree' => [[
        'type' => 'cn',
        'value' => 'Test User Manish 1'
        ],
        [
        'type' => 'c',
        'value' => 'US'
            ]]
    ];

$data = json_encode($array);

In both cases use

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks 10 on 10! I think I tried to do all the things together. Should have kept the request body in array format and I wouldn't even need to use http_build_query in the first place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.