87 lines
2.6 KiB
PHP
87 lines
2.6 KiB
PHP
<?php
|
|
|
|
require_once(__DIR__ . '/../wp-load.php');
|
|
|
|
getIntradayData();
|
|
|
|
//Fonction Pour récupérer les données de l'action (A utiliser dans un cron toutes les 15 minutes entre 09h 17h30)
|
|
function getIntradayData() {
|
|
|
|
if (date('H:i:s') > date('H:i:s', strtotime('17:30:00')))
|
|
return;
|
|
|
|
global $wpdb;
|
|
$token = "A0RACEYVTKIGRF5M";
|
|
$baseUrl = 'https://www.alphavantage.co/query?apikey=' . $token . '&function=TIME_SERIES_INTRADAY&symbol=LOUP.PA&interval=15min&outputsize=full';
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_URL, $baseUrl);
|
|
$response = curl_exec($ch);
|
|
|
|
if (!curl_errno($ch)) {
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
if ($http_code !== 200) {
|
|
email(MAIL_ADMIN, '[ERROR] Erreur "API" Alphavantage : getIntradayData (1)', 'Code HTTP de l\'erreur : ' . $http_code);
|
|
die();
|
|
}
|
|
}else{
|
|
email(MAIL_ADMIN, '[ERROR] Erreur "API" Alphavantage : getIntradayData (1)', 'Erreur cURL');
|
|
die();
|
|
}
|
|
|
|
curl_close($ch);
|
|
|
|
$intraday = json_decode($response, true);
|
|
|
|
if (!isset($intraday['Time Series (15min)'])) {
|
|
email(MAIL_ADMIN, '[ERROR] Erreur "API" Alphavantage : getIntradayData (1)', 'Champs requis manquant.');
|
|
return;
|
|
}
|
|
// Clear table
|
|
$wpdb->query("DELETE FROM wp_financialdata_intraday");
|
|
|
|
foreach ($intraday['Time Series (15min)'] as $date => $datas) {
|
|
|
|
// Skip <> current day datas
|
|
if (date('Y-m-d', strtotime($date)) !== date('Y-m-d'))
|
|
continue;
|
|
|
|
$open = $datas['1. open'];
|
|
$high = $datas['2. high'];
|
|
$low = $datas['3. low'];
|
|
$close = $datas['4. close'];
|
|
$volume = $datas['5. volume'];
|
|
|
|
$fDate = new DateTime($date, new DateTimeZone('America/New_York'));
|
|
$tDate = $fDate->setTimezone(new DateTimeZone('Europe/Paris'));
|
|
|
|
$wpdb->insert(
|
|
'wp_financialdata_intraday',
|
|
[
|
|
'date' => $tDate->format('Y-m-d H:i:s'),
|
|
'open' => $open,
|
|
'high' => $high,
|
|
'low' => $low,
|
|
'close' => $close,
|
|
'volume' => $volume
|
|
], [
|
|
'%s',
|
|
'%f',
|
|
'%f',
|
|
'%f',
|
|
'%f',
|
|
'%f',
|
|
'%d'
|
|
]
|
|
);
|
|
}
|
|
}
|
|
|
|
function email($email, $subject, $message)
|
|
{
|
|
$headers[] = 'From: Groupe LDC <nepasrepondre@ldc.fr>';
|
|
wp_mail($email, $subject, $message, $headers);
|
|
}
|