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 CMCService
*
* @author jimmy
*
* exemple response
* {
* "data": {
* "symbol": "BTC",
* "id": "1",
* "name": "Bitcoin",
* "amount": 50,
* "last_updated": "2018-06-06T08:04:36.000Z",
* "quote": {
* "GBP": {
* "price": 284656.08465608465,
* "last_updated": "2018-06-06T06:00:00.000Z"
* },
* "LTC": {
* "price": 3128.7279766396537,
* "last_updated": "2018-06-06T08:04:02.000Z"
* },
* "USD": {
* "price": 381442,
* "last_updated": "2018-06-06T08:06:51.968Z"
* }
* }
* },
* "status": {
* "timestamp": "2019-08-28T08:00:42.816Z",
* "error_code": 0,
* "error_message": "",
* "elapsed": 10,
* "credit_count": 1
* }
* }
*/
class CMCService {
/*
* https://pro-api.coinmarketcap.com/v1/tools/price-conversion?id=1&amount=1&convert=EUR&CMC_PRO_API_KEY=5922b05c-4c84-47c6-a462-f303e6c209be
* payant si on veut à une date précise en attente de réponse
*/
public function getUnitPriceInEur(Devise $devise, \DateTime $dateTime)
{
if (empty($devise->getIdCmc())) {
throw new \Exception('Absence d id CMC pour : ' . $devise->getCode());
}
$url = 'https://pro-api.coinmarketcap.com/v1/tools/price-conversion';
$parameters = [
'id' => $devise->getIdCmc(),
'amount' => '1',
'convert' => 'EUR',
'time' => $dateTime->format(\DateTime::ATOM),
//'CMC_PRO_API_KEY' => '5922b05c-4c84-47c6-a462-f303e6c209be'
];
$headers = [
'Accepts: application/json',
'X-CMC_PRO_API_KEY: 5922b05c-4c84-47c6-a462-f303e6c209be'
];
$qs = http_build_query($parameters); // query string encode the parameters
$request = "{$url}?{$qs}"; // 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
curl_close($curl); // Close request
if (!$response) {
throw new \Exception('Absence de prix pour : ' . $devise->getCode());
}
$response = json_decode($response, true);
if ($response['status'] && $response['status']['error_code'] !== 0) {
throw new \Exception($response['status']['error_message']);
}
if (isset($response['data']['quote']['EUR']['price'])) {
$prixUnitaireEuro = $response['data']['quote']['EUR']['price'];
// on l'ajoute à notre bdd
$this->insertCoursDevise($devise, $dateTime, $prixUnitaireEuro);
return $prixUnitaireEuro;
}
throw new \Exception('Absence de prix pour : ' . $devise->getCode());
}
// public function getPriceByIdCMC($idCMC, $datetime, $devise) {
// $url = 'https://pro-api.coinmarketcap.com/v1/tools/price-conversion';
// $parameters = [
// 'id' => '1',
// 'amount' => '1',
// 'convert' => 'EUR',
// 'time' => $datetime->format(\DateTime::ATOM)
// ];
//
// $headers = [
// 'Accepts: application/json',
// 'X-CMC_PRO_API_KEY: 5922b05c-4c84-47c6-a462-f303e6c209be'
// ];
// $qs = http_build_query($parameters); // query string encode the parameters
// $request = "{$url}?{$qs}"; // 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
// $responseObj = json_decode($response);
// //var_dump($responseObj);
// if ($responseObj && $responseObj->status && $responseObj->status->error_code === 0) {
// return $response->data->quote->EUR->price;
// } else {
//
// throw new \Exception('Pas de prix pour l id CMC : ' . $idCMC . ' code : ' . $devise->getCode());
// }
// }
public function getInfoByCode($code) {
$url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/map';
// $url = 'https://pro-api.coinmarketcap.com/v1//cryptocurrency/listings/latest';
$parameters = [
'symbol' => $code
];
$headers = [
'Accepts: application/json',
'X-CMC_PRO_API_KEY: 5922b05c-4c84-47c6-a462-f303e6c209be'
];
$qs = http_build_query($parameters); // query string encode the parameters
$request = "{$url}?{$qs}"; // 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
$responseObj = json_decode($response);
echo 'response obj';
//var_dump($responseObj);
}
}