[英]curl post Json with addslashes
我需要:
$content = "{\"data1\":90,\"data2\":\"SUKAORU\",\"data3\":7483478334}";
curl_setopt_array($curl, array(CURLOPT_POSTFIELDS => $content);
我做了:
$_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和$ content1看起來相同:
但是第二個版本從服務器返回錯誤“無法解碼請求”。
如何像我需要的示例中那樣將數組拖入JSON中?
不要使用addslashes
。 只是不要使用它。
這里的斜線:
$content = "{\\"data1\\":90,\\"data2\\":\\"SUKAORU\\",\\"data3\\":7483478334}";
…只是PHP源代碼的一部分 。 它們不是數據的一部分。
如果要使用json_encode
生成JSON,則無需手動編寫\\"
即可將引號括入"
分隔字符串文字中。 您沒有字符串文字,並且json_encode
正在生成引號。
沒關系
$data = array("data1"=>90,"data2"=>"SUKAORU","data3"=>7483478334);
$json = json_encode($data);
curl_setopt_array($curl, array(CURLOPT_POSTFIELDS => $json);
注意在"SUKAORU
"
之后缺少"
"SUKAORU
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.