mirror of
https://github.com/grocy/grocy.git
synced 2025-09-19 19:11:48 +00:00
49 lines
852 B
PHP
49 lines
852 B
PHP
![]() |
<?php
|
||
|
|
||
|
namespace Grocy\Helpers;
|
||
|
|
||
|
use GuzzleHttp\Client;
|
||
|
use GuzzleHttp\ExceptionRequestException;
|
||
|
use Psr\Http\Message\ResponseInterface;
|
||
|
|
||
|
class WebhookRunner
|
||
|
{
|
||
|
private $client;
|
||
|
|
||
|
public function __construct()
|
||
|
{
|
||
|
$this->client = new Client(['timeout' => 2.0]);
|
||
|
}
|
||
|
|
||
|
public function run($url, $args, $json = false)
|
||
|
{
|
||
|
$reqArgs = [];
|
||
|
if ($json)
|
||
|
{
|
||
|
$reqArgs = ['json' => $args];
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
$reqArgs = ['form_params' => $args];
|
||
|
}
|
||
|
try
|
||
|
{
|
||
|
file_put_contents('php://stderr', 'Running Webhook: ' . $url . "\n" . print_r($reqArgs, true));
|
||
|
|
||
|
$this->client->request('POST', $url, $reqArgs);
|
||
|
}
|
||
|
catch (RequestException $e)
|
||
|
{
|
||
|
file_put_contents('php://stderr', 'Webhook failed: ' . $url . "\n" . $e->getMessage());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function runAll($urls, $args)
|
||
|
{
|
||
|
foreach ($urls as $url)
|
||
|
{
|
||
|
$this->run($url, $args);
|
||
|
}
|
||
|
}
|
||
|
}
|