1KA_F2F/admin/survey/api/api_test.php
2020-08-14 13:36:36 +02:00

125 lines
3.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
*
* Skripta za testiranje API-ja
*
**/
// Nastavimo url api-ja
//$api_url = 'http://localhost/admin/survey/api/api.php';
$api_url = 'http://test.1ka.si/admin/survey/api/api.php';
//$api_url = 'https://www.1ka.si/admin/survey/api/api.php';
// Nastavimo identifier in key userja
$identifier = 'a1c3b90fdae3c45d';
$private_key = 'e60032141a7aae518f9938636b963fc14d58d40025dc7b6b82f4e69b8a42599e';
// Nastavimo parametre
$ank_id = '8086';
$action = 'getSurveyQuestions';
// Izvedemo klic (GET ali POST)
$result = executeGET();
//$result = executePOST();
// Izpisemo rezultat
echo 'REZULTAT (RAW):<br />';
echo $result;
echo '<br /><br /><br />';
// Izvedemo se json decode in izpisemo array
$result_array = json_decode($result, true);
echo 'REZULTAT (JSON DECODE):';
var_dump($result_array);
// GET
function executeGET(){
global $api_url;
global $identifier;
global $private_key;
global $ank_id;
global $action;
// GET params
$params = 'action='.$action; // Funkcija, ki jo želimo izvesti
$params .= '&ank_id='.$ank_id; // ostali parametri potrebni za klic funkcije (id ankete, vprašanja...)
// Pripravimo podatke za hashiranje
$request_method = 'GET';
$request = $api_url.'?'.$params;
$data = $request_method . $request;
// Izracunamo hash (token)
$token = hash_hmac('sha256', $data, $private_key);
// Pripravimo klic dodamo parametra »identifikator« in »token«
$ch = curl_init($request.'&identifier='.$identifier.'&token='.$token);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request_method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Izvedemo klic
$result = curl_exec($ch);
return $result;
}
// POST
function executePOST(){
global $api_url;
global $identifier;
global $private_key;
global $ank_id;
global $action;
// GET params
$params = 'action='.$action; // Funkcija, ki jo želimo izvesti
$params .= '&ank_id='.$ank_id; // ostali parametri potrebni za klic funkcije (id ankete, vprašanja...)
// POST data
$post_data = array(
"email" => "peter@1ka.si",
"firstname" => "Peter",
"lastname" => "Hrvatin"
);
/*$post_data = array(
"email" => "BRATISLAVA.LAMAC@PLANEOELEKTRO.SK",
"firstname" => "MONIKA",
"lastname" => "ŽIAKOVÁ",
"expired" => "7",
"param_string" => "rekid=5406729&dnid=10352449&brand=MORA&ser=Žiak Pavel Žiak - GOREMO (Víglaš)&serenota=Pavel Žiak - GOREMO&sercenter=SC Slovakia&drzava=Slovakia"
);*/
// Pripravimo podatke za hashiranje
$request_method = 'POST';
$request = $api_url.'?'.$params;
$raw_post_data = http_build_query($post_data);
$data = $request_method . $request . $raw_post_data;
// Izracunamo hash (token)
$token = hash_hmac('sha256', $data, $private_key);
// Pripravimo klic dodamo parametra »identifikator« in »token«
$ch = curl_init($request.'&identifier='.$identifier.'&token='.$token);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data)); // JSON string za POST
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request_method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Izvedemo klic
$result = curl_exec($ch);
return $result;
}