Page 1 of 1

Trouble submitting Tickets via API (and SOLUTION)

Posted: Mon Sep 18, 2017 6:21 pm
by TechCoder
Script URL: rather not leave this on a public forum at this time (initial setup/testing... :)
Mods for HESK version: 3.1.1
HESK version: 2.7.3
Hosting company: N/A
URL of phpinfo.php: provided only if needed
URL of session_test.php: provided if needed
What terms did you try when SEARCHING for a solution: API

Write your message below:

Following are the errors seen in trying to make a ticket and what I did to get the API working to consistently create new tickets. I know this is still beta so I hope this helps in moving it a step forward.

Several areas needed changing in order to get API ticket creation working, but the final result is that it now does (and we can begin integrating this into our apps, etc.)!

body of call is very similar to the API sample (- suggestedArticles and screenResolution)
{"name":"My Name","email":"useyourrealeamail@example.com","category":"1","priority":1,"subject":"Billing Inquiry","message":"Here are some comments","html":false,"location":["-0.287","-73.513"],"userAgent":"Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/60.0.3112.113 Safari\/537.36","language":"English"}
X-AUTH-TOKEN: token
NOTE: though the API does say "This endpoint DOES NOT require authentication", the header and token IS needed, but the token does not have to be valid (I put in "123456" and the tickets are generated just as well as a token freshly generated in MFH)

INITIAL API PROBLEM (first API call):
ERROR:
/api/BusinessLogic/Categories/CategoryRetriever.php:29 Trying to get property of non-object
Troubleshooting this, I found that $userContext is empty, so the code

Code: Select all

$category->accessible = $userContext->admin ...
produces the error (non-object)

I changed it to

Code: Select all

 if (!empty($userContext)) {
                $category->accessible = $userContext->admin ||
                    in_array($category->id, $userContext->categories);
            }
which allowed me to move past this error (though this also bypasses the category->accessible check - OK for now, as we are being careful on what we send, though I'm sure there is a better way to do this!)

SECOND ATTEMPT (after bypassing the non-object error)
Received the error
/api/BusinessLogic/Emails/EmailTemplateParser.php:182 implode(): Invalid arguments passed
This is due to $ticket->email actually being a string, not an array, so the code

Code: Select all

rawurlencode(implode(';', $ticket->email))
fails.

This code may be used in other fashions, though the API documentation clearly states that email should be a string, so I fixed the code to be

Code: Select all

$emails = is_array($ticket->email)? implode(";", $ticket->email) : $ticket->email;
        $heskSettings['e_param'] = $heskSettings['email_view_ticket'] ? '&e=' . rawurlencode( $emails) : '';
Which further requires (down the page in the same file)

Code: Select all

$msg = str_replace('%%EMAIL%%', implode(';',$ticket->email), $msg);
to be

Code: Select all

$msg = str_replace('%%EMAIL%%', $emails, $msg);
.......and, VIOLA! Ticket generation via the MFH API!

Re: Trouble submitting Tickets via API (and SOLUTION)

Posted: Thu Oct 19, 2017 7:06 pm
by friedrich
you can share the complete example of the api, url included, for some reason it does not work for me

greeting

Re: Trouble submitting Tickets via API (and SOLUTION)

Posted: Mon Oct 30, 2017 10:02 am
by TechCoder
If you are having trouble, you need to put some of your testing and showing what you have done (and what works/not) in details as I have..... - someone might be able to help you.