-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket.php
More file actions
78 lines (68 loc) · 2.5 KB
/
Copy pathwebsocket.php
File metadata and controls
78 lines (68 loc) · 2.5 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
<?php
/**
* Real-time news streaming.
*
* NEWSDATA_API_KEY=<your key> php examples/websocket.php
*
* Streaming needs the optional phrity/websocket package (PHP 8.1+):
*
* composer require phrity/websocket
*
* Articles are matched by a registered query. If NEWSDATA_REGISTRATION_ID is
* set, that query is streamed directly; otherwise this registers a demo query
* (q="pizza") first and prints the resulting registration_id so you can reuse
* it on the next run — or remove it later with $ws->delete($id).
*/
declare(strict_types=1);
require_once __DIR__ . '/../vendor/autoload.php';
use NewsdataIO\Exception\NewsdataAPIError;
use NewsdataIO\Exception\NewsdataWebSocketAuthError;
use NewsdataIO\Exception\NewsdataWebSocketError;
use NewsdataIO\NewsdataApi;
use NewsdataIO\NewsdataWebSocket;
$apiKey = getenv('NEWSDATA_API_KEY');
if ($apiKey === false || $apiKey === '') {
fwrite(STDERR, "Set NEWSDATA_API_KEY in your environment before running this example.\n");
exit(1);
}
$api = new NewsdataApi($apiKey);
$ws = new NewsdataWebSocket($api);
/**
* Register q="pizza" and return its registration_id. Registering an identical
* query again answers HTTP 409 with the existing id in the response body —
* reuse it instead of failing.
*/
function registerDemoQuery(NewsdataWebSocket $ws): string
{
try {
$response = $ws->register(['q' => 'pizza']);
$id = $response->results->registration_id;
echo "registered demo query q=\"pizza\" -> {$id}\n";
return $id;
} catch (NewsdataAPIError $e) {
// getResponseBody() decodes to an array regardless of the client's
// setDecodeJsonAsArray() setting.
$body = $e->getResponseBody();
$existing = $body['results']['registration_id'] ?? null;
if ($e->getStatusCode() === 409 && $existing !== null) {
echo "query already registered; reusing {$existing}\n";
return $existing;
}
throw $e;
}
}
$registrationId = getenv('NEWSDATA_REGISTRATION_ID') ?: registerDemoQuery($ws);
echo "streaming {$registrationId} — Ctrl-C to stop\n";
try {
foreach ($ws->stream($registrationId) as $response) {
foreach ($response->results as $article) {
echo $article->title, ' - ', $article->link, PHP_EOL;
}
}
} catch (NewsdataWebSocketAuthError $e) {
fwrite(STDERR, 'rejected: ' . $e->getMessage() . PHP_EOL);
exit(1);
} catch (NewsdataWebSocketError $e) {
fwrite(STDERR, 'stream error: ' . $e->getMessage() . PHP_EOL);
exit(1);
}