guzzle - Pardot API - 返回 JSON 而不是 XML
问题描述
我正在使用 GuzzleHttp 向 Pardots API 发送请求。
class PardotIntegration {
private $client;
private $apikey;
public function __construct() {
$this->client = new \GuzzleHttp\Client();
}
public function authenticate() {
$params = [
'email' => 'abc@example.com',
'user_key' => '3487328947239478927',
'password' => 'password'
];
$res = $this->client->post('https://pi.pardot.com/api/login/version/3', [
'form_params' => $params
]);
echo $res->getBody();
}
}
$pardot = new PardotIntegration;
$pardot->authenticate();
文档说明您可以从请求中返回 XML 或 JSON:http: //developer.pardot.com/#sharing-the-api-response-format
但是,我不知道如何返回 JSON 而不是默认的 XML。
我试过添加
$res = $this->client->post('https://pi.pardot.com/api/login/version/3', [
'headers' => [
'Accept' => 'application/json'
],
'form_params' => $params
]);
但这仍然返回 XML。
解决方案
他们的文档很糟糕。
我发现隐藏在您需要添加到正文参数的特定请求中:
'format' => 'json'
推荐阅读
- batch-file - BATCH,将文件副本创建到具有 VARIABLE NAME 的各个目录中
- html - 颜色选择器的问题
- android - 活动内容下方的调整按钮
- cookies - mvc 5 中的 Cookie
- javascript - react router v3中如何防止用户意外更改路由
- python - 机器人框架没有通过终端执行苹果脚本
- ios - 如何从 Codable 结构中捕获 init(来自解码器:解码器)中的错误?
- python - 如何制作 Python 正则表达式,有可选的启动要求?
- java - Maven 依赖项或 Eclipse 项目方面
- ag-grid - Ag-grid 恢复数据