Call to CDRAPI and returns Error 400


#1

We’ve configured the device Grandstream 6202 and we’re have tested request API in PHP, JAVASCRIPT and PYTHON. The answer always is Error 400 BAD REQUEST.


If we put the url request in browser, the answer is 200 OK!

class Ucm6202 {
/*
* Host name or IP of PBX
/
private $host;
/

* API port. Default 8443
/
private $port;
/

* User name for access to API. Default ‘cdrapi’
/
private $user;
/

* User password for access to API. Default ‘cdrapi123’
/
private $pass;
/

* CURL handler
/
private $curl;
/
*
* Initial connection to PBX API
* @param string $host
* @param string $user
* @param string $pass
* @param string $port
/
public function __construct($host, $user = ‘cdrapi’, $pass = ‘cdrapi123’, $port = ‘8443’) {
$this->host = $host;
$this->port = $port;
$this->user = $user;
$this->pass = $pass;
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($this->curl, CURLOPT_USERPWD, $this->user . “:” . $this->pass);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($this->curl, CURLOPT_COOKIEJAR, ‘cookie.txt’);
curl_setopt($this->curl, CURLOPT_COOKIEFILE, “cookie.txt”);
curl_setopt($this->curl, CURLOPT_USERAGENT, ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0’);
curl_setopt($this->curl, CURLOPT_TIMEOUT, 50);
}
/
*
* Find records from CDR
* @param array $params
* @return array|false
*/
public function find($params = []) {
$url = ‘/cdrapi?format=json’;
$urlBuf = array_filter($params, function($item) {
if (is_null($item)) {
return false;
} else {
return true;
}
});
$resBuf = [];
foreach ($urlBuf as $key => $value) {
$resBuf[] = $key . ‘=’ . $value;
}
$urlParams = implode(’&’, $resBuf);
if (strlen($urlParams) > 0) {
$url .= ‘&’ . $urlParams;
}
$url = $this->host . ‘:’ . $this->port . $url;
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($this->curl);
if ($result) {
var_dump($result);
// $res = CJSON::decode($result);
// $cdrRoot = $res[‘cdr_root’];
// $cdrRoot2 = [];
// foreach ($cdrRoot as $item) {
// if (!isset($item[‘start’])) {
// if (isset($item[‘sub_cdr_3’])) {
// $item[‘sub_cdr_3’][‘calldate’] = $item[‘sub_cdr_3’][‘start’];
// $item[‘sub_cdr_3’][‘cdr_pkey’] = $item[‘sub_cdr_3’][‘AcctId’];
// $cdrRoot2[] = $item[‘sub_cdr_3’];
// } else {
// continue;
// }
// } else {
// $item[‘calldate’] = $item[‘start’];
// $item[‘cdr_pkey’] = $item[‘AcctId’];
// $cdrRoot2[] = $item;
// }
// }
// return $cdrRoot2;
} else {
return FALSE;
}
}
}

$url = “https://172.16.1.102:8443/cdrapi?format=JSON&caller=206&minDur=8&maxDur=60”;
include(‘Ucm6202.php’);
$PBX = new Ucm6202(‘https://172.16.1.102’, ‘ims’, ‘ims123’, ‘8443’);
$params = [
// ‘startTime’ => ‘2018-03-01T09:00:00’,
// ‘endTime’ => ‘2018-03-02T23:59:59’,
‘numRecords’ => 100,
‘format’ => ‘JSON’,
‘caller’ => 206,
‘minDur’ => 8,
‘maxDur’ => 60
];
$data = $PBX->find($params);


#2

I think most here work with layers 1 to 4 doubt you get much help here on this but then I could very wrong…