Skip to content

Instantly share code, notes, and snippets.

@kasparsd
Created January 22, 2026 10:48
Show Gist options
  • Select an option

  • Save kasparsd/7acdc0fd0e474e18c99930278c40a81b to your computer and use it in GitHub Desktop.

Select an option

Save kasparsd/7acdc0fd0e474e18c99930278c40a81b to your computer and use it in GitHub Desktop.
PHP client for Systeme.io API
<?php
class Systeme_API {
const API_URL = 'https://api.systeme.io/api';
public function __construct( private string $api_key ) {}
private function url_to( string $endpoint, ?array $params = [] ): string {
$url = sprintf( '%s/%s', self::API_URL, ltrim( $endpoint, '/' ) );
if ( ! empty( $params ) ) {
return $url . '?' . http_build_query( $params );
}
return $url;
}
private function get_context( string $method, ?array $body = [] ) {
$headers = [
'X-API-Key' => $this->api_key,
'Accept' => 'application/json',
'Content-Type' => 'application/json',
];
return stream_context_create(
[
'http' => [
'ignore_errors' => true,
'method' => strtoupper( $method ),
'header' => implode( "\r\n", array_map( fn( $k, $v ) => "$k: $v", array_keys( $headers ), $headers ) ),
'content' => ! empty( $body ) ? json_encode( $body ) : null,
],
]
);
}
private function call( string $method, string $endpoint, ?array $params = [], ?array $body = [] ): array {
$response = file_get_contents(
$this->url_to( $endpoint, $params ),
false,
$this->get_context( $method, $body )
);
if ( function_exists( 'http_get_last_response_headers' ) ) {
$response_headers = http_get_last_response_headers();
} else {
// $response_headers = @$http_response_header;
}
preg_match( '/HTTP\/\d\.\d\s+(\d{3})/', $response_headers[0], $matches );
if ( false === $response ) {
$error = error_get_last();
throw new RuntimeException( $error['message'] ?? 'Unknown error occurred while making API request.' );
}
return [
'status' => (int) $matches[1],
'body' => json_decode( $response, true ),
];
}
private function get( string $endpoint, ?array $params = [] ): array {
return $this->call( 'GET', $endpoint, $params )['body'];
}
private function post( string $endpoint, ?array $body = [] ) {
$this->call( 'POST', $endpoint, null, $body );
}
public function get_contacts(): array {
$contacts = $this->get( 'contacts', [ 'limit' => 100 ] );
return $contacts['items'];
}
public function get_students( string $course_id ): array {
$students = $this->get( 'school/enrollments', [
'course' => $course_id,
'limit' => 100,
] );
return $students['items'];
}
public function get_tags(): array {
$tags = $this->get( 'tags', [ 'limit' => 100 ] );
return $tags['items'];
}
public function set_tag( string $contact_id, int $tag_id ) {
$this->post(
sprintf( 'contacts/%s/tags', $contact_id ),
[ 'tagId' => $tag_id ]
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment