|
Which part of your code is responsible for sending those field values to API? Hi,
i hav eleft out that part,
"Server tab" sends to sa_api.php and that file look like this:
<?php
class sa_api
{
var $headers;
var $user_agent;
var $compression = 'gzip';
var $cookie_file;
var $proxy;
var $proto = 'http';
var $server = 'localhost'; function sa_api()
{
$this->headers[] = 'Accept: application/json';
$this->headers[] = 'Connection: Keep-Alive';
$this->headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
$this->user_agent = 'SA_WebGUI';
} function post($uri, $data_as_array)
{ $process = curl_init($this->proto . "://" . $this->server. "/" .$uri);
curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers);
curl_setopt($process, CURLOPT_HEADER, false);
curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);
curl_setopt($process, CURLOPT_ENCODING, $this->compression);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
if ($this->proxy)
curl_setopt($process, CURLOPT_PROXY, $this->proxy);
curl_setopt($process, CURLOPT_POSTFIELDS, $this->stringify_array($data_as_array));
curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($process, CURLOPT_POST, true);
$return = curl_exec($process);
$err_status = curl_error($curl);
$http_status_code = curl_getinfo($process, CURLINFO_HTTP_CODE);
curl_close($process); if (strlen($err_status) > 0)
return $this->error("curl reported a error: $err_status"); $json = json_decode($return);
if($json == null){
$constants = get_defined_constants(true);
$json_errors = array();
foreach ($constants["json"] as $name => $value) {
if (!strncmp($name, "JSON_ERROR_", 11)) {
$json_errors[$value] = $name;
}
}
return $this->error("json_decode reported a error: " . $json_errors[json_last_error()]);
} if($http_status_code != 200)
return $this->error("request failed $http_status_code:$json->message");
return $json;
} private function stringify_array($array)
{
$encoded = array();
foreach($array as $key=>$value) {
$encoded[] = $key .'='. urlencode($value);
}
return join("&", $encoded);
}
private function error($error){
trigger_error($error, E_USER_ERROR);
return null;
}
}
?>
I know that there is a way to do this "before record added" with code like this:
require_once ("saresources/included_files/sa_api.php");
//Test att skicka special om tillgänglig är valt
if ($values["presence_cause"] == 'Tillgänglig') {
$data[extension_id] = $values["presence_extension"];
$data[mood] = 1;
}
else {
$data[extension_id] = $values["presence_extension"];
$data[status] = join(" ", array($values["presence_cause"], $values["presence_date"], $values["presence_endtime"]));
$data[mood] = $values["presence_mood"];
}
$api = new sa_api();
$json = $api->post('api/set_extension_status', $data);
return true;
document.location.reload(true);
But my problem is that i cant save to the db so i would like to make an new button that sends th evalues to my api (sa_api.php).
|