Current path: home/webdevt/cryptoimpot.fr/module/Application/src/Service/ApiHistorical/
?? Go up: /home/webdevt/cryptoimpot.fr/module/Application/src/Service
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace Application\Service\ApiHistorical;
use Application\Entity\Devise;
/**
* Description of CoinAPIService
* https://docs.coinapi.io/#md-docs
*
* @author jimmy
*/
class CoinAPIService
{
/*
* https://rest.coinapi.io/v1/exchangerate/KCS/EUR?apikey=3862582B-9493-425E-AFAE-AB6AFEAE35D5&time=2018-01-06T10:40:01.0000000
* exemple retour {
"error": "You requested specific single item that we don\u0027t have at this moment."
}
*/
public function getUnitPriceInEur(Devise $devise, \DateTime $dateTime)
{
if (empty($devise->getIdCoinApi())) {
throw new \Exception('Absence d id Coin API pour : ' . $devise->getCode());
}
$url = 'https://rest.coinapi.io/v1/exchangerate/' . $devise->getIdCoinApi() . '/EUR?apikey=3862582B-9493-425E-AFAE-AB6AFEAE35D5&time=' . $dateTime->format('Y-m-d\TH:i:s.uZ');
$headers = [
//'Accepts: application/json',
'X-CoinAPI-Key' => '3862582B-9493-425E-AFAE-AB6AFEAE35D5'
];
// $qs = http_build_query($parameters); // query string encode the parameters
$request = "{$url}"; // create the request URL
$curl = curl_init(); // Get cURL resource
// Set cURL options
curl_setopt_array($curl, array(
CURLOPT_URL => $request, // set the request URL
CURLOPT_HTTPHEADER => $headers, // set the headers
CURLOPT_RETURNTRANSFER => 1 // ask for raw response instead of bool
));
$response = curl_exec($curl); // Send the request, save the response
//print_r(json_decode($response)); // print json decoded response
curl_close($curl); // Close request
if (!empty($response) && !empty($response['rate'])) {
return $response['rate'];
} else {
throw new \Exception('Prix en EUR du : ' . $devise->getCode() . ' non trouvé.');
}
}
}