Hi,

I want to generate a ticket using the api from an external system in php, but it throws error 200 since I am using the key according to the ip. What can be wrong?

My PHP:

`<?php

$config = array(
'url'=>'mesaayudaimg.mideplan.cl/api/tickets.json', // URL to site.tld/api/tickets.json
'key'=>'04EE8066743552714317C389943CC6DD' // API Key goes here
);

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

#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.8');
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('code: '.$code.' Unable to create ticket: '.$result);

$ticket_id = (int) $result;

if(isset($ticket_id) && $ticket_id!='')
{
echo "Ticket Created Sucessfully";
}else{
echo "Ticket not created. Try again later.";

}

?>`

My JSON File in "mesaayudaimg.mideplan.cl/api/tickets.json":

{
"alert": true,
"autorespond": true,
"source": "API",
"name": "Angry User",
"email": "api@osticket.com",
"subject": "Testing API",
"message": "",
"ip": "192.168.0.18"
}

And API created in administrator:

And Result:

My guess is that the IP address is incorrect. (Since it is ::1 and not an IP address) so it is preventing the ticket from being created.

Thanks, I tried putting my ip and the corresponding key. But the same problem happens. And the JSON file is not modified.


Result:

Have you tried looking at the osTicket Admin panel -> Dashboard -> System Logs?

You might need to set Admin panel -> Settings -> System Default Log level to DEBUG.

I set log level to DEBUG and i tried again, but no errors are shown.

Not sure then. Do you have required fields that you are not populating?

Thank you very much for the support ntozier, finally the problem was solved with HTTP Responses functionality using framework laravel 7 instead native php.

Write a Reply...