Pipedrive API PUT 和 POST 请求过滤器端点不工作

Pipedrive API PUT and POST requests for Filters endpoint not working

我正在尝试从 Pipedrive API 中提取特定日期范围内的交易。我通过 API 编辑(或 creating/deleting)一个具有我想要的日期范围的过滤器,然后在后续请求中传递该过滤器以提取所有交易来完成此操作。

"deals" 端点运行良好。我的问题是 API 似乎不喜欢我为 "filters" 端点传递的 "conditions" 参数——但前提是我使用 cURL(在我的自己的代码)或邮递员。如果我在 API 文档中测试 "edit filter" 或 "create filter" 端点,当我将 JSON 对象从我的代码复制并粘贴到"conditions" 参数。但是,如果我使用 cURL 或 Postman,PUT 端点只是 returns 我正在编辑的过滤器而不对其进行编辑,而 POST 端点会创建一个具有空条件的新过滤器。

这是我用于 POST 端点的 PHP 代码:

$data = [
    'name' => 'Custom date range',
    'type' => 'deals',
    'conditions' => '{
        "glue": "and",
        "conditions": [
            {
                "glue": "and",
                "conditions": [
                    {
                    "object": "deal",
                    "field_id": "12449",
                    "operator": "=",
                    "value": "won",
                    "extra_value": "null"
                    },
                    {
                      "object": "deal",
                    "field_id": "12455",
                    "operator": ">=",
                    "value": "2017-03-01",
                    "extra_value": "null"
                    },
                    {
                      "object": "deal",
                    "field_id": "12455",
                    "operator": "<=",
                    "value": "2017-03-10",
                    "extra_value": "null"
                    }
                ]
            },
            {
                "glue": "or",
                "conditions": []
            }
        ]
     }'
];

$ch = curl_init("https://api.pipedrive.com/v1/filters?api_token=$apiKey");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json;"));
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$response = curl_exec($ch);

这是 "create filter" 端点的 "conditions" 参数的描述:

》过滤条件为JSON对象。它要求的最小结构如下:

{"glue":"and","conditions":[{"glue":"and","conditions": [CONDITION_OBJECTS]},{"glue":"or","conditions":[CONDITION_OBJECTS]}]}

将 CONDITION_OBJECTS 替换为具有以下结构的 JSON 个对象:

{"object":"","field_id":"", "operator":"","value":"", "extra_value":""} or leave the array empty. 

根据对象类型,您应该使用另一个 API 端点来获取 field_id。您可以选择五种类型的对象:

"person", "deal", "organization", "product", "activity" 

您可以根据您拥有的字段类型使用这些类型的运算符:

"IS NOT NULL", "IS NULL", "<=", ">=", "<", ">", "!=", "=", "LIKE '%$%'", "NOT LIKE '%$%'", "LIKE '$%'", "NOT LIKE '$%'", "LIKE '%$'", "NOT LIKE '%$'".

要更好地了解过滤器的工作原理,请尝试直接从 Pipedrive 应用程序创建它们。"

POST端点的"conditions"参数相同。同样,当我将那个大的 JSON 对象粘贴到 API 文档测试中时,两个端点都可以正常工作——但不是在我自己的代码中。任何帮助将不胜感激。

编辑:这是我从 cURL 获得的 "create filter" 端点的响应:

{#233 ▼
    +"id": 60
    +"name": "Custom date range"
    +"active_flag": true
    +"type": "deals"
    +"temporary_flag": null
    +"user_id": 504569
    +"add_time": "2017-04-19 11:18:10"
    +"update_time": "2017-04-19 11:18:10"
    +"visible_to": "7"
    +"custom_view_id": null
    +"conditions": {#219 ▼
        +"glue": "and"
        +"conditions": array:2 [▼
            0 => {#230 ▼
                +"glue": "and"
                +"conditions": []
            }
            1 => {#223 ▼
                +"glue": "or"
                +"conditions": []
            }
        ]
    }
}

没有错误,但如您所见,条件为空。我也刚刚尝试了为 Pipedrive API 创建的 PHP 包装器并得到了相同的结果。

要拉动交易,您应该使用 GET 方法。 POST 用于添加交易。

CURLPOST 更改为 GET 方法,它应该可以工作。

这里是管道驱动工程师。这是我测试的一个例子..

<?php
$data = '
{
    "name":"Custom filter less than 1000",
    "type":"deals",
    "visible_to":1,
    "conditions":{
        "glue": "and",
        "conditions":[
            {
                "glue": "and",
                "conditions": [
                        {
                            "object": "deal",
                            "field_id": "12452",
                            "operator": "<",
                            "value": 1000,
                            "extra_value": null
                        }
                    ]
                },
            {
                "glue": "or",
                "conditions": []
            }
        ]
    }
}
';

$ch = curl_init("https://api.pipedrive.com/v1/filters?api_token=xxx");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json;', 'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST,           1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);

echo $response;

注意

  1. 整个json是raw-posted
  2. 我加了Content-Typeheader
  3. 我添加了visible_to参数

过滤 API 非常复杂,我们正在努力改进文档。希望这有帮助