php通过curl传输JSON对象

PHP通过curl方式传参给java接口报错:JSONObject["xxx"] is not a JSONObject

PHP 对变量进行 JSON 编码 (json_encode()) 返回字符串,而接口参数需要 json 对象。

{"code":0,"data":{"isSrvAccount":0},"mccode":0,"errObj":{},"reqId":"S1B6Mt01w","seqId":"d4d7435b-75ca-962e-17b6-cbc478da83d8"}

解决办法:将变量 json_decode() 后即可。

例:

// 转成JSON对象
$test = json_decode($test);

print_r 打印

stdClass Object
(
    [code] => 0
    [data] => stdClass Object
        (
            [isSrvAccount] => 0
        )

    [mccode] => 0
    [errObj] => stdClass Object
        (
        )

    [reqId] => S1B6Mt01w
    [seqId] => d4d7435b-75ca-962e-17b6-cbc478da83d8
)

如果是 curl 方式的话记得设置 CURLOPT_HTTPHEADER

// 设置HTTP头
curl_setopt ( $curl, CURLOPT_HTTPHEADER, ["Content-Type: application/json"] );

你可能感兴趣的:(php通过curl传输JSON对象)