Search code examples
phphtmlwhmcs

WHMCS API - Trying to post data from a html form


I understand PHP pretty well however I have never had to deal with curl before today so I'm having a bit of an issue understanding how I am supposed to submit data to WHMCS API

I have made a simple HTML form on my website but I'm trying to make the following code obtains the variables such as subject and message I have tried many different ways and I keep getting error 500 and I can't find a guide on the WHMCS forum there seems to be a few which might work however these topics have been removed as there old I'm guessing

The following code is what WHMCS gives you to work with all I need is some help to understand how I format the variables coming from my form

<?php 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, '####'); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
            'action' => 'OpenTicket',
            'username' => '#',
            'password' => '#',
            'accesskey' => '#',
            'deptid' => '1',
            'subject' => 'This is a sample ticket',            
            'message' => 'Demo Text',
            'email' => '[email protected]',
            'name' => 'Demo User',
            'priority' => 'Medium',
            'markdown' => true,
            'responsetype' => 'json',
        ) ) ); 
$response = curl_exec($ch); 
curl_close($ch);
?>`

Solution

  • You need to read the posted variables from the $_POST array, e.g. $_POST['example'] will contain the value of example input element of the submitted form.

    For the form (as an example, we will get subject and email as inputs):

    <form action="" method="post">
    Subject: <input type="text" name="subject" value="" /><br />
    Email: <input type="email" name="email" value="" /><br />
    <input type="submit" name="btnAct" value="Submit" />
    </form>
    

    For the API:

    <?php
    if (isset($_POST['btnAct'])) {
        //ToDo: sanitize inputs, use filter_var() for example
        $subject = $_POST['subject'];
        $email = $_POST['email'];
        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_URL, '####'); 
        curl_setopt($ch, CURLOPT_POST, 1); 
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
                    'action' => 'OpenTicket',
                    'username' => '#',
                    'password' => '#',
                    'accesskey' => '#',
                    'deptid' => '1',
                    'subject' => $subject,            
                    'message' => 'Demo Text',
                    'email' => $email,
                    'name' => 'Demo User',
                    'priority' => 'Medium',
                    'markdown' => true,
                    'responsetype' => 'json',
                ) ) ); 
        $response = curl_exec($ch); 
        curl_close($ch);
    
    }