[英]curl post Json with addslashes
I need: 我需要:
$content = "{\"data1\":90,\"data2\":\"SUKAORU\",\"data3\":7483478334}";
curl_setopt_array($curl, array(CURLOPT_POSTFIELDS => $content);
I did: 我做了:
$_REQUEST = array("data1"=>90,"data2"=>"SUKAORU,"data3"=>7483478334);
$content1 = '"' . addslashes(json_encode($_REQUEST)) . '"';
curl_setopt_array($curl, array(CURLOPT_POSTFIELDS => $content1);
//or
$content1 = addslashes(json_encode($_REQUEST));
curl_setopt_array($curl, array(CURLOPT_POSTFIELDS => $content1);
//or
$content1 = json_encode($_REQUEST);
curl_setopt_array($curl, array(CURLOPT_POSTFIELDS => $content1);
$content and $content1 looks identically: $ content和$ content1看起来相同:
But second version returns error from server "unable to decode request". 但是第二个版本从服务器返回错误“无法解码请求”。
How can I ecranate array into JSON like in I need example? 如何像我需要的示例中那样将数组拖入JSON中?
Don't use addslashes
. 不要使用
addslashes
。 Just don't use it. 只是不要使用它。
The slashes here: 这里的斜线:
$content = "{\\"data1\\":90,\\"data2\\":\\"SUKAORU\\",\\"data3\\":7483478334}";
… are only part of the PHP source code . …只是PHP源代码的一部分 。 They are not part of the data.
它们不是数据的一部分。
If you're generating the JSON using json_encode
then you don't need to manually write \\"
to get quotes into the "
delimited string literal. 如果要使用
json_encode
生成JSON,则无需手动编写\\"
即可将引号括入"
分隔字符串文字中。 You don't have a string literal and json_encode
is generating the quotes. 您没有字符串文字,并且
json_encode
正在生成引号。
This should be fine. 没关系
$data = array("data1"=>90,"data2"=>"SUKAORU","data3"=>7483478334);
$json = json_encode($data);
curl_setopt_array($curl, array(CURLOPT_POSTFIELDS => $json);
Note addition of missing "
after "SUKAORU
. 注意在
"SUKAORU
"
之后缺少"
"SUKAORU
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.