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:
But second version returns error from server "unable to decode request".
How can I ecranate array into JSON like in I need example?
Don't use addslashes
. Just don't use it.
The slashes here:
$content = "{\\"data1\\":90,\\"data2\\":\\"SUKAORU\\",\\"data3\\":7483478334}";
… are only part of the PHP source code . 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. You don't have a string literal and json_encode
is generating the quotes.
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.