simranjeet Posted August 15 Share Posted August 15 I have a question regarding setting up a webhook in WHMCS for ticket creation. Could you please guide me on how to configure a webhook in WHMCS to trigger when a ticket is opened? If possible, could you also share some screenshots to illustrate the setup process? 0 Quote Link to comment Share on other sites More sharing options...
pRieStaKos Posted August 18 Share Posted August 18 First read this to learn how to create a hook and Then check TicketOpen 0 Quote Link to comment Share on other sites More sharing options...
simranjeet Posted August 20 Author Share Posted August 20 I've already looked into this, but I want to confirm something. Is there an option in WHMCS to directly set up a webhook URL without using a hook? Specifically, is there a setting where I can configure the webhook URL for the "open ticket" event? Thanks 0 Quote Link to comment Share on other sites More sharing options...
Solution simranjeet Posted August 20 Author Solution Share Posted August 20 I’ve created the file as per the instructions in the document you mentioned and added the hook for the open ticket event. However, when I trigger the hook, it's not working, and I’m not receiving any response at the webhook URL I set up. When I try to access this file directly through the browser, I get the error: "This file cannot be accessed directly." What does this error mean, and how can I resolve it? I’m using this script—could you please review the screenshot and let me know what I might be doing wrong? 0 Quote Link to comment Share on other sites More sharing options...
pRieStaKos Posted August 21 Share Posted August 21 This hook will trigger when a ticket opens. You cannot access it directly. This is not how hooks work in WHMCS. You can dump or store the response to a file for debugging. 0 Quote Link to comment Share on other sites More sharing options...
DennisHermannsen Posted August 21 Share Posted August 21 The point of hooks is to hook onto an event - ticket creation for example. If you want to access the file directly, remove lines 3-5 - but accessing the file won't help you since it needs to have data passed to it. Glancing over your code, it should work. Data is being sent to the get_hook_data.php script. 0 Quote Link to comment Share on other sites More sharing options...
simranjeet Posted August 22 Author Share Posted August 22 Yes, I understand that direct access isn’t possible. The hook should trigger when a ticket is opened. However, when I created an open ticket in my WHMCS system, the hook didn’t trigger, and I didn’t receive any response at my webhook URL. Here is the code in my webhook URL: <?php $data = file_get_contents('php://input'); file_put_contents('open.json', $data); ?> The hook should automatically trigger when a ticket is created, but it hasn’t worked, and no response is recorded in my webhook file. I’ve also tried removing a few lines of code as suggested, but it’s still not working. Additionally, I added this line for debugging: file_put_contents('hook_debug.log', print_r($vars, true), FILE_APPEND); but the hook_debug.log file isn’t being created either. 0 Quote Link to comment Share on other sites More sharing options...
DennisHermannsen Posted August 22 Share Posted August 22 (edited) An easy way to see if it's working is doing something like: logActivity("Test hook: $subject") right after defining the $subject variable. After triggering the hook, look at the activity log in WHMCS. As long as the .php file is placed inside /includes/hooks/ and has valid syntax, it should work just fine. Edited August 22 by DennisHermannsen 0 Quote Link to comment Share on other sites More sharing options...
pRieStaKos Posted August 22 Share Posted August 22 (edited) 13 minutes ago, simranjeet said: However, when I created an open ticket in my WHMCS system, the hook didn’t trigger, and I didn’t receive any response at my webhook URL. Here is the code in my webhook URL: Do you create the ticket through Client Area or Admin Area ? Edited August 22 by pRieStaKos 0 Quote Link to comment Share on other sites More sharing options...
DennisHermannsen Posted August 22 Share Posted August 22 Valid question! The TicketOpen hook only triggers when a client opens the ticket. If you're opening the ticket from the Admin Area, you need to use TicketOpenAdmin. This is also described on their documentation: https://developers.whmcs.com/hooks-reference/ticket/ 0 Quote Link to comment Share on other sites More sharing options...
simranjeet Posted August 22 Author Share Posted August 22 I have created both way Client Area and Admin Area but still its not working. 0 Quote Link to comment Share on other sites More sharing options...
simranjeet Posted August 22 Author Share Posted August 22 Additionally, when I run my hook file in the browser, I encounter the following error:->[22-Aug-2024 12:24:05 UTC] PHP Fatal error: Uncaught Error: Call to undefined function add_hook() in /home/mydomain/public_html/clients/includes/hooks/open_ticket.php:3 Stack trace: #0 {main} thrown in /home/nextsulting/public_html/clients/includes/hooks/open_ticket.php on line 3 0 Quote Link to comment Share on other sites More sharing options...
pRieStaKos Posted August 22 Share Posted August 22 (edited) Just to make this clear again: Don't try to directly access a WHMCS hook. It's not how it suppose to work. Now, I'm attaching a hook and get_hook_data that I can confirm work: <?php add_hook('TicketOpen', 1, function ($vars) { try { // Send ticket data to webhook $webhook_url = ".../get_hook_data.php"; // Set post data $data = [ "ticketid" => $vars["ticketid"], "ticketmask" => $vars["ticketmask"], "userid" => $vars["userid"], "deptid" => $vars["deptid"], "deptname" => $vars["deptname"], "subject" => $vars["subject"], "message" => $vars["message"], "priority" => $vars["priority"], ]; $ch = curl_init($webhook_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json' ]); $response = curl_exec($ch); $data = json_decode($response, true); if ($response) { logActivity('[TicketOpen] Response: ' . $response); } curl_close($ch); } catch (Exception $e) { // If error, write to Activity log logActivity('[TicketOpen] Error: ' . $e->getMessage()); } }); <?php // Receive data from curl $data = json_decode(file_get_contents('php://input'), true); // Check if data is empty if (empty($data)) { // Return an error response $response = [ 'status' => 'error', 'message' => 'No data received', ]; header('Content-Type: application/json'); echo json_encode($response); } // Process the received data $vars = file_get_contents('php://input'); file_put_contents('vars.json', $vars); // Send a success response $response = [ 'status' => 'success', 'message' => 'Data received successfully', ]; header('Content-Type: application/json'); echo json_encode($response); Adjust get_hook_data to your needs.....You must get the point. Edited August 22 by pRieStaKos 0 Quote Link to comment Share on other sites More sharing options...
DennisHermannsen Posted August 22 Share Posted August 22 (edited) 1 hour ago, pRieStaKos said: Just to make this clear again: Don't try to directly access a WHMCS hook. It's not how it suppose to work. Accessing the file directly shouldn't cause a fatal error. By the looks of it, it could seem like the hook file is somehow not registered in WHMCS. If it was, it would know of the add_hook() function. Nevermind, of course it should. WHMCS isn't booted up when accessing the file directly. By the looks of it, you haven't configured WHMCS correctly. When I try to access your WHMCS installation, I'm told there was a problem establishing a database connection. Edited August 22 by DennisHermannsen 0 Quote Link to comment Share on other sites More sharing options...
simranjeet Posted August 26 Author Share Posted August 26 I'm still facing the same issue. It seems to be related to my server. Whenever I create a file on my server, it only works the first time I run it, but then it stops working. Do you have any idea why this might be happening? For example, if I create a new file with just echo "hello"; and run it in the browser, it displays "hello" as expected. But if I change the code to something like echo "text"; and run the file again, it still displays the original "hello" message instead of the updated "text". 0 Quote Link to comment Share on other sites More sharing options...
DennisHermannsen Posted August 26 Share Posted August 26 That sounds like a cache issue. Have you tried contacting your web host or sysadmin regarding that issue? 0 Quote Link to comment Share on other sites More sharing options...
simranjeet Posted August 26 Author Share Posted August 26 I don't think it's a cache issue because I've already cleared the cache from my browser. The problem is that the file only runs once, and if I try to run it again, it doesn't work. However, if I rename the file and run it, it starts working again 0 Quote Link to comment Share on other sites More sharing options...
simranjeet Posted August 26 Author Share Posted August 26 I’m currently in touch with my web host about this issue. Once it's resolved, I'll test the hook again. If the problem persists, I'll update you here. Thanks 0 Quote Link to comment Share on other sites More sharing options...
DennisHermannsen Posted August 26 Share Posted August 26 Browser cache is not the only cache. There's also server cache. If you change the contents of a file and the change is not shown when you access the file in your browser (after refreshing the page using Ctrl+F5 to clear the browser cache), the issue is without a doubt some sort of server cache. 0 Quote Link to comment Share on other sites More sharing options...
simranjeet Posted August 27 Author Share Posted August 27 Hi, The cache issue has been resolved, and I can now access a file multiple times without any problems. I’ve created a file inside the [includes/hooks] directory to capture the data of a ticket created in WHMCS. However, despite using the same code, I’m not receiving any data for the open ticket. I’ve tried creating a ticket using the API as well as manually in WHMCS, but I still don’t get any data. I'll share some screenshots with you so you can see exactly what I'm trying to accomplish. If you think I'm making a mistake, please let me know and guide me on how to proceed. 0 Quote Link to comment Share on other sites More sharing options...
DennisHermannsen Posted August 27 Share Posted August 27 Do you see anything in the Activity Log? Try to simplify your hook down to just logging something. 0 Quote Link to comment Share on other sites More sharing options...
simranjeet Posted August 27 Author Share Posted August 27 No i didn't found anything in whmcs activity log related to hook. 0 Quote Link to comment Share on other sites More sharing options...
simranjeet Posted August 27 Author Share Posted August 27 I have enable the Hooks Debug Mode and I am getting this error-> 0 Quote Link to comment Share on other sites More sharing options...
pRieStaKos Posted August 27 Share Posted August 27 TicketOpen and TicketOpenAdmin is not triggered for tickets created by API. There is no hook index for this Please read the hook documentation and set the proper hook index for the action you are making. Try opening a ticket through client and admin area and check if hook is working. 0 Quote Link to comment Share on other sites More sharing options...
simranjeet Posted August 27 Author Share Posted August 27 I have already attempted to create tickets through both the client and admin interfaces, but it’s not working. Additionally, creating a ticket via the API was only for testing purposes. I placed my file in the directory specified in the documentation: includes/hooks/open_ticket.php. I have provided the code from this file earlier, but I’m still encountering the same issue. I’m unsure what might be causing the problem or what steps I need to take to resolve it, as I haven't received any errors to help diagnose the issue. 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.