Happyfox is a ticketing service provider. Recently I’m dealing with it’s API and took me quite some time to figure out how to create a ticket with attachment. You can see the official documentation here.

1. Create an account and Generate API key

You can sign up an account

Now you can create api key

Create API key step 1

Create API key step 2

Create API key step 3

Enter Identification Name and save it

Create API key step 4

2. Get your username & password

As the requirement mention that HTTP Basic Authentication is required, thus you need to have a username & password

Get credential

Note: you must move your mouse over only you can see the link see auth code

See auth code

The API key served as username and Auth code served as password.

3. Make a POST request thru PHP cURL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
// get your data ready
$data = array(
'subject' => 'How to change the DNS server',
'text' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor ...',
'category' => 1, // set your category (if you not sure about that, just put 1)
'email' => 'john.smith@example.com', // web user's email
'name' => 'John Smith', // web user's name

// Optional. Note that there is an '@' infront
'attachments' => '@/home/user/Desktop/screenshot.png', // attachment's path
);

$ch = curl_init('https://<youraccount>.happyfox.com/api/1.1/json/tickets/');

curl_setopt($ch, CURLOPT_POST, true); // this is POST request

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, sprintf('%s:%s', <Happyfox api key>, <Happyfox auth code>)); // here is the HTTP basic auth
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

// don't forget to set your form data
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$response = curl_exec($ch);
$status = curl_getinfo($ch);
curl_close($ch);

if ($status['http_code] == 200) {
$result = json_decode($response, true);
print_r($result);
} else {
echo 'Error occurred';
}

References: