-1

I'd like to use the FreshDesk API, say, the 'create a ticket' command.
I have used POST cUrl before, but the example given there confuses me and I am not quite sure how to concretely make a cUrl call as provided there,

curl -u user@yourcompany.com:test -H "Content-Type: application/json" -d '{ "helpdesk_ticket": { "description": "Details about the issue...", "subject": "Support Needed...", "email": "tom@outerspace.com", "priority": 1, "status": 2 }, "cc_emails": "ram@freshdesk.com,diana@freshdesk.com" }' -X POST http://domain.freshdesk.com/helpdesk/tickets.json

I noticed this is a shell command, and I do not have access to shell commands.
However, I have successfully been able to use cUrl POST API's before (with Disqus).
There, API calls would look like

$thread = "param1";
$remote_auth_s3 = "param2";
$forum = "param3";
$api = "param4";
$url = 'http://disqus.com/api/3.0/posts/create.json';

$fields = array(
    'api_secret'=>urlencode($api),
    'thread'=>urlencode($thread),
    'remote_auth'=>urlencode($remote_auth_s3),
    'message'=>urlencode($message)
);

foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($ch);
curl_close($ch);

$results = json_decode($result, true);
if ($results === NULL) die('Error parsing json');

This seemed all very logical, but how am I supposed to convert the shell command to this format of a cUrl call?
This might be a very stupid question, but also a honest one. I did find two others with the same problem, but they did not need to also include POST Parameters, so please don't mark this as a duplicate.

I hope someone is able to solve my dilemma.

Community
  • 1
  • 1
Isaiah
  • 1,852
  • 4
  • 23
  • 48
  • Where exactly is your problem? This is fairly easy, just set the headers with `CURLOPT_HTTPHEADER` and the post data as an array in `CURLOPT_POSTFIELDS` (use `json_encode` if necessary). – René Roth May 31 '14 at 18:39
  • @RenéRoth would this mean I'd have to add `description` and `subject` etc as array of `helpdesk_ticket` which is in an array itself? – Isaiah May 31 '14 at 18:41
  • I posted an answer that addresses your issues fully, I hope. – René Roth May 31 '14 at 18:49

2 Answers2

1
// set HTTP auth credentials
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
// set Content-Type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
// use POST method
curl_setopt($ch, CURLOPT_POST, true);
// send your JSON string in the request, do not urlencode it
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
lafor
  • 12,472
  • 4
  • 32
  • 35
  • I get your answer, and this indeed helps me, but it was not the proper format, though it was essential as well. Thank you! – Isaiah Jun 01 '14 at 07:07
0

Use curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($fields)); to send a json encoded array to the API.

Your example array would look like:

$fields=array(
 'helpdesk_ticket'=>array(
  'description'=>'Details about the issue...',
  'subject'=>'Support needed...',
  'email'=>'tom@outerspace.com',
  'priority'=>1,
  'status'=>2
 ),
 'cc_emails'=>'ram@freshdesk.com,diana@freshdesk.com'
);
René Roth
  • 1,979
  • 1
  • 18
  • 25