first commit symfony 6
This commit is contained in:
89
src/Service/ApiService.php
Normal file
89
src/Service/ApiService.php
Normal file
@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
||||
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
||||
|
||||
class ApiService
|
||||
{
|
||||
private $params;
|
||||
|
||||
public function __construct(ParameterBagInterface $params)
|
||||
{
|
||||
$this->params = $params;
|
||||
}
|
||||
|
||||
public function setbody(Array $array)
|
||||
{
|
||||
return \Unirest\Request\Body::json($array);
|
||||
}
|
||||
|
||||
public function run($method,$url,$query,$header=null) {
|
||||
// Entete
|
||||
$headerini = [
|
||||
'Accept' => 'application/json',
|
||||
'Content-Type' => 'application/json',
|
||||
];
|
||||
|
||||
if($header) $header=array_merge($headerini,$header);
|
||||
else $header=$headerini;
|
||||
|
||||
// Paramétrage unirest
|
||||
\Unirest\Request::verifyPeer(false);
|
||||
\Unirest\Request::verifyHost(false);
|
||||
\Unirest\Request::timeout(5);
|
||||
|
||||
// Déclaration du proxy
|
||||
$proxyUse = $this->params->get("proxyUse");
|
||||
if($proxyUse) {
|
||||
$proxyHost = $this->params->get("proxyHost");
|
||||
$proxyPort = $this->params->get("proxyPort");
|
||||
\Unirest\Request::proxy($proxyHost, $proxyPort, CURLPROXY_HTTP, true);
|
||||
}
|
||||
|
||||
if($query) $query = \Unirest\Request\Body::json($query);
|
||||
|
||||
$response = false;
|
||||
switch($method) {
|
||||
case "POST":
|
||||
try{
|
||||
$response = \Unirest\Request::post($url,$header,$query);
|
||||
}
|
||||
catch (\Exception $e) {
|
||||
die();
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
|
||||
case "GET":
|
||||
try{
|
||||
$response = @\Unirest\Request::get($url,$header,$query);
|
||||
}
|
||||
catch (\Exception $e) {
|
||||
dump($e);
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
|
||||
case "DELETE":
|
||||
try{
|
||||
$response = \Unirest\Request::delete($url,$header,$query);
|
||||
}
|
||||
catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
|
||||
case "PATCH":
|
||||
try{
|
||||
$response = \Unirest\Request::patch($url,$header,$query);
|
||||
}
|
||||
catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user