-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFreescout.php
More file actions
63 lines (58 loc) · 1.59 KB
/
Freescout.php
File metadata and controls
63 lines (58 loc) · 1.59 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
namespace core;
class Freescout {
private ?array $config = null;
public function __construct(array $config)
{
$this->config = $config;
}
// https://api-docs.freescout.net/#create-conversation
public function create_conversation(string $subject, string $body, string $email, ?string $phone = null): bool {
$ch = curl_init();
if ($ch === false) {
user_error("curl_init fail");
}
$opt = 1;
$opt &= curl_setopt($ch, CURLOPT_URL, $this->config["base"] . "/api/conversations");
$opt &= curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$opt &= curl_setopt($ch, CURLOPT_TIMEOUT, 3);
$opt &= curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
$opt &= curl_setopt($ch, CURLOPT_HTTPHEADER, [
"X-FreeScout-API-Key: " . $this->config["apikey"],
"Content-Type: application/json",
"Accept: application/json",
]);
$opt &= curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
"type" => "email",
"mailboxId" => $this->config["mboxid"],
"subject" => $subject,
"customer" => [
"email" => $email,
"phone" => $phone,
"firstName" => "customer",
],
"threads" => [
[
"text" => $body,
"type" => "customer",
"customer" => [
"email" => $email,
]
]
],
]));
if ($opt !== 1) {
user_error("curl_setopt fail");
}
$res = curl_exec($ch);
if ($res === false) {
user_error("curl_exec fail e=" . curl_error($ch));
}
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($code !== 201) {
error_log("freescout(http=$code) body=" . $res);
}
return $code === 201;
}
}