From 51637d7e91d9e41417d413edfe194bafac89cf99 Mon Sep 17 00:00:00 2001 From: Adriaan Zonnenberg Date: Mon, 17 Aug 2026 14:29:01 +0200 Subject: [PATCH] Allow setting the Referer header --- src/Connection.php | 16 +++++++++++++++- tests/ConnectionTest.php | 13 +++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Connection.php b/src/Connection.php index 16133b3..17661ee 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -24,7 +24,7 @@ */ class Connection { - public const VERSION = '3.0.1'; + public const VERSION = '3.1.0'; public const BASE_URL = 'https://app.sendy.nl'; @@ -60,6 +60,9 @@ class Connection /** @var string The appendix for the user agent */ private string $userAgentAppendix = ''; + /** @var string|null The URL of the application or website using this SDK, sent as the Referer header */ + private ?string $referer = null; + /** @var mixed */ private $state = null; @@ -96,6 +99,13 @@ public function setUserAgentAppendix(string $userAgentAppendix): Connection return $this; } + public function setReferer(?string $referer): Connection + { + $this->referer = $referer; + + return $this; + } + public function setClientId(string $clientId): Connection { $this->clientId = $clientId; @@ -295,6 +305,10 @@ public function createRequest( 'User-Agent' => trim($userAgent), ]); + if ($this->referer !== null) { + $headers['Referer'] = $this->referer; + } + if (! empty($params)) { $endpoint .= strpos($endpoint, '?') === false ? '?' : '&'; $endpoint .= http_build_query($params); diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 0f63480..238c392 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -46,6 +46,19 @@ public function testUserAgentIsSet(): void ); } + public function testRefererIsSet(): void + { + $connection = $this->createConnection(); + $this->assertArrayNotHasKey('referer', $connection->createRequest('GET', '/')->getHeaders()); + + $connection = $this->createConnection(); + $connection->setReferer('https://example.com'); + $this->assertEquals( + 'https://example.com', + $connection->createRequest('GET', '/')->getHeaders()['referer'], + ); + } + public function testTokenExpires(): void { $connection = new Connection();