Files
2025-06-24 14:14:35 +02:00

61 lines
1.5 KiB
PHP

<?php
class PrintNodeApi
{
public $api;
public $webhook = 'https://api.printnode.com/';
public function __construct($apiKey)
{
$this->api = $apiKey;
}
public function whoami()
{
return json_decode($this->request('whoami'));
}
public function getPrinters()
{
return json_decode($this->request('printers'));
}
public function printJob($printerId, $content, $title = '', $contentType = 'pdf_uri')
{
$post = array ();
$post['printerId'] = $printerId;
$post['contentType'] = $contentType;
$post['content'] = $content;
$post['title'] = $title;
return $this->request('printjobs', json_encode($post), 'POST');
}
private function request($url, $post = null, $request = 'GET')
{
$authKey = base64_encode($this->api);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $this->webhook.$url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $request,
CURLOPT_HTTPHEADER => array(
'Authorization: Basic '.$authKey,
'Content-Type: application/json'
),
CURLOPT_POSTFIELDS => $post,
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
}