0

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:

enter image description here

But second version returns error from server "unable to decode request".

How can I ecranate array into JSON like in I need example?

San Ya
  • 107
  • 7

1 Answers1

0

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • you are right, I think it was server temporarily problem, my previous code now works fine, still don't know why `json_encode($_REQUEST,JSON_NUMERIC_CHECK);` – San Ya Apr 04 '17 at 13:44