如何在JSON请求体中使用php变量?

如何在JSON请求体中使用php变量?,php,json,Php,Json,我有一个如下所示的请求主体: curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"username\":\"testuser5@so.com\",\"send_welcome\":true,\"welcome_message\":\"Test\",\"account_attributes\":{\"email_address\":\"testuser5@so.com\",\"first_name\":\"Snow\",\"last_name\":\"White\",

我有一个如下所示的请求主体:

curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"username\":\"testuser5@so.com\",\"send_welcome\":true,\"welcome_message\":\"Test\",\"account_attributes\":{\"email_address\":\"testuser5@so.com\",\"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\":[]}");
我想将username php变量分配给“username”json参数,但我在尝试时一直导致400个错误请求:

$username = json_encode('something@something.com');

curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"username\":" . $username . ",\"send_welcome\":true,\"welcome_message\":\"Test\",\"account_attributes\":{\"email_address\":\"testuser5@so.com\",\"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\":[]}");
尝试:

使用数组并使用json_encode()获取json

为什么?


PHP不能自然地使用类似JSON的Javascript,用原始字符串连接和构造JSON是一种不好的做法,也是一个令人头痛的问题。

当您对用户名进行编码时,它看起来是这样的:

"something@something.com"
因此,当连接时,您添加了额外的引号,导致JSON格式错误。结果是:

{"username":""something@something.com""...
您应该做的是直接连接:

$username = 'something@something.com'; // do not encode
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"username\":\"" . $username . "\",\"send_welcome\":true,\"welcome_message\":\"Test\",\"account_attributes\":{\"email_address\":\"testuser5@so.com\",\"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\u stopt()
中的JSON,为值添加必要的反斜杠和附加引号,以确保正确连接。这将确保您的JSON是正确的。

如果您与其他开发人员合作,请使用此方法:Doh我在回答中遗漏了这一点。迄今为止最好的一个。+1老实说,这是两个答案中最好的一个,因为我同意kip关于在PHP中直接使用JSON的主张。我删除了我的评论(它们现在没有任何意义),并将它们改为upvote。我希望这是正确的行为。感谢@jh1711上的备份-是的,这将是Stack认为的好公民。+1,这个答案解释了OP代码的错误,而不是建议一个完全不同(尽管更好)的解决方案。我感谢你解释我的错误的答案,只是为了让你知道。我也投了更高的票。我只选择了另一个答案,因为这是一个比我试图做的更好的实践,如果有人用谷歌搜索这个,我宁愿他们先看到最佳实践。
$username = 'something@something.com'; // do not encode
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"username\":\"" . $username . "\",\"send_welcome\":true,\"welcome_message\":\"Test\",\"account_attributes\":{\"email_address\":\"testuser5@so.com\",\"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\":[]}");