繁体   English   中英

无法通过 OsTicket api 创建票证(格式错误的 JSON)

[英]Can't create ticket via OsTicket api (Malformed JSON)

<?php
#$test = snmpget -v 1 -O v 192.168.0.100 -c public .1.3.6.1.4.1.6574.1.2.0;

$oksystemp = "40";

$cursystemp = snmpget("192.168.0.100", "public", "1.3.6.1.4.1.6574.1.2.0");

if ($cursystemp > $oksystemp) {

    $config = array(
        'url'=>'http://10.0.0.56/api/http.php/tickets.json',
        'key'=>'83952BBA4A8173FF265FCD9941019797'
        );

    # Fill in the data for the new ticket, this will likely come from $_POST.

    $data = array(
        'name'      =>      'John Doe',
        'email'     =>      'mailbox@host.com',
        'subject'   =>      'Test API message',
        'message'   =>      'Die Temperatur des Systems beträgt ".$cursystemp."',
        'attachments' => array(),
    );

    /* 
     * Add in attachments here if necessary
    $data['attachments'][] =
    array('filename.pdf' =>
            'data:image/png;base64,' .
                base64_encode(file_get_contents('/path/to/filename.pdf')));
    */

    #pre-checks
    function_exists('curl_version') or die('CURL support required');
    function_exists('json_encode') or die('JSON support required');

    #set timeout
    set_time_limit(30);

    #curl post
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $config['url']);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_USERAGENT, 'osTicket API Client v1.7');
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Expect:', 'X-API-Key: '.$config['key']));
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result=curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($code != 201)
            die('Unable to create ticket: '.$result);

    $ticket_id = (int) $result;

    # Continue onward here if necessary. $ticket_id has the ID number of the
    # newly-created ticket

} elseif ($cursystemp == $oksystemp) {

    $config = array(
        'url'=>'http://10.0.0.56/api/http.php/tickets.json',
        'key'=>'83952BBA4A8173FF265FCD9941019797'
        );

    # Fill in the data for the new ticket, this will likely come from $_POST.

    $data = array(
        'name'      =>      'John Doe',
        'email'     =>      'mailbox@host.com',
        'subject'   =>      'Test API message',
        'message'   =>      'Die Temperatur des Systems beträgt ".$cursystemp."',
        'attachments' => array(),
    );

    /* 
     * Add in attachments here if necessary
    $data['attachments'][] =
    array('filename.pdf' =>
            'data:image/png;base64,' .
                base64_encode(file_get_contents('/path/to/filename.pdf')));
    */

    #pre-checks
    function_exists('curl_version') or die('CURL support required');
    function_exists('json_encode') or die('JSON support required');

    #set timeout
    set_time_limit(30);

    #curl post
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $config['url']);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_USERAGENT, 'osTicket API Client v1.7');
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Expect:', 'X-API-Key: '.$config['key']));
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result=curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($code != 201)
            die('Unable to create ticket: '.$result);

    $ticket_id = (int) $result;

    # Continue onward here if necessary. $ticket_id has the ID number of the
    # newly-created ticket

} else {
    echo ($cursystemp);
}

?>

我得到了这个代码。 当我执行脚本时,我得到一个“语法错误,格式错误的 JSON”。 我不知道问题应该出在哪里。 我想创建一个脚本,它将作为服务器上的 crontab 工作,并每 5 分钟从 Synology nas 请求一次参数,当温度过高时,它应该自动在 osticket 中创建票证。

亲切的问候

<?php
#$test = snmpget -v 1 -O v 192.168.0.100 -c public .1.3.6.1.4.1.6574.1.2.0;

$oksystemp = "40";

$cursystemp = snmpget("192.168.0.100", "public", "1.3.6.1.4.1.6574.1.2.0");

function ticket() {
    # Configuration: Enter the url and key. That is it.
    #  url => URL to api/task/cron e.g #  http://yourdomain.com/support/api/tickets.json
    #  key => API's Key (see admin panel on how to generate a key)
    #

    $config = array(
                'url'=>'http://10.0.0.56/api/http.php/tickets.json',
                'key'=>'83952BBA4A8173FF265FCD9941019797'
                );

    # Fill in the data for the new ticket, this will likely come from $_POST.

    $data = array(
        'name'      =>      'John Doe',
        'email'     =>      'mailbox@host.com',
        'subject'   =>      'Test API message',
        'message'   =>      'This is a test of the osTicket API',
        'attachments' => array(),
    );

    /* 
     * Add in attachments here if necessary
    $data['attachments'][] =
    array('filename.pdf' =>
                'data:image/png;base64,' .
                base64_encode(file_get_contents('/path/to/filename.pdf')));
    */

    #pre-checks
    function_exists('curl_version') or die('CURL support required');
    function_exists('json_encode') or die('JSON support required');

    #set timeout
    set_time_limit(30);

    #curl post
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $config['url']);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_USERAGENT, 'osTicket API Client v1.7');
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Expect:', 'X-API-Key: '.$config['key']));
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result=curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($code != 201)
        die('Unable to create ticket: '.$result);

    $ticket_id = (int) $result;

    # Continue onward here if necessary. $ticket_id has the ID number of the
    # newly-created ticket
}


if ($cursystemp > $oksystemp) {

    ticket();

} elseif ($cursystemp == $oksystemp) {

    ticket();

} else {
    echo ($cursystemp);
}

?>

我只是稍微清理了我的代码,现在它可以工作了,如果有人遇到同样的问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM