KinsDotNet
KinsDotNet

Reputation: 1560

How can I use a php variable in a JSON request body?

I have a request body that looks like this:

curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"username\":\"[email protected]\",\"send_welcome\":true,\"welcome_message\":\"Test\",\"account_attributes\":{\"email_address\":\"[email protected]\",\"first_name\":\"Snow\",\"last_name\":\"White\",\"middle_initial\":\"A\",\"phone_number\":\"2055551234\",\"address\":\"1 Main St\",\"city\":\"Detroit\",\"state\":\"MI\",\"zip\":\"48220\",\"country\":\"US\"},\"Gender\":\"Male\",\"portals\":[],\"groups\":[],\"identification_card_numbers\":[]}");

I want to assign the username php variable to the "username" json parameter, but I keep causing 400 bad requests when I try:

$username = json_encode('[email protected]');

curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"username\":" . $username . ",\"send_welcome\":true,\"welcome_message\":\"Test\",\"account_attributes\":{\"email_address\":\"[email protected]\",\"first_name\":\"Snow\",\"last_name\":\"White\",\"middle_initial\":\"A\",\"phone_number\":\"2055551234\",\"address\":\"1 Main St\",\"city\":\"Detroit\",\"state\":\"MI\",\"zip\":\"48220\",\"country\":\"US\"},\"Gender\":\"Male\",\"portals\":[],\"groups\":[],\"identification_card_numbers\":[]}");

Upvotes: 3

Views: 721

Answers (2)

Jay Blanchard
Jay Blanchard

Reputation: 34426

When you encode the username it looks like this:

"[email protected]"

So, when you concatenate you're adding in the extra quotes, causing your JSON to be malformed. The result is:

{"username":""[email protected]""...

What you should do is just concatenate directly:

$username = '[email protected]'; // do not encode
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"username\":\"" . $username . "\",\"send_welcome\":true,\"welcome_message\":\"Test\",\"account_attributes\":{\"email_address\":\"[email protected]\",\"first_name\":\"Snow\",\"last_name\":\"White\",\"middle_initial\":\"A\",\"phone_number\":\"2055551234\",\"address\":\"1 Main St\",\"city\":\"Detroit\",\"state\":\"MI\",\"zip\":\"48220\",\"country\":\"US\"},\"Gender\":\"Male\",\"portals\":[],\"groups\":[],\"identification_card_numbers\":[]}");

Concatenating directly requires that you modify the JSON in curl_stopt() to add in the necessary backslashes for the value as well as the addition quotes to insure the concatenation happens correctly. That will insure your JSON is correct.

Upvotes: 5

kip
kip

Reputation: 1140

Try:

 $username = '[email protected]';

 $json = json_encode(
    array(
        "username" => $username,
        "send_welcome" => true,
        "welcome_message" => "Test",
        "account_attributes" => array(
            "email_address" => "[email protected]",
            "first_name" => "Snow",
            "last_name" => "White",
            "middle_initial" => "A",
            "phone_number" => "2055551234",
            "address" => "1 Main St",
            "city" => "Detroit",
            "state" => "MI",
            "zip" => "48220",
            "country" => "US"
        ),
        "Gender" => "Male",
        "portals" => [],
        "groups" => [],
        "identification_card_numbers" => []
    )
);

curl_setopt($ch, CURLOPT_POSTFIELDS, $json);

Work with array and use json_encode() to get the JSON

Why ?

PHP dont work naturally with JSON like Javascript, and concatenate and construct a JSON with a raw string is a bad practice and a headache.

Upvotes: 6

Related Questions