API : Create user + PHP sample script



I wanted to create a account using API :

But not sure how to execute this script:


curl -u {username}:{password} -H "Content-Type: application/xml" -d"{UserName}{}" -X POST http://{}/contacts.xml


Is it possible to do the same using PHP script OR do you provide any sort of PHP documentaion to execute above script in php. I am familiar with PHP curl ( but not sure how to send request and get response from freshdesk using your current API sample script.


Thanks in advance.

Best Answer


You can find some sample repos on API  here : . 


3 people have this question

I have tried above thing using PHP curl shell_exec commad but it isn't working:


$var =  shell_exec('curl -u -H "Content-Type: application/xml" -d"rahul1" -X POST');


echo $var;

It shows blank page.

This should help you:

$urltopost = "https://$yourfreshdeskurl/contacts.xml";
$header[] = "Content-type: application/xml";

$ch = curl_init ($urltopost);
curl_setopt ($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_USERPWD, "$email:$password");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);

$returndata = curl_exec ($ch);

This should get you authenticated and receive a response. 

 I coded create a ticket via API  same as your source with java but I received error:

<!-- This file lives in public/500.html --> 

<div class="dialog">  <h1>We're sorry, but something went wrong.</h1>  <p>We've been notified about this issue and we'll take a look at it shortly.</p>  </div>

Please help me 

my source:

 // Create an instance of

HttpClient.  HttpClient client = new HttpClient(); 

// Create a method instance. 

PostMethod method = new PostMethod(""); 

NameValuePair[] data = {  new NameValuePair("username", "***@****.net"),  new NameValuePair("password", "*****")  }; 


method.addRequestHeader("Content-type", "application/xml");    

 // Execute the method. 

int statusCode = client.executeMethod(method);

Thank you so much

Best Regards

The reason you may be seeing a blank page if you are using Junior Hemelaer's example is because it returns xml which will not render as visible text in a browser.  try viewing the source or changing xml to json to see the results!

I have a repo here which provides a simple freshdesk wrapper: Most of the methods are geared around solutions.  But, extending for other objects would be really easy.

Here's the simple rest wrapper function in particular:

* @param $urlMinusDomain - should start with /... example /solutions/categories.xml
* @param $method - should be either GET, POST, PUT (and theoretically DELETE but that's untested).
* @param string $postData - only specified if $method == POST or PUT
* @param $debugMode {bool} optional - prints the request and response with headers
* @return the raw response
 private function restCall($urlMinusDomain, $method, $postData = '',$debugMode=false) {
 $url = "https://{$this->domain}$urlMinusDomain";

 //print "REST URL: " . $url . "\n";
 $header[] = "Content-type: application/xml";
 $ch = curl_init ($url);

 if( $method == "POST") {
 if( empty($postData) ){
 $header[] = "Content-length: 0"; // <-- seems to be unneccessary to specify this... curl does it automatically
 curl_setopt ($ch, CURLOPT_POST, true);
 curl_setopt ($ch, CURLOPT_POSTFIELDS, $postData);
else if( $method == "PUT" ) {
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "PUT" );
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postData);
else if( $method == "DELETE" ) {
 else {
 curl_setopt ($ch, CURLOPT_POST, false);

 curl_setopt($ch, CURLOPT_USERPWD, "{$this->username}:{$this->password}");
 curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

 if( $debugMode ) {
 // CURLOPT_VERBOSE: TRUE to output verbose information. Writes output to STDERR,
 // or the file specified using CURLOPT_STDERR.
 curl_setopt($ch, CURLOPT_VERBOSE, true);
 $verbose = fopen('php://temp', 'rw+');
 curl_setopt($ch, CURLOPT_STDERR, $verbose);

 $returndata = curl_exec ($ch);

 if( $debugMode ) {
 $verboseLog = stream_get_contents($verbose);
 print $verboseLog;

 $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 // curl_close($http);
 if( !preg_match( '/2\d\d/', $http_status ) ) {
 print "ERROR: HTTP Status Code == " . $http_status . " (302 also isn't an error)\n";

 // print "\n\nREST RESPONSE: " . $returndata . "\n\n";

 return $returndata;



You can find some sample repos on API  here : .