首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用freshdesk PHP创建用户

使用freshdesk PHP创建用户
EN

Stack Overflow用户
提问于 2018-04-18 06:29:57
回答 1查看 231关注 0票数 1

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

使用freshdesk PHP,我试图创建新的用户,但是当我在postman插件中运行代码时,我得到了内部服务器错误,并且在我的项目中使用了瘦框架。

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

PHP:

代码语言:javascript
复制
<?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);
  ?>
EN

回答 1

Stack Overflow用户

发布于 2018-04-18 06:40:17

我使用过相同的freshdesk PHP API。我认为您定义的参数是新的desk.We所不能接受的,因此需要传递它们定义的参数。

与其传递user_name,不如传递name,而不是传递email_id,而是传递email

此外,当您在API中传递自定义字段时,意味着应该在您的帐户中存在或预定义该字段,那么只有它才能工作。对于department,您要传递的是API中的Accounts,这意味着Accounts应该在您的Accounts帐户中预定义。

检查下面的代码并尝试。希望它能帮到你。

PHP:

代码语言:javascript
复制
<?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密钥和密码,两者都是正确的还是错误的?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49892654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档