Reputation: 1818
I am using an example script that was provided on a separate forum that would allow me to embed a ticket submission form into one of my webpages.
I downloaded the example script and replaced the relevant data. The problem is that when I run the script I receive an error. The error is saved in a file in the directory that the script is also located.
The error that I receive is:
[13-Jan-2015 17:07:37 Europe/London] PHP Parse error: syntax error, unexpected ';', expecting ')' in /home/sinergqx/public_html/pages/new_ticket.php on line 46
When I received the error, I assumed it was just a typo in the script that had been provided so I did exactly what the error message was describing and replaced the ;
with a )
. This still made no difference.
I am not too sure what the problem is. I am very limited php knowledge so it is hard for me to troubleshoot to much myself.
I searched the error on the internet but found no threads that explained what to do in this situation.
If it helps to see the thread that I was looking at before, then you can look at it by clicking here.
Finally, I have provided the code that I am using. This is the exact script that I have uploaded to my website. It is bellow:
#!/usr/bin/php -q
<?php
#
# 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)
# $data add custom required fields to the array.
#
# Originally authored by [email protected]
# Modified by ntozier@osTicket / tmib.net
// If 1, display things to debug.
$debug="0";
// You must configure the url and key in the array below.
$config = array(
'url'=>'http://support.sinergycraft.net/api/cron.php', // URL to site.tld/api/tickets.json
'key'=>'I WOULD PUT MY API HERE' // API Key goes here
);
# NOTE: some people have reported having to use "http://your.domain.tld/api/http.php/tickets.json" instead.
if($config['url'] === 'http://your.domain.tld/api/tickets.json') {
echo "<p style=\"color:red;\"><b>Error: No URL</b><br>You have not configured this script with your URL!</p>";
echo "Please edit this file ".__FILE__." and add your URL at line 18.</p>";
die();
}
if(IsNullOrEmptyString($config['key']) || ($config['key'] === 'PUTyourAPIkeyHERE')) {
echo "<p style=\"color:red;\"><b>Error: No API Key</b><br>You have not configured this script with an API Key!</p>";
echo "<p>Please log into osticket as an admin and navigate to: Admin panel -> Manage -> Api Keys then add a new API Key.<br>";
echo "Once you have your key edit this file ".__FILE__." and add the key at line 19.</p>";
die();
}
# Fill in the data for the new ticket, this will likely come from $_POST.
# NOTE: your variable names in osT are case sensiTive.
# So when adding custom lists or fields make sure you use the same case
# For examples on how to do that see Agency and Site below.
$data = array(
'name' => 'John Doe', // from name aka User/Client Name
'email' => '[email protected]', // from email aka User/Client Email
'phone' => '1234567890', // phone number aka User/Client Phone Number
'subject' => 'Test API message', // test subject, aka Issue Summary
'message' => 'This is a test of the osTicket API', // test ticket body, aka Issue Details.
'ip' => $_SERVER['REMOTE_ADDR'], // Should be IP address of the machine thats trying to open the ticket.
'topicId' => '1'; // the help Topic that you want to use for the ticket --L46
//'Agency' => '58', //this is an example of a custom list entry. This should be the number of the entry.
//'Site' => 'Bermuda'; // this is an example of a custom text field. You can push anything into here you want.
'attachments' => array()
);
# more fields are available and are documented at:
# https://github.com/osTicket/osTicket-1.8/blob/develop/setup/doc/api/tickets.md
if($debug=='1') {
print_r($data);
die();
}
# Add in attachments here if necessary
# Note: there is something with this wrong with the file attachment here it does not work.
$data['attachments'][] =
array('file.txt' =>
'data:text/plain;base64;'
.base64_encode(file_get_contents('/file.txt'))); // replace ./file.txt with /path/to/your/test/filename.txt
#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.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('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
function IsNullOrEmptyString($question){
return (!isset($question) || trim($question)==='');
}
?>
The only thing that isn't as it would be if I uploaded it is that I have deleted the api key and replaced it with I WOULD PUT MY API HERE
.
Also, although in the comment section of the code it says to link to http://your.domain.tld/api/http.php/tickets.json
, this was not in any of my directories so instead I linked it to http://your.domain.tld/api/http.php/cron.php
. I did this because it said in the commented section at the top it said url => URL to api/task/cron e.g # http://yourdomain.com/support/api/tickets.json
.
Does anybody know what I have done wrong as I have done what it said in the error message and I haven't had any luck.
Thanks for anyone's help, it is very much appreciated as I am a real beginner at php.
EDIT:
I did look at line 46 and see the ;
and I replaced it with a )
which the error message suggested to me was what was wrong but it did not fix the problem.
Upvotes: 0
Views: 324
Reputation: 59701
Change this line:
'topicId' => '1';
to this:
'topicId' => '1',
//^ You have to use a comma to separate each array element
Also for more information about an array see the manual: http://php.net/manual/en/language.types.array.php
And a quote from there:
An array can be created using the array() language construct. It takes any number of comma-separated key => value pairs as arguments.
Also as a good reference which error means what see this: Reference - What does this error mean in PHP?
Upvotes: 3
Reputation: 98
Just reading your code I found that have a ';' in line 46.
Looking to line 46 I was a ';' where need be a ','. How can I know it? Because it is an array, look to another end-lines of variables in array and you will see that from one value to another you need a ',' to separate them.
So here is your corrected code:
#!/usr/bin/php -q
<?php
#
# 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)
# $data add custom required fields to the array.
#
# Originally authored by [email protected]
# Modified by ntozier@osTicket / tmib.net
// If 1, display things to debug.
$debug="0";
// You must configure the url and key in the array below.
$config = array(
'url'=>'http://support.sinergycraft.net/api/cron.php', // URL to site.tld/api/tickets.json
'key'=>'I WOULD PUT MY API HERE' // API Key goes here
);
# NOTE: some people have reported having to use "http://your.domain.tld/api/http.php/tickets.json" instead.
if($config['url'] === 'http://your.domain.tld/api/tickets.json') {
echo "<p style=\"color:red;\"><b>Error: No URL</b><br>You have not configured this script with your URL!</p>";
echo "Please edit this file ".__FILE__." and add your URL at line 18.</p>";
die();
}
if(IsNullOrEmptyString($config['key']) || ($config['key'] === 'PUTyourAPIkeyHERE')) {
echo "<p style=\"color:red;\"><b>Error: No API Key</b><br>You have not configured this script with an API Key!</p>";
echo "<p>Please log into osticket as an admin and navigate to: Admin panel -> Manage -> Api Keys then add a new API Key.<br>";
echo "Once you have your key edit this file ".__FILE__." and add the key at line 19.</p>";
die();
}
# Fill in the data for the new ticket, this will likely come from $_POST.
# NOTE: your variable names in osT are case sensiTive.
# So when adding custom lists or fields make sure you use the same case
# For examples on how to do that see Agency and Site below.
$data = array(
'name' => 'John Doe', // from name aka User/Client Name
'email' => '[email protected]', // from email aka User/Client Email
'phone' => '1234567890', // phone number aka User/Client Phone Number
'subject' => 'Test API message', // test subject, aka Issue Summary
'message' => 'This is a test of the osTicket API', // test ticket body, aka Issue Details.
'ip' => $_SERVER['REMOTE_ADDR'], // Should be IP address of the machine thats trying to open the ticket.
'topicId' => '1', // the help Topic that you want to use for the ticket
//'Agency' => '58', //this is an example of a custom list entry. This should be the number of the entry.
//'Site' => 'Bermuda'; // this is an example of a custom text field. You can push anything into here you want.
'attachments' => array()
);
# more fields are available and are documented at:
# https://github.com/osTicket/osTicket-1.8/blob/develop/setup/doc/api/tickets.md
if($debug=='1') {
print_r($data);
die();
}
# Add in attachments here if necessary
# Note: there is something with this wrong with the file attachment here it does not work.
$data['attachments'][] =
array('file.txt' =>
'data:text/plain;base64;'
.base64_encode(file_get_contents('/file.txt'))); // replace ./file.txt with /path/to/your/test/filename.txt
#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.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('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
function IsNullOrEmptyString($question){
return (!isset($question) || trim($question)==='');
}
?>
Upvotes: 1
Reputation: 1808
'topicId' => '1'; // the help Topic that you want to use for the ticket
It's in the middle of an array definition, so it should be a ,
, not a ;
or )
.
Upvotes: 2