Skip to content

Instantly share code, notes, and snippets.

@ethaniel
Created March 1, 2024 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ethaniel/72dae721ff770b7e1dcb0101589ed361 to your computer and use it in GitHub Desktop.
Save ethaniel/72dae721ff770b7e1dcb0101589ed361 to your computer and use it in GitHub Desktop.
Simple PHP script to connect to Hikvision notification API
<?php
// Composer dependencies:
// composer require amphp/socket amphp/byte-stream
// IMPORTANT: Go to System > Security -> Authentication tab, set "WEB Authentication" to "digest/basic" first!!!
// Otherwise you will get authentication errors
require __DIR__ . '/vendor/autoload.php';
use Amp\Loop;
use Amp\Socket\ClientTlsContext;
use Amp\Socket\ConnectContext;
use function Amp\Socket\connect;
$url = "https://bar.ath.cx:409/ISAPI/Event/notification/alertStream";
Loop::run(static function () {
$stdout = Amp\ByteStream\getStdout();
$host = "10.10.13.49"; // ip or hostname of camera
$port = 443; // http or https port
$https = true; // set to false if using port 80
$username = "username";
$password = "password";
// IMPORTANT: Go to System > Security -> Authentication tab, set "WEB Authentication" to "digest/basic" first!!!
// Otherwise you will get authentication errors
$tls = (new ClientTlsContext($host))->withoutPeerVerification();
$connectContext = (new ConnectContext)->withTlsContext($tls);
$socket = yield connect($host . ':' . $port, $connectContext);
if ($https == true) {
yield $socket->setupTls();
}
yield $socket->write("GET /ISAPI/Event/notification/alertStream HTTP/1.1\r\nHost: $host\r\nAuthorization: Basic " . base64_encode($username . ":" . $password) . "\r\nAccept: multipart/x-mixed-replace\r\n\r\n");
while (null !== $chunk = yield $socket->read()) {
if (strstr($chunk, '<eventDescription>')) {
echo $chunk; // notification data is here
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment