Skip to content

Instantly share code, notes, and snippets.

@tstachl
Created December 18, 2013 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tstachl/8028418 to your computer and use it in GitHub Desktop.
Save tstachl/8028418 to your computer and use it in GitHub Desktop.
This php script shows how you search for a customer before creating the case, however it doesn't plan for the customer not existing, you'd have to check the response for total_entries == 0 or an empty entries array and create the customer accordingly.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://yoursite.desk.com/api/v2/customers/search?email=thomas%40stachl.me");
curl_setopt($ch, CURLOPT_PORT, 443);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_USERPWD, "email:password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$customer_search_result = json_decode(curl_exec($ch));
$customer_path = $customer_search_result->_embedded->entries[0]->_links->self;
$case = array(
'type' => 'email',
'subject' => 'Email Case Subject',
'priority' => 4,
'status' => 'open',
'labels' => array('Spam', 'Ignore'),
'message' => array(
'direction' => 'in',
'body' => 'Example Body',
'to' => 'support@example.com',
'from' => 'jdoe@example.com',
'subject' => 'My email subject'
),
'_links' => array(
'customer' => $customer_path
)
);
$case_string = json_encode($case);
echo $case_string;
echo "\n\n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://yoursite.desk.com/api/v2/cases");
curl_setopt($ch, CURLOPT_PORT, 443);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_USERPWD, "email:password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $case_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($case_string)
)
);
$return = curl_exec($ch);
echo $return;
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment