forked from victorycodedev/metaapi-cloud-php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhooks_example.php
More file actions
96 lines (80 loc) · 2.77 KB
/
webhooks_example.php
File metadata and controls
96 lines (80 loc) · 2.77 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
require_once __DIR__ . '/../../vendor/autoload.php';
use Oyi77\MetaapiCloudPhpSdk\CopyFactory;
/**
* CopyFactory Webhooks Example
*
* Demonstrates how to:
* - Create webhooks for a strategy
* - List webhooks
* - Update webhooks
* - Delete webhooks
*/
$token = getenv('TOKEN') ?: '<put your token here>';
$strategyId = getenv('STRATEGY_ID') ?: '<put your strategy ID here>';
try {
// Initialize CopyFactory API
$copyFactory = new CopyFactory($token);
// Create a webhook
echo "Creating webhook for strategy {$strategyId}...\n";
$webhook = $copyFactory->createWebhook($strategyId, [
'url' => 'https://example.com/webhook',
'method' => 'POST',
'headers' => [
'Authorization' => 'Bearer secret-token',
],
'type' => 'subscriber', // 'subscriber' or 'provider'
]);
if (is_array($webhook)) {
$webhookId = $webhook['_id'] ?? $webhook['id'] ?? null;
echo "Webhook created: {$webhookId}\n";
print_r($webhook);
} else {
echo "Webhook response: {$webhook}\n";
}
echo "\n";
// List all webhooks
echo "Listing all webhooks for strategy {$strategyId}...\n";
$webhooks = $copyFactory->webhooksWithInfiniteScroll($strategyId);
if (is_array($webhooks)) {
echo "Found " . count($webhooks) . " webhook(s)\n";
foreach ($webhooks as $wh) {
echo " - " . ($wh['_id'] ?? $wh['id'] ?? 'unknown') . ": " . ($wh['url'] ?? 'no url') . "\n";
}
} else {
echo "Webhooks response: {$webhooks}\n";
}
echo "\n";
// Update webhook (if we have one)
if (isset($webhookId)) {
echo "Updating webhook {$webhookId}...\n";
$updatedWebhook = $copyFactory->updateWebhook($strategyId, $webhookId, [
'url' => 'https://example.com/webhook/updated',
'method' => 'POST',
'headers' => [
'Authorization' => 'Bearer updated-token',
'X-Custom-Header' => 'custom-value',
],
]);
if (is_array($updatedWebhook)) {
echo "Webhook updated successfully\n";
print_r($updatedWebhook);
} else {
echo "Update response: {$updatedWebhook}\n";
}
echo "\n";
// Delete webhook
echo "Deleting webhook {$webhookId}...\n";
$deleteResponse = $copyFactory->deleteWebhook($strategyId, $webhookId);
echo "Webhook deleted\n";
if (is_string($deleteResponse)) {
echo "Response: {$deleteResponse}\n";
}
} else {
echo "Skipping update/delete (no webhook ID available)\n";
}
echo "\nWebhook operations complete!\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
echo $e->getTraceAsString() . "\n";
}