2016-11-29 78 views
1

中的Freshdesk API新用戶這是我的代碼:創建使用PHP

 $contact_data = json_encode(array(
      "name" => "Jimmy Jimmy", 
      "email" => "[email protected]", 
      "phone" => "555-555-555", 
      "mobile" => "312-312-213" 
     )); 

     $url = $domain."api/v2/contacts"; 
     $ch = curl_init($url); 
     $header[] = "Content-type: application/json"; 
     curl_setopt($ch, CURLOPT_POST, true); 
     curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
     curl_setopt($ch, CURLOPT_HEADER, true); 
     curl_setopt($ch, CURLOPT_USERPWD, "$apiKey"); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $contact_data); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     $server_output = curl_exec($ch); 
     $info = curl_getinfo($ch); 
     $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); 
     $headers = substr($server_output, 0, $header_size); 
     $response = substr($server_output, $header_size); 
     if($info['http_code'] == 201) { 
      echo "Contact created successfully, the response is given below \n"; 
      echo "Response Headers are \n"; 
      echo $headers."\n"; 
      echo "Response Body \n"; 
      echo "$response \n"; 
     } else { 
      if($info['http_code'] == 404) { 
       echo "Error, Please check the end point \n"; 
      } else { 
       echo "Error, HTTP Status Code : " . $info['http_code'] . "\n"; 
       echo "Headers are ".$headers."\n"; 
       echo "Response is ".$response; 
      } 
     } 
     curl_close($ch); 

當我執行這段代碼,我收到此錯誤消息:提及

Response is {"description":"Validation failed","errors":[{"field":"last_name","message":"It should be a/an String","code":"missing_field"},{"field":"life_cycle_status","message":"It should be a/an String","code":"missing_field"}]} 

文檔什麼這些字段:last_name & life_cycle_status在新鮮工作臺中創建新聯繫人。任何想法我做錯了什麼? THX

[更新]

$contact_data = json_encode(array(
      "name" => "Jimmy Jimmy", 
      "email" => "[email protected]", 
      "phone" => "555-555-555", 
      "mobile" => "312-312-213" 
      "life_cycle_status" => "asdasdsa", 
      "last_name" =>"dasdasdad" 
     )); 

有了這些新的項目,我得到這個消息的錯誤:

Response is {"description":"Validation failed","errors":[{"field":"life_cycle_status","message":"Unexpected/invalid field in request","code":"invalid_field"},{"field":"last_name","message":"Unexpected/invalid field in request","code":"invalid_field"}]} 

回答

1

這些字段不是默認的的Freshdesk聯繫實體但是,很可能在後端需要定義(檢查管理員>中的Freshdesk的後端客戶字段)

這意味着我們必須將其定義爲custom_fields如Freshdesk API文檔here中所示。

這意味着你的POST陣列會是這個樣子

$contact_data = json_encode(array(
    'name' => 'Jimmy Jimmy', 
    'email' => '[email protected]', 
    'custom_fields' => [ 
     // put all your custom fields here 
     'last_name' => 'Jimmy', 
     'life_cycle_status' => 'value' 
    ] 
)); 
1

那麼你已經有答案了 - 你沒看該錯誤信息是嗎?

響應:

{ 
    "description": "Validation failed", 
    "errors":[ 
     { 
     "field":"last_name", 
     "message":"It should be a/an String", 
     "code":"missing_field" 
     }, 
     { 
     "field":"life_cycle_status", 
     "message":"It should be a/an String", 
     "code":"missing_field" 
     } 
    ] 
} 

含義:

last_namelife_cycle_status都需要一個String並不能爲空。

+0

你可以檢查我的帖子嗎?我更新了它 –