2022-07-21 16:15:47 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Service;
|
|
|
|
|
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
|
|
|
|
|
|
|
class ApiService
|
|
|
|
{
|
|
|
|
private $params;
|
2022-09-23 16:14:15 +02:00
|
|
|
|
2022-07-21 16:15:47 +02:00
|
|
|
public function __construct(ParameterBagInterface $params)
|
|
|
|
{
|
|
|
|
$this->params = $params;
|
|
|
|
}
|
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
public function setbody(array $array)
|
2022-07-21 16:15:47 +02:00
|
|
|
{
|
|
|
|
return \Unirest\Request\Body::json($array);
|
|
|
|
}
|
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
public function run($method, $url, $query, $header = null, $content = 'json')
|
|
|
|
{
|
2022-07-21 16:15:47 +02:00
|
|
|
// Entete
|
2022-09-23 16:14:15 +02:00
|
|
|
$headerini = null;
|
|
|
|
switch ($content) {
|
|
|
|
case 'json':
|
2022-08-26 15:13:22 +02:00
|
|
|
$headerini = [
|
|
|
|
'Accept' => 'application/json',
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
];
|
2022-09-23 16:14:15 +02:00
|
|
|
if ($query) {
|
|
|
|
$query = \Unirest\Request\Body::json($query);
|
|
|
|
}
|
|
|
|
break;
|
2022-08-26 15:13:22 +02:00
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
case 'form':
|
2022-08-26 15:13:22 +02:00
|
|
|
$headerini = [
|
|
|
|
'Accept' => 'application/json',
|
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded',
|
2022-09-23 16:14:15 +02:00
|
|
|
];
|
|
|
|
if ($query) {
|
|
|
|
$query = \Unirest\Request\Body::form($query);
|
|
|
|
}
|
|
|
|
break;
|
2022-08-26 15:13:22 +02:00
|
|
|
}
|
2022-07-21 16:15:47 +02:00
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
if ($header) {
|
|
|
|
$header = array_merge($headerini, $header);
|
|
|
|
} else {
|
|
|
|
$header = $headerini;
|
|
|
|
}
|
2022-07-21 16:15:47 +02:00
|
|
|
|
|
|
|
// Paramétrage unirest
|
|
|
|
\Unirest\Request::verifyPeer(false);
|
|
|
|
\Unirest\Request::verifyHost(false);
|
|
|
|
\Unirest\Request::timeout(5);
|
|
|
|
|
|
|
|
// Déclaration du proxy
|
2022-09-23 16:14:15 +02:00
|
|
|
$proxyUse = $this->params->get('proxyUse');
|
|
|
|
if ($proxyUse) {
|
|
|
|
$proxyHost = $this->params->get('proxyHost');
|
|
|
|
$proxyPort = $this->params->get('proxyPort');
|
2022-07-21 16:15:47 +02:00
|
|
|
\Unirest\Request::proxy($proxyHost, $proxyPort, CURLPROXY_HTTP, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = false;
|
2022-09-23 16:14:15 +02:00
|
|
|
switch ($method) {
|
|
|
|
case 'POST':
|
|
|
|
try {
|
|
|
|
$response = \Unirest\Request::post($url, $header, $query);
|
|
|
|
} catch (\Exception $e) {
|
2022-07-21 16:15:47 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
case 'GET':
|
|
|
|
try {
|
|
|
|
$response = @\Unirest\Request::get($url, $header, $query);
|
|
|
|
} catch (\Exception $e) {
|
2022-07-21 16:15:47 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
case 'PUT':
|
|
|
|
try {
|
|
|
|
$response = \Unirest\Request::put($url, $header, $query);
|
|
|
|
} catch (\Exception $e) {
|
2022-08-27 18:24:56 +02:00
|
|
|
return false;
|
|
|
|
}
|
2022-09-23 16:14:15 +02:00
|
|
|
break;
|
2022-08-27 18:24:56 +02:00
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
case 'DELETE':
|
|
|
|
try {
|
|
|
|
$response = \Unirest\Request::delete($url, $header, $query);
|
|
|
|
} catch (\Exception $e) {
|
2022-07-21 16:15:47 +02:00
|
|
|
return false;
|
|
|
|
}
|
2022-09-23 16:14:15 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'PATCH':
|
|
|
|
try {
|
|
|
|
$response = \Unirest\Request::patch($url, $header, $query);
|
|
|
|
} catch (\Exception $e) {
|
2022-07-21 16:15:47 +02:00
|
|
|
return false;
|
|
|
|
}
|
2022-09-23 16:14:15 +02:00
|
|
|
break;
|
2022-07-21 16:15:47 +02:00
|
|
|
}
|
2022-09-23 16:14:15 +02:00
|
|
|
|
2022-07-21 16:15:47 +02:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
}
|