【问题标题】:PHP OAuth Library JSON parsingPHP OAuth 库 JSON 解析
【发布时间】:2018-07-07 22:05:53
【问题描述】:

我正在尝试使用 PHP OAuth 库 (https://www.phpclasses.org/package/7700-PHP-Authorize-and-access-APIs-using-OAuth.html) 将一些数据发送到我的 api,但每次发生错误时

错误:无法访问 API 调用:返回了意外的响应状态 400 响应:{"message":"Error conversion value \"{\"lastName\":\"Smith\",\ "firstName\":\"John\",\"webAddresses\":{\"value\":\"john@smith.com\"},\"phoneNumbers\":{\"value\":\" 0666864\"}}\" 输入...

这是我的代码:

$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"]
  ])
);
$json = json_encode($fields);

$success = $client->CallAPI(
  'https://myurl.com/v2/api',
  'POST', $json, array('FailOnAccessError'=>true, 'RequestHeaders' => $params,'RequestContentType' => 'application/json', 'FollowRedirection' => true, ), $user);

任何想法我做错了什么? 问候

【问题讨论】:

  • “将值 [...] 转换为类型时出错...” - 转换为什么类型?您可能在这里遗漏了错误消息中最重要的部分。
  • @CBroe - 输入“Company_Name.Docs.PublicApi.ViewModels.Write.Persons.Create.PersonModel”。

标签: php json oauth oauth-2.0


【解决方案1】:

我找到了解决方案:)

我在发送请求之前删除了 json 编码,并从标头中删除了“内容类型”。

工作代码:

$params = array(
  'x-api-key' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  '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"]
  ])
);

$success = $client->CallAPI(
  'https://myurl.com/v2/api',
  'POST', $fields, array('FailOnAccessError'=>true, 'RequestHeaders' => $params,'RequestContentType' => 'application/json', 'FollowRedirection' => true, ), $user);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-21
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    相关资源
    最近更新 更多