-
-
Notifications
You must be signed in to change notification settings - Fork 177
PHP SSE Client
Akram El Assas edited this page Jul 19, 2025
·
2 revisions
- Install PHP
- On Windows, enable
curl
inphp.ini
:
extension_dir = "ext"
extension=curl
Here is a sample PHP SSE client sse.php
:
<?php
function login($username, $password, $stayConnected = false) {
$url = 'http://localhost:8000/api/v1/login';
$data = json_encode([
'username' => $username,
'password' => $password,
'stayConnected' => $stayConnected
]);
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_POSTFIELDS => $data
]);
$response = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($statusCode !== 200) {
throw new Exception("Login failed: HTTP $statusCode");
}
$json = json_decode($response, true);
return $json['access_token'];
}
function startWorkflow($token, $workflowId) {
$url = "http://localhost:8000/api/v1/start?w=$workflowId";
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
"Authorization: Bearer $token"
]
]);
$response = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($statusCode !== 200) {
throw new Exception("Start workflow failed: HTTP $statusCode");
}
return json_decode($response, true);
}
function listenToSse($url, $token) {
$headers = [
"Authorization: Bearer $token",
"Accept: text/event-stream"
];
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_HTTPHEADER => $headers,
CURLOPT_WRITEFUNCTION => function($ch, $data) {
if (strpos($data, "data: ") === 0) {
$json = trim(substr($data, 6));
$decoded = json_decode($json, true);
if ($decoded !== null) {
echo "Received SSE JSON:\n";
print_r($decoded);
} else {
echo "Invalid SSE JSON\n";
}
}
return strlen($data);
},
CURLOPT_TIMEOUT => 0,
CURLOPT_RETURNTRANSFER => false,
]);
echo "Opening SSE connection...\n";
curl_exec($ch);
if (curl_errno($ch)) {
echo "SSE error: " . curl_error($ch) . "\n";
}
curl_close($ch);
}
// ---- Main execution ----
try {
$username = 'admin';
$password = 'wexflow2018';
$workflowId = 41;
$baseUrl = 'http://localhost:8000/api/v1';
$token = login($username, $password);
$jobId = startWorkflow($token, $workflowId);
echo "Workflow $workflowId started. Job ID: $jobId\n";
$sseUrl = "$baseUrl/sse/$workflowId/$jobId";
listenToSse($sseUrl, $token);
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage() . "\n";
}
To run the client, use the following command:
php sse.php
Copyright © Akram El Assas. All rights reserved.
- Install Guide
- HTTPS/SSL
- Screenshots
- Docker
- Configuration Guide
- Persistence Providers
- Getting Started
- Android App
- Local Variables
- Global Variables
- REST Variables
- Functions
- Cron Scheduling
- Command Line Interface (CLI)
- REST API Reference
- Samples
- Logging
- Custom Tasks
-
Built-in Tasks
- File system tasks
- Encryption tasks
- Compression tasks
- Iso tasks
- Speech tasks
- Hashing tasks
- Process tasks
- Network tasks
- XML tasks
- SQL tasks
- WMI tasks
- Image tasks
- Audio and video tasks
- Email tasks
- Workflow tasks
- Social media tasks
- Waitable tasks
- Reporting tasks
- Web tasks
- Script tasks
- JSON and YAML tasks
- Entities tasks
- Flowchart tasks
- Approval tasks
- Notification tasks
- SMS tasks
- Run from Source
- Fork, Customize, and Sync