简体   繁体   English

CURLOPT_POSTFIELDS 的 curl 格式

[英]curl format for CURLOPT_POSTFIELDS

I want to send this via curl:我想通过 curl 发送这个:

{
  "clientID": "9J8pM7Y…",
  "data": [
    {
      "photoId": 1,

      "success": true
    }
  ]
}

Ι was trying to send it like this:我试图像这样发送它:

$body = str_replace('\"','', wp_json_encode( [
        'clientID' => $clientid,
        'data' => ['photoId' => $photoId, 'success' => true]
    ]))

But then it was a json object not an array of json objects as needed.但后来它是 json object 而不是 json 对象的数组。 I tried sending it like a string but with no success:我尝试像字符串一样发送它,但没有成功:

$body ='{"clientID":"'.$clientid.'","data":[{"photoId":'.$photoId.',"success":true}]}';

Here is the whole curl:这是整个 curl:

curl_setopt_array($curl, array(
        CURLOPT_URL => "http://example.com/js/RDCJS/UpdatePhotos/",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS =>$body,
        CURLOPT_HTTPHEADER => array(
            "Accept: application/json",
            "Content-Type: application/json"
        ),
    ));

Add an array around the data value.data值周围添加一个数组。

$body = wp_json_encode( [
        'clientID' => $clientid,
        'data' => [['photoId' => $photoId, 'success' => true]]
    ]));

Don't remove the " characters, that produces invalid JSON.不要删除"字符,这会产生无效的 JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM