Skip to content

Instantly share code, notes, and snippets.

@onurdemir
Last active August 23, 2017 20:53
Show Gist options
  • Save onurdemir/595696836282fb10034a to your computer and use it in GitHub Desktop.
Save onurdemir/595696836282fb10034a to your computer and use it in GitHub Desktop.
gravity and zendesk integration
<?php
/*
* ZENDESK API
*/
define("ZDAPIKEY", "YOUR Zendesk API Key");
define("ZDUSER", "YOUR ZENDESK USERNAME (e-mail)");
define("ZDURL", "https://yourdomain.zendesk.com/api/v2");
/*
* Here goes the Gravity Forms Functions
*/
add_action("gform_after_submission_1", "send_contact_to_zendesk", 10, 2); // Invoke ZENDESK for form #1
add_action("gform_after_submission_1", "ult_cu_disable_post_creation", 20, 2); // Disable entry creation for FROM id #1
/*
* Deletes entry created in WordPress Dashboard
*/
function ult_cu_disable_post_creation( $entry, $form ) {
GFAPI::delete_entry( $entry['id'] );
}
/*
* Prepares and sends data to Zendesk
*/
function send_contact_to_zendesk($entry,$form){
$create = json_encode(
array(
'ticket' => array(
'subject' => $entry[1]. '-' .$entry[8], // Title of ticket ids of fields you need in subject field ours was type of request - message title
'comment' => array(
"value"=> $entry[6] // content of the ticket
),
'requester' => array(
'name' => $entry[3], // Name of ticket creator
'email' => $entry[4] // email of ticket creator
)
)
)
);
$return = ZD_contact_us_curlWrap("/tickets.json", $create);
}
/*
* Zendesk post function
* TODO : use wp_remote_post
*/
function ZD_contact_us_curlWrap($url, $json){
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_MAXREDIRS, 10 );
curl_setopt($ch, CURLOPT_URL, ZDURL.$url);
curl_setopt($ch, CURLOPT_USERPWD, ZDUSER."/token:".ZDAPIKEY);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
$decoded = json_decode($output);
return $decoded;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment