PHP cURL, POST JSON

后端 未结 5 2278
南方客
南方客 2020-11-28 13:48

I need to POST the following JSON code, but for some reason it is not working. Below is the code that I have.

$fieldString = \"395609399\";
//the curl reques         


        
5条回答
  •  误落风尘
    2020-11-28 14:23

    The bit that is the problem is:

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode("{categoryId: $fieldString}"));
    

    CURLOPT_POSTFIELDS will accept either an array of parameters, or a URL-encoded string of parameters:

    curl_setopt($ch, CURLOPT_POSTFIELDS, array('json'=>json_encode($stuff)));
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'json='.urlencode(json_encode($stuff)));
    

    Where json will be the name of the POST field (i.e.: will result in $_POST['json'] being accessible).

提交回复
热议问题