2017-04-19 46 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

我不想試圖與這個電話進行交易。我正在進行兩個調用:一個用於創建/編輯具有自定義日期範圍的過濾器,另一個用於使用該過濾器獲取交易。 第二個電話(獲得交易)完美地工作。然而,第一個(創建/編輯過濾器)對我無效。 –