使用osticket api_create_ticket.php创建故障单时出错

时间:2016-05-17 10:36:17

标签: php json curl osticket

我正在尝试使用osticket api_create_ticket.php创建票证,但我收到响应代码200.一切都与许多文档和示例中描述的相同。我用过服务器ip for api key也试过我的系统ip。授予该文件夹的写权限。但它仍然没有用。

#!/usr/bin/php -q
<?php


$config = array(
    'url'=>'http://myweb.in/project1/support/api/tickets.json',
    'key'=>'3B2BADDBF72D30DBEBD6378A1DF2E6FB'
    );


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

   function_exists('curl_version') or die('CURL support required');
   function_exists('json_encode') or die('JSON support required');

   set_time_limit(30);

   $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);

   print_r($code);

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

   $ticket_id = (int) $result;

   ?>

1 个答案:

答案 0 :(得分:0)

试试这段代码我从这里得到https://github.com/osTicket/osTicket-1.7/blob/develop/setup/scripts/rcron.php

<?php


$config = array(
    'url'=>'http://myweb.in/project1/support/api/tickets.json',
    'key'=>'3B2BADDBF72D30DBEBD6378A1DF2E6FB'
    );
#check if curl is enabled
function_exists('curl_version') or die('CURL support required');

#set execution time. Make it 0 if there is no time limit
set_time_limit(60);

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

   function_exists('curl_version') or die('CURL NOT SUPORTED');

   set_time_limit(30);

   $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, TRUE);
   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);

   if(preg_match('/HTTP\/.* ([0-9]+) .*/', $result, $status) && $status[1] == 200)
exit(0);
   $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

   curl_close($ch);

   print_r($code);

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

   $ticket_id = (int) $result;

   ?>