From 9a79e3696ef7fd07eeb994865c2c12cee37d7272 Mon Sep 17 00:00:00 2001 From: nepyro Date: Thu, 21 Sep 2017 08:19:00 -0500 Subject: [PATCH] Created new test script testapiauth.php Added an example PHP script with more detailed explanation on how to authenticate via the API --- php/testapiauth.php | 57 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 php/testapiauth.php diff --git a/php/testapiauth.php b/php/testapiauth.php new file mode 100644 index 0000000..857f523 --- /dev/null +++ b/php/testapiauth.php @@ -0,0 +1,57 @@ +name."\n"; + +function finale_auth($host, $path, $username, $password) { + + // Create curl handle with options used for all requests. Finale API authentication is cookie based, so cookies need to be enabled + $ch = curl_init(); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_COOKIEJAR,""); + + + // Login to Finale + curl_setopt($ch, CURLOPT_URL, $host.$path); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode( array( "username" => $username, "password" => $password))); + curl_setopt($ch, CURLOPT_HEADER, 1); + + $response = curl_exec($ch); + + $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + if ($status_code != 200) exit("FAIL: authentication error statusCode=$status_code\n"); + + $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); + $header = substr($response, 0, $header_size); + $body = substr($response, $header_size); + + // Pull out all JSESSIONID cookie headers + preg_match_all('|Set-Cookie: JSESSIONID=(.*);|U', $header, $cookies); + + // Don't return headers in future http requests to keep them simple (reuse to curl handle for automatic cookie handling) + curl_setopt($ch, CURLOPT_HEADER, 0); + + return array( "curl_handle" => $ch, "auth_response" => json_decode($body), "host" => $host, "session_secret" => array_pop($cookies[1]) ); +} + +// Example program just tests authentication +// +?>