61 lines
1.6 KiB
PHP
61 lines
1.6 KiB
PHP
<?php
|
|
|
|
require_once(__DIR__ . '/../public/wp/wp-load.php');
|
|
|
|
getStockDatas();
|
|
|
|
function getStockDatas()
|
|
{
|
|
if (date('H:i:s') > date('H:i:s', strtotime('17:30:00')))
|
|
return;
|
|
|
|
global $wpdb;
|
|
$token = ALPHAVANTAGE_API_KEY;
|
|
|
|
$baseUrl = 'https://www.alphavantage.co/query?apikey=' . $token . '&function=GLOBAL_QUOTE&symbol=LOUP.PA';
|
|
|
|
$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 : getStockDatas', 'Code HTTP de l\'erreur : ' . $http_code);
|
|
die();
|
|
}
|
|
} else {
|
|
email(MAIL_ADMIN, '[ERROR] Erreur "API" Alphavantage : getStockDatas', 'Erreur cURL');
|
|
die();
|
|
}
|
|
|
|
curl_close($ch);
|
|
|
|
$stock = json_decode($response, true);
|
|
|
|
if (!isset($stock['Global Quote'])) {
|
|
email(MAIL_ADMIN, '[ERROR] Erreur "API" Alphavantage : getStockDatas', 'Champs requis manquant.');
|
|
return;
|
|
}
|
|
|
|
$wpdb->replace(
|
|
'wp_financialdata',
|
|
array(
|
|
'name' => 'stock',
|
|
'data' => json_encode($stock['Global Quote']),
|
|
),
|
|
array(
|
|
'%s',
|
|
'%s',
|
|
)
|
|
);
|
|
}
|
|
|
|
function email($email, $subject, $message)
|
|
{
|
|
$headers[] = 'From: Groupe LDC <nepasrepondre@ldc.fr>';
|
|
wp_mail($email, $subject, $message, $headers);
|
|
}
|