首页 > 解决方案 > 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。

标签: guzzlepardot

解决方案


他们的文档很糟糕。

我发现隐藏在您需要添加到正文参数的特定请求中:

'format' => 'json'


推荐阅读