Last active
August 29, 2015 14:15
-
-
Save mchristie/a8dc304c67d7ce136057 to your computer and use it in GitHub Desktop.
GeckoForm JSON POST example
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
<? | |
// Build an array of the information you need to include in the request | |
$intended_parameters = [ | |
'include' => 'pages,pages.fields,workflows,conditions,fields:all,fields.option', | |
'name' => 'Example form update', | |
'workflows' => [ | |
// Omitted | |
], | |
'pages' => [ | |
[ | |
'order' => 1, | |
'title' => 'Page 1', | |
'show_title' => 1, | |
'description' => 'Example', | |
'fields' => [ | |
// Omitted | |
] | |
] | |
] | |
]; | |
// Retrieve your token information from a secure storage location | |
$token_information = json_decode(Session::get('token_information')); | |
/* | |
This will have a data structure similar to this | |
$token_information = [ | |
'key' => 'KfdS1T0N2buSUSt0bRd2oGMhmbO6gVhp05oByr32G3VUWEsMcmV7nthIM8cINk6M', | |
'secret' => 'R1rkbq3oavo13PiX6A0sesqeUD6ya6I7OGJmZqXSGcdnMU5OZ7n1De8opTJDmR06', | |
// Using a timestamp is an easy way to ensure the nonce always increments | |
'nonce' => 1423823278321 | |
]; | |
*/ | |
// Increment the nonce to ensure it is higher than the previously used nonce | |
$token_information['nonce'] += 2; | |
// Add the nonce and key to the parameters | |
// Do NOT add the secret to the array which will be sent to the API | |
$intended_parameters['key'] = $intended_parameters['key']; | |
$intended_parameters['nonce'] = $intended_parameters['nonce']; | |
// JSON encode the intended parameters for sending to the API | |
$json_body = json_encode($intended_parameters); | |
// Append the secret to the JSON for generating the signature | |
$sign_str = $json_body . $token_information['secret']; | |
// Create an sha256 hash of this string which will be our request signature | |
$signature = hash('sha256', $sign_str); | |
// We can not POST to the API using the JSON as the post body and include the signature in the query string | |
// e.g. /forms/123?signature=$signature |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment