Hello.
I'm trying to create a new ticket via API but it always respond me 'Valid API Key required'.
I have setted corretly the API Key, and take like sample the piping email code.
This is my code:
<?php
# Configuration: Enter the url and key. That is it.
# url => URL to api/tickets.email e.g http://yourdomain.com/support/api/tickets.email
# key => API's Key (see admin panel on how to generate a key)
#
$config = array(
'url'=>'http://localhost/osticket/api/tickets.xml',
'key'=>'5D42A08394AE6CB5336D2EE5DB42E3DB'
);
#pre-checks
function_exists('file_get_contents') or die('upgrade php >=4.3');
function_exists('curl_version') or die('CURL support required');
#read stdin (piped email)
$data='<?xml version="1.0" encoding="UTF-8"?>
<ticket alert="true" autorespond="true" source="API">
<name>Angry User</name>
<email>api@osticket.com</email>
<subject>Testing API</subject>
<phone ext="123">318-555-8634</phone>
<message><!<!<C><s>```</s>[CDATA>></message>
<attachments>
<file name="file.txt" type="text/plain"><!<![CDATA<e>```</e></C>>></file>
<file name="image.gif" type="image/gif" encoding="base64">
R0lGODdhMAAwAPAAAAAAAP///ywAAAAAMAAwAAAC8IyPqcvt3wCcDkiLc7C0qwy
GHhSWpjQu5yqmCYsapyuvUUlvONmOZtfzgFzByTB10QgxOR0TqBQejhRNzOfkVJ
+5YiUqrXF5Y5lKh/DeuNcP5yLWGsEbtLiOSpa/TPg7JpJHxyendzWTBfX0cxOnK
PjgBzi4diinWGdkF8kjdfnycQZXZeYGejmJlZeGl9i2icVqaNVailT6F5iJ90m6
mvuTS4OK05M0vDk0Q4XUtwvKOzrcd3iq9uisF81M1OIcR7lEewwcLp7tuNNkM3u
Nna3F2JQFo97Vriy/Xl4/f1cf5VWzXyym7PHhhx4dbgYKAAA7
</file>
</attachments>
<ip>192.168.0.173</ip>
</ticket>';
#set timeout
set_time_limit(10);
#curl post
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $config);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $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));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result=curl_exec($ch);
curl_close($ch);
//Use postfix exit codes...expected by MTA.
$code = 75;
if(preg_match('/HTTP\/.* (+) .*/', $result, $status)) {
echo $result;
echo '<br/><br/>';
echo print_r($status);
switch($status) {
case 201: //Success
$code = 0;
break;
case 400:
$code = 66;
break;
case 401: /* permission denied */
case 403:
$code = 77;
break;
case 415:
case 416:
case 417:
case 501:
$code = 65;
break;
case 503:
$code = 69;
break;
case 500: //Server error.
default: //Temp (unknown) failure - retry
$code = 75;
}
}
exit($code);
?>
And this is the result:
HTTP/1.1 401 Unauthorized Date: Tue, 16 Apr 2013 08 GMT Server: Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7 X-Powered-By: PHP/5.4.7 Set-Cookie: PHPSESSID=6sv6khgcs4sf2g79slielqccf0; path=/ Expires: Thu, 19 Nov 1981 08 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Status: 401 Unauthorized Connection: Close Content-Length: 22 Content-Type: text/html; charset=UTF-8 Valid API key required
Array ( => HTTP/1.1 401 Unauthorized => 401 ) 1
This is the configuration of the API key:
API Key is auto-generated. Delete and re-add to change the key.
IP Address: 192.168.0.173
API Key: 5D42A08394AE6CB5336D2EE5DB42E3DB
The IP is the IP from which the request is made, Is it correct?
What can I do?
Thanks