DAC
10/17/2016 - 10:42 PM

main.php

<?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);

//
// fetch data function
//
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;
};

//
//Exercise 1: Create, update, and delete a person
//


//
// 1.1: Create a person 
//

// body
$data               = array(
    'person'        => array(
      "last_name"   => "Guffaw",
      "first_name"  => "Paul",
      "sex"         => "F",
      "email"       => "hogan.wp@gmail.com",
      "signup_type" => 0
    )
);
$json_data = json_encode($data); 

$new_person = fetchData('POST', $json_data, $ch, $headers);


//
// 1.2 Update that person 
//

// body
$data               = array(
    'person'        => array(
      "last_name"   => "Linger",
    )
);

$json_data      = json_encode($data); 
$url            = $baseApiUrl .'/api/v1/people/' . $new_person['person']['id'] . '?access_token=' . $token;
$ch             = curl_init($url);
$updated_person = fetchData('PUT', $json_data, $ch, $headers);


//
// 1.3 Delete that person
//

$deleted_person = fetchData('DELETE', null, $ch, $headers);




//
// Exercise 2: Create event, make event editable to users
//


//
// 2.1: Create event
//

//body
$event_data               =  array(
  "event"                 => array(
    "status"              => "unlisted",
    "name"                => "New Post-election support day",
    "intro"               => "Get ready for the next four years",
    "time_zone"           => "Pacific Time (US & Canada)",
    "start_time"          => "2016-11-09T17:00:00-00:00",
    "end_time"            => "2016-11-09T19:00:00-00:f00",
    "contact"             => array(
      "name"              => "Phil Pho",
      "contact_phone"     => "5555555555",
      "show_phone"        => true,
      "contact_email"     => "contact@venue.com",
      "email"             => "contact@venue.com",
      "show_email"        => true
    ),
    "rsvp_form"           => array(
      "phone"             => "optional",
      "address"           => "required",
      "allow_guests"      => true,
      "accept_rsvps"      => true,
      "gather_volunteers" => true
    ),
    "show_guests"         => true,
    "capacity"            => 80,
    "venue"               => array(
      "name"              => "Ralphs Parking Lot",
      "address"           => array(
        "address1"        => "123 Foo St",
        "city"            => "Pasadena",
        "state"           => "CA"
      )
    )
  )
);

$json_event_data = json_encode($event_data); 
$url             = $baseApiUrl .'/api/v1/sites/billhogan/pages/events' . '?access_token=' . $token;
$ch              = curl_init($url);
$new_event       = fetchData('POST', $json_event_data, $ch, $headers);
echo '<hr><hr><hr><pre>';
var_dump($new_event);
echo '</pre>';

//
// 2.2: Make event editable to users
//

?>
<form class="form" method="POST" action="<?php echo home_url(); ?>/form.php">
  <h3 class="form-signin-heading">Edit the event name:</h3>
  <input class="form-control" type="text"  name="event_title" placeholder="Event Title" value="<?php echo $new_event['event']['name']  ?>">
  <input type="hidden" name="event_id" value="<?php echo $new_event['event']['id']  ?>">
  <input type="hidden" name="event_status" value="<?php echo $new_event['event']['status']  ?>">
  <input type="hidden" name="event_time_zone" value="<?php echo $new_event['event']['time_zone']  ?>">
  <input type="hidden" name="event_start_time" value="<?php echo $new_event['event']['start_time']  ?>">
  <input type="hidden" name="event_end_time" value="<?php echo $new_event['event']['end_time']  ?>">
  <button class="btn" type="submit">Update Event</button>
</form>