Last active
February 24, 2017 18:32
-
-
Save whatnickcodes/841c75d8bd4c8b4118f49f2de7f907a9 to your computer and use it in GitHub Desktop.
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 | |
// Param check / validation | |
if (!isset($_POST['email']) || !isset($_POST['zip'])) | |
wp_send_json(array('is_true' => FALSE, 'Not enough params sent.')); | |
// Rate Limit: 100 requests to a nation every 10 seconds. | |
// Expiration: Check on this | |
$access_token = 'TOKEN_GOES_HERE'; | |
$nation = 'scotch'; | |
//put in your nation slug and token | |
$endpoint = 'https://'.$nation.'.nationbuilder.com/api/v1/people?access_token='.$access_token; | |
// Get request | |
$data = array( | |
'person' => array( | |
'email' => $_POST['email'], | |
'registered_address' => array( | |
'zip' => $_POST['zip'] | |
) | |
), | |
); | |
$ch = curl_init($endpoint); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); | |
curl_setopt($ch, CURLOPT_TIMEOUT, '10'); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
'Content-Type: application/json', | |
'Accept: application/json' | |
) | |
); | |
$json_data = json_encode($data); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data); | |
$json_response = curl_exec($ch); | |
curl_close($ch); | |
$response = json_decode($json_response, TRUE); | |
if ($response) : | |
wp_send_json(array( | |
'is_true' => TRUE, | |
'message' => 'User saved to NationBuilder' | |
)); | |
else : | |
wp_send_json(array( | |
'is_true' => FALSE, | |
'message' => 'User not saved to NationBuilder' | |
)); | |
endif; | |
////// IMPORTANT //////////////// | |
// IF this isn't in wordpress you'll need to uncomment this | |
// function wp_send_json($data) { | |
// header('Content-Type: application/json'); | |
// echo json_encode($data); | |
// exit;; | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment