【问题标题】:error in creating ticket by using osticket api_create_ticket.php使用 osticket api_create_ticket.php 创建票证时出错
【发布时间】:2016-05-17 10:36:17
【问题描述】:

我正在尝试使用 osticket api_create_ticket.php 创建票证,但我收到响应代码 200。一切都与许多文档和示例中的描述相同。我已将服务器 ip 用于 api 密钥也尝试了我的系统 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;

   ?>

【问题讨论】:

  • 响应码200表示请求成功。
  • 我需要响应码 201
  • 我可以在我的服务器上试试。你从哪里得到的 api?

标签: php json curl osticket


【解决方案1】:

试试这个我从这里得到的代码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;

   ?>

【讨论】:

  • 我的代码没有这一行,if(preg_match('/HTTP\/.* ([0-9]+) .*/', $result, $status) && $status[ 1] == 200) 退出(0);你认为它会带来什么改变吗?
  • 仔细检查这一行 curl_setopt($ch, CURLOPT_HEADER, TRUE);
  • 你能做一个 $result 的 var_dump 吗?在此行下方 $result=curl_exec($ch);添加 var_dump($result);退出;
  • 它返回字符串(1)“”。
  • 在我提供的网址中,我看到网址为 yourdomain.com/support/api/tasks/cron 但您的网址看起来像 myweb.in/project1/support/api/tickets.json 。我对 OsTicket 没有太多想法,但如果你看一下 URL,它会更好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多