DAC
10/17/2016 - 10:43 PM

Takes submitted form data and updates event record at Nation Builder

Takes submitted form data and updates event record at Nation Builder

<?php 
// configuration
$baseApiUrl = 'https://billhogan.nationbuilder.com';
$token      = "f59f8ef560b4d7acdab853b8573cf87f40f9a3c830af0b48be56f2f9a8aa5fa6";
$headers    = array("Content-Type: application/json","Accept: application/json");
$url        = $baseApiUrl .'/api/v1/people?access_token=' . $token;
$ch         = curl_init($url);

// function to call the api
function fetchData($request_type = null, $json_body = null, $ch, $headers) {
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request_type); 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
  curl_setopt($ch, CURLOPT_TIMEOUT, '10'); 
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $json_body);
  $json_response = curl_exec($ch);
  curl_close($ch);
  $response = json_decode($json_response, true);
  return $response;
};

// check if a form was submitted
if( !empty( $_POST ) ){

    // convert form data to json
    $postArray = array(
      "event" => array(
         "status"=> $_POST["event_status"],
         "name" => $_POST["event_title"],
         "time_zone"=> $_POST["event_time_zone"],
         "start_time"=>  $_POST["event_start_time"],
         "end_time"=> $_POST["event_end_time"]
        )
    ); 
    $json_event_data = json_encode( $postArray );

    $url       = $baseApiUrl .'/api/v1/sites/billhogan/pages/events/' . $_POST["event_id"] . '?access_token=' . $token;
    $ch        = curl_init($url);
    $updated_event = fetchData('PUT', $json_event_data, $ch, $headers);

 }; //end if _POST

?>