Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions lib/PostHog.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,24 @@ public static function init(
if (null === $client) {
$apiKey = $apiKey ?: getenv(self::ENV_API_KEY);

$rawHost = null;
if (array_key_exists("host", $options)) {
$options["host"] = self::cleanHost($options["host"]);
$rawHost = $options["host"];
$options["host"] = self::cleanHost($rawHost);
} else {
$envHost = getenv(self::ENV_HOST) ?: null;
if (null !== $envHost) {
$options["host"] = self::cleanHost(getenv(self::ENV_HOST));
$rawHost = $envHost;
$options["host"] = self::cleanHost($rawHost);
}
}

// Infer ssl from the host protocol if the user hasn't explicitly set it
if ($rawHost !== null && !array_key_exists("ssl", $options)) {
if (substr($rawHost, 0, 7) === "http://") {
$options["ssl"] = false;
} elseif (substr($rawHost, 0, 8) === "https://") {
Comment on lines +45 to +47
Copy link
Copy Markdown
Member

@marandaneto marandaneto Mar 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you do that with startsWith? its likely better than alocating a new string
its less error prone
if PHP < 8 we cna also do like if (strpos($rawHost, "http://") === 0)
do we support PHP 7 and older?

$options["ssl"] = true;
}
}

Expand Down
69 changes: 69 additions & 0 deletions test/PostHogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,75 @@ public function testInitWithEnvApiKey(): void
putenv(PostHog::ENV_API_KEY);
}

public function testInitWithHttpHostSetsSslFalse(): void
{
PostHog::init("random_key", ["host" => "http://localhost:8010"]);

$client = PostHog::getClient();
$ref = new \ReflectionClass($client);
$consumerProp = $ref->getProperty('consumer');
$consumerProp->setAccessible(true);
$consumer = $consumerProp->getValue($client);

$cRef = new \ReflectionClass($consumer);
$httpProp = $cRef->getProperty('httpClient');
$httpProp->setAccessible(true);
$httpClient = $httpProp->getValue($consumer);

$hRef = new \ReflectionClass($httpClient);
$sslProp = $hRef->getProperty('useSsl');
$sslProp->setAccessible(true);

$this->assertFalse($sslProp->getValue($httpClient), 'HttpClient should use ssl=false for http:// hosts');
}

public function testInitWithHttpsHostSetsSslTrue(): void
{
PostHog::init("random_key", ["host" => "https://app.posthog.com"]);

$client = PostHog::getClient();
$ref = new \ReflectionClass($client);
$consumerProp = $ref->getProperty('consumer');
$consumerProp->setAccessible(true);
$consumer = $consumerProp->getValue($client);

$cRef = new \ReflectionClass($consumer);
$httpProp = $cRef->getProperty('httpClient');
$httpProp->setAccessible(true);
$httpClient = $httpProp->getValue($consumer);

$hRef = new \ReflectionClass($httpClient);
$sslProp = $hRef->getProperty('useSsl');
$sslProp->setAccessible(true);

$this->assertTrue($sslProp->getValue($httpClient), 'HttpClient should use ssl=true for https:// hosts');
}

public function testInitWithEnvHttpHostSetsSslFalse(): void
{
putenv(PostHog::ENV_HOST . "=http://localhost:8010");
PostHog::init("random_key");

$client = PostHog::getClient();
$ref = new \ReflectionClass($client);
$consumerProp = $ref->getProperty('consumer');
$consumerProp->setAccessible(true);
$consumer = $consumerProp->getValue($client);

$cRef = new \ReflectionClass($consumer);
$httpProp = $cRef->getProperty('httpClient');
$httpProp->setAccessible(true);
$httpClient = $httpProp->getValue($consumer);

$hRef = new \ReflectionClass($httpClient);
$sslProp = $hRef->getProperty('useSsl');
$sslProp->setAccessible(true);

$this->assertFalse($sslProp->getValue($httpClient), 'HttpClient should use ssl=false for http:// env host');

putenv(PostHog::ENV_HOST);
}

public function testInitThrowsExceptionWithNoApiKey(): void
{
$this->expectException(Exception::class);
Expand Down
Loading