Added listmonk API call for published article
This commit is contained in:
parent
52065707df
commit
494f3c69a6
3 changed files with 351 additions and 76 deletions
141
lib/listmonk.php
Normal file
141
lib/listmonk.php
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
<?php
|
||||
|
||||
// $title - title
|
||||
// $articleUrl - permalink
|
||||
// $excerpt - teaser
|
||||
// $articleId - articleId
|
||||
// $newsletterTitle - randomly chosen from header_titles.php
|
||||
|
||||
function listmonkApiRequest(string $base, string $user, string $pass, string $method, string $path, ?array $payload = null): array {
|
||||
$json = $payload !== null ? json_encode($payload) : null;
|
||||
$url = rtrim($base, '/') . $path;
|
||||
|
||||
if (function_exists('curl_init')) {
|
||||
$ch = curl_init($url);
|
||||
$headers = ['Content-Type: application/json'];
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_USERPWD => $user . ':' . $pass,
|
||||
CURLOPT_HTTPHEADER => $headers,
|
||||
CURLOPT_CUSTOMREQUEST => $method,
|
||||
]);
|
||||
if ($json !== null) {
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
|
||||
}
|
||||
|
||||
$response = curl_exec($ch);
|
||||
if ($response === false) {
|
||||
$error = curl_error($ch);
|
||||
curl_close($ch);
|
||||
throw new Exception('Listmonk API request failed: ' . $error);
|
||||
}
|
||||
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
} else {
|
||||
$context = stream_context_create([
|
||||
'http' => [
|
||||
'method' => $method,
|
||||
'header' => "Content-Type: application/json\r\nAuthorization: Basic " . base64_encode($user . ':' . $pass) . "\r\n",
|
||||
'content' => $json ?? '',
|
||||
'timeout' => 10,
|
||||
'ignore_errors' => true,
|
||||
],
|
||||
]);
|
||||
$response = @file_get_contents($url, false, $context);
|
||||
$code = 0;
|
||||
if (isset($http_response_header[0]) && preg_match('/\s(\d{3})\s/', $http_response_header[0], $m)) {
|
||||
$code = (int)$m[1];
|
||||
}
|
||||
if ($response === false) {
|
||||
throw new Exception('Listmonk API request failed.');
|
||||
}
|
||||
}
|
||||
|
||||
if ($code < 200 || $code >= 300) {
|
||||
throw new Exception('Listmonk API request failed. HTTP ' . $code . ': ' . $response);
|
||||
}
|
||||
|
||||
$decoded = json_decode((string)$response, true);
|
||||
return is_array($decoded) ? $decoded : [];
|
||||
}
|
||||
|
||||
function listmonkCreateCampaign(
|
||||
string $articleId,
|
||||
string $articleUrl,
|
||||
string $title,
|
||||
string $excerpt,
|
||||
string $newsletterTitle = 'Mistakes, Views & Luck',
|
||||
bool $sendImmediately = false
|
||||
): array {
|
||||
$base = 'http://127.0.0.1:9000';
|
||||
|
||||
$user = 'mvlog';
|
||||
$pass = '1VLUsqSyIQ4xboEd2tmQvgCjWJJk0F3Q';
|
||||
|
||||
$listId = 3; // MVLog Readers
|
||||
$templateId = 5; // Your Listmonk template ID
|
||||
|
||||
$ogImageUrl = 'https://bubulescu.org/out-dir/' . rawurlencode($articleId) . '_og.jpg';
|
||||
|
||||
$safeTitle = htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
|
||||
$safeExcerpt = nl2br(htmlspecialchars($excerpt, ENT_QUOTES, 'UTF-8'));
|
||||
$safeArticleUrl = htmlspecialchars($articleUrl, ENT_QUOTES, 'UTF-8');
|
||||
$safeOgImageUrl = htmlspecialchars($ogImageUrl, ENT_QUOTES, 'UTF-8');
|
||||
|
||||
$body = '
|
||||
<a href="' . $safeArticleUrl . '">
|
||||
<img
|
||||
src="' . $safeOgImageUrl . '"
|
||||
alt="' . $safeTitle . '"
|
||||
style="display:block;width:100%;max-width:632px;height:auto;border-radius:18px;border:0;margin:0 auto 24px auto;"
|
||||
>
|
||||
</a>
|
||||
|
||||
<div style="padding:0 24px 8px 24px;text-align:center;">
|
||||
<h2 style="margin:0 0 12px 0;color:#ffffff;font-size:26px;line-height:1.25;">
|
||||
' . $safeTitle . '
|
||||
</h2>
|
||||
|
||||
<p style="margin:0;color:#eeeeee;font-size:16px;line-height:1.6;">
|
||||
' . $safeExcerpt . '
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<p style="text-align:center;margin-top:28px;">
|
||||
<a href="' . $safeArticleUrl . '"
|
||||
style="display:inline-block;background:#d9822b;color:#111;text-decoration:none;font-weight:bold;padding:13px 22px;border-radius:999px;">
|
||||
Read the story
|
||||
</a>
|
||||
</p>
|
||||
';
|
||||
|
||||
$payload = [
|
||||
'name' => $newsletterTitle,
|
||||
'subject' => 'Story published: ' . $title,
|
||||
'lists' => [$listId],
|
||||
'from_email' => 'MVLog <newsletter@bubulescu.org>',
|
||||
'type' => 'regular',
|
||||
'content_type' => 'html',
|
||||
'body' => $body,
|
||||
'template_id' => $templateId,
|
||||
'messenger' => 'email',
|
||||
'status' => 'draft',
|
||||
'headers' => [
|
||||
['Reply-To' => 'mvlog@bubulescu.org']
|
||||
],
|
||||
];
|
||||
|
||||
$decoded = listmonkApiRequest($base, $user, $pass, 'POST', '/api/campaigns', $payload);
|
||||
if ($sendImmediately) {
|
||||
$campaignId = $decoded['data']['id'] ?? $decoded['id'] ?? null;
|
||||
if (!$campaignId) {
|
||||
throw new Exception('Listmonk campaign create succeeded but campaign ID was not returned.');
|
||||
}
|
||||
$sendResponse = listmonkApiRequest($base, $user, $pass, 'PUT', '/api/campaigns/' . rawurlencode((string)$campaignId) . '/status', [
|
||||
'status' => 'running',
|
||||
]);
|
||||
$decoded['send_response'] = $sendResponse;
|
||||
}
|
||||
|
||||
return $decoded;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue