Created
December 18, 2013 19:35
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' => '[email protected]', | |
'from' => '[email protected]', | |
'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