Skip to content

Instantly share code, notes, and snippets.

@nullmem
Created May 11, 2017 12:20
Show Gist options
  • Save nullmem/37861153aaab3f7e72956ad95b4c0793 to your computer and use it in GitHub Desktop.
Save nullmem/37861153aaab3f7e72956ad95b4c0793 to your computer and use it in GitHub Desktop.
Simple Zendesk API hook for contact form
<?php
parse_str(file_get_contents('php://input'), $input);
$data = array(
'ticket' => array(
'requester' => array(
'name' => $input['First_Name'].' '.$input['Last_Name'],
'email' => $input['Email'],
'phone' => $input['Phone_Number'],
),
'subject' => 'Website Contact',
'comment' => array(
'body' => $input['Message'],
),
),
);
$json = json_encode($data);
$api = curl_init('https://subdomain.zendesk.com/api/v2/tickets.json');
curl_setopt($api, CURLOPT_USERPWD, 'email@address.com/token:dF6hkoHxT71oQM3JthpTr3axzdDfvVmHdMg16yS8');
curl_setopt($api, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($api, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($api, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($api, CURLOPT_POSTFIELDS, $json);
$results = json_decode(curl_exec($api));
curl_close($api);
if (isset($results->error)) exit(1);
?>
@nullmem
Copy link
Author

nullmem commented May 11, 2017

If field names have space in elementor it will be replaced with underscore as in my script.

Don't forget to change email address and API key with the one on line 20
Also don't forget to change subdomain to your subdomain on line 19

Optional: I would recommend putting email/API key into a file out of web root and change it in script to:
trim(file_get_contents('/safe/place/.zendesk-key'))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment