2017-04-19 116 views
0

我试图从Pipedrive API为特定日期范围提交交易。我正在通过编辑(或创建/删除)一个过滤器来实现这一点,并通过具有我想要的日期范围的API,然后在随后的请求中传入该过滤器来提取所有交易。Pipedrive API PUT和POST请求过滤器端点不工作

“交易”端点完美运作。我的问题是,API似乎不喜欢我为“过滤器”端点传入的“条件”参数 - 但只有当我使用cURL(在我自己的代码中)或Postman时。如果我在API文档中测试“编辑过滤器”或“创建过滤器”端点,那么当我从代码中将JSON对象复制并粘贴到“条件”参数中时,它们都按预期工作。但是,如果我使用cURL或Postman,PUT端点只是返回我正在编辑的过滤器而不进行编辑,并且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); 

这是“条件”参数为“创建过滤器”端点描述:

“过滤条件如JSON对象它需要最小的结构,如下所示:

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

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

{"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文档测试中时,两个端点完美地工作 - 在我自己的代码中,任何帮助都可以ciated。

编辑:这是我从卷曲获得了“创建过滤器”端点响应:

{#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包装器,并获得了相同的结果。

+0

打印Curl抛出的错误。 http://stackoverflow.com/questions/3987006/how-to-catch-curl-errors-in-php – Samir

+0

@萨米尔刚刚编辑的OP与我得到的响应。没有错误被抛出。 –

回答

1

Pipedrive工程师在这里。这是我测试的一个例子..

<?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是原始发布
  2. 我加Content-Type头
  3. 我加visible_to PARAM

过滤API是相当复杂的,我们正在努力改进文档。希望这有助于

+0

这工作完美。谢谢你的快速反应! –

0

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

更改CURLPOSTGET方法,它应该工作。

+0

我不想试图与这个电话进行交易。我正在进行两个调用:一个用于创建/编辑具有自定义日期范围的过滤器,另一个用于使用该过滤器获取交易。 第二个电话(获得交易)完美地工作。然而,第一个(创建/编辑过滤器)对我无效。 –