Skip to content

Instantly share code, notes, and snippets.

@phmLabs
Last active December 22, 2017 20:05
Show Gist options
  • Save phmLabs/78fe05c168e4f349cc89ff93670ea2ae to your computer and use it in GitHub Desktop.
Save phmLabs/78fe05c168e4f349cc89ff93670ea2ae to your computer and use it in GitHub Desktop.
<?php
class LeankoalaHandler
{
const ENDPOINT = 'https://monitor.leankoala.com/rest/%s/system/%s/component/add?api_key=%s';
private $projectIdentifier;
private $projectApiKey;
public function __construct($projectIdentifier, $projectApiKey)
{
$this->projectIdentifier = $projectIdentifier;
$this->projectApiKey = $projectApiKey;
}
private function getEndpoint($systemId)
{
return sprintf(self::ENDPOINT, $this->projectIdentifier, $systemId, $this->projectApiKey);
}
public function addComponent($systemId, $url, $name, $type = 'html')
{
$postFields = json_encode(['url' => $url, 'name' => $name, 'type' => $type]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->getEndpoint($systemId));
curl_setopt($ch, CURLOPT_POST, 1); //0 for a get request
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
$response = curl_exec($ch);
if ($response === false) {
$errorMsg = curl_error($ch);
throw new \RuntimeException($errorMsg);
}
return $response;
}
}
$handler = new LeankoalaHandler('pro-218', '<project_api_key>');
$response = $handler->addComponent(1139, 'https://www.goethe.de/ins/nz/de/index.html', 'Wellington');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment