在 PHP 中使用 Freshdesk API 创建新用户
Create new user using the Freshdesk API in PHP
这是我的代码:
$contact_data = json_encode(array(
"name" => "Jimmy Jimmy",
"email" => "jimmy@example.com",
"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
在 freshdesk 中创建新联系人。知道我做错了什么吗?谢谢
[更新]
$contact_data = json_encode(array(
"name" => "Jimmy Jimmy",
"email" => "jimmy@example.com",
"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"}]}
嗯,您已经有了答案 - 您确实阅读了错误消息是吗?
回复:
{
"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
都需要是 String
并且不能为空。
这些字段不是 Freshdesk 联系人实体的默认字段,但可能是根据需要在后端定义的(检查 Freshdesk 后端的管理 > 客户字段)
这意味着我们必须将它们定义为 custom_fields
,如 Freshdesk API 文档中所述 here。
这意味着您的 POST 数组看起来像这样
$contact_data = json_encode(array(
'name' => 'Jimmy Jimmy',
'email' => 'jimmy@example.com',
'custom_fields' => [
// put all your custom fields here
'last_name' => 'Jimmy',
'life_cycle_status' => 'value'
]
));
这是我的代码:
$contact_data = json_encode(array(
"name" => "Jimmy Jimmy",
"email" => "jimmy@example.com",
"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
在 freshdesk 中创建新联系人。知道我做错了什么吗?谢谢
[更新]
$contact_data = json_encode(array(
"name" => "Jimmy Jimmy",
"email" => "jimmy@example.com",
"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"}]}
嗯,您已经有了答案 - 您确实阅读了错误消息是吗?
回复:
{
"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
都需要是 String
并且不能为空。
这些字段不是 Freshdesk 联系人实体的默认字段,但可能是根据需要在后端定义的(检查 Freshdesk 后端的管理 > 客户字段)
这意味着我们必须将它们定义为 custom_fields
,如 Freshdesk API 文档中所述 here。
这意味着您的 POST 数组看起来像这样
$contact_data = json_encode(array(
'name' => 'Jimmy Jimmy',
'email' => 'jimmy@example.com',
'custom_fields' => [
// put all your custom fields here
'last_name' => 'Jimmy',
'life_cycle_status' => 'value'
]
));