【问题标题】:Create user using freshdesk PHP api使用freshdesk PHP api创建用户
【发布时间】:2018-04-18 06:29:57
【问题描述】:

我正在为我的一个项目实施freshdesk API。

我尝试使用freshdesk PHP API 创建新用户,但是当我在邮递员插件中运行代码并且我正在为我的项目使用SLIM 框架时出现内部服务器错误。

请检查我的以下代码并提前感谢!

PHP:

<?php    
 $api_key = "MY_API_KEY";
 $password = "xyz123M";
 $mydomain = "MY_DOMAIN";

 $custom_fields = array(
 "department" => "Accounts"
 );
 $contact_data = json_encode(array(
    "user_name" => "Sri Jitthu",
    "email_id" => "srij@mydomain.com"
 ));

 $url = "https://$mydomain.freshdesk.com/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, "$api_key:$password");
 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);
  ?>

【问题讨论】:

  • 还有其他错误吗?
  • @Andrew 是的,我收到来自 freshdesk 的 user_name 和 email_id 参数的验证错误

标签: php freshdesk


【解决方案1】:

我使用了相同的freshdesk PHP API。我认为你定义的参数是新桌不接受的,我们需要按照他们定义的参数传递。

你应该传递name而不是传递user_name,而不是传递email_id你应该传递email

当您在 API 中传递自定义字段时,这意味着您的帐户中应该存在或预定义的字段,那么只有它才会起作用。例如,您在 API 中传递的 departmentAccounts,这意味着应该在您的 Freshdesk 帐户中预定义 Accounts

检查下面的代码并尝试。希望对你有帮助。

PHP:

<?php    
 $api_key = "YOUR_API_KEY";
 $password = "xyz";
 $yourdomain = "YOUR_DOMAIN";

 $custom_fields = array(
 "department" => "Accounts"
 );
 $contact_data = json_encode(array(
    "name" => "Sri Jitthu",
    "email" => "srij@mydomain.com"
 ));

 $url = "https://$yourdomain.freshdesk.com/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, "$api_key:$password");
 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);
  ?>

在发送请求之前,还要再检查一次您的 API 密钥和密码是否正确?

【讨论】:

  • 好吧!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-06
  • 1970-01-01
  • 1970-01-01
  • 2016-03-23
  • 1970-01-01
  • 2012-07-29
  • 1970-01-01
相关资源
最近更新 更多