You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PHP OAuth Library调用API报400错误,求问题排查

Fixing the 400 Error with Double-Encoded JSON

The root cause of your error is double JSON encoding. Here's what's happening behind the scenes:

  1. You manually convert your $fields array to a JSON string using json_encode($fields).
  2. Then you pass that string to CallAPI, which (since you set RequestContentType to application/json) automatically encodes the input again into JSON.

The result is that your API receives a quoted string of your original JSON (like "{"lastName":"Smith"...}") instead of the actual JSON object it expects. That's why you get the error about converting a string to the required data type.

Corrected Code

Remove the manual json_encode step and pass the raw $fields array directly to CallAPI:

$params = array( 
    'x-api-key' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 
    'Content-Type' => 'application/json', 
    'Accept' => 'application/json' 
);

$fields = array( 
    "lastName" => $entries[$tmp_id]["2.6"], 
    "firstName" => $entries[$tmp_id]["2.3"], 
    "webAddresses" => array([ "value" => $entries[$tmp_id]["4"] ]), 
    "phoneNumbers" => array([ "value" => $entries[$tmp_id]["5"] ]) 
);

// Remove the json_encode line here
$success = $client->CallAPI( 
    'https://myurl.com/v2/api', 
    'POST', 
    $fields, // Pass the array directly instead of $json
    array(
        'FailOnAccessError'=>true, 
        'RequestHeaders' => $params,
        'RequestContentType' => 'application/json', 
        'FollowRedirection' => true, 
    ), 
    $user
);

Additional Check: Structure of webAddresses and phoneNumbers

Looking at the error message, the API is receiving webAddresses as a single object ({"value":"john@smith.com"}), but your code is sending it as an array of objects ([{"value":"john@smith.com"}]).

If the API expects a single address object (not an array), adjust those lines to remove the extra array layer:

"webAddresses" => array("value" => $entries[$tmp_id]["4"]), 
"phoneNumbers" => array("value" => $entries[$tmp_id]["5"]) 

Or using shorter PHP array syntax:

"webAddresses" => ["value" => $entries[$tmp_id]["4"]], 
"phoneNumbers" => ["value" => $entries[$tmp_id]["5"]] 

This should resolve the 400 error by sending a properly formatted JSON object to your API.

内容的提问来源于stack exchange,提问作者troxxx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:57:56