Reputation:
I'm implementing freshdesk API's for one of my project.
Using freshdesk PHP API I tried to create new user but i'm getting internal server error when I run the code in postman plugin and i'm using SLIM framework for my project.
Please check my below code and thanks in advance!
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" => "[email protected]"
));
$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);
?>
Upvotes: 1
Views: 276
Reputation:
I have used same freshdesk PHP API. I think the parameters that you have defined is not acceptable by fresh desk.We need to pass the parameters as they defined.
Instead of passing user_name
you should pass name
also instead of passing email_id
you should pass email
.
As well as when you're passing custom fields in your API means that should exists or predefined in your account then only it will work. for ex you're passing department
is Accounts
in your API which means the Accounts
should be predefined in your freshdesk account.
Check the below code and try. Hope it will help you.
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" => "[email protected]"
));
$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);
?>
Also check your API key and password one more time before sending the request whether both are correct or wrong?
Upvotes: 0