HEX
Server: Apache/2.4.58 (Ubuntu)
System: Linux bsx-1-dev 6.8.0-101-generic #101-Ubuntu SMP PREEMPT_DYNAMIC Mon Feb 9 10:15:05 UTC 2026 x86_64
User: www-data (33)
PHP: 8.3.6
Disabled: NONE
Upload Files
File: /var/www/html/wp-content/plugins/bsx/be/bsx-rest-get.php
<?php

// ==========================================================================================

function bsx_rest_get($api, $args=NULL) {

    $url0 = BACKEND_API_URL . '/' . $api;
    if(is_null($args)) {
        $param = '';
    } else {
        $params = [];
        foreach($args as $key => $val) $params[] = $key . "=" . $val;
        $param  = "?" . implode('&',$params);
    }        
    $url = $url0 . $param;
    error_log($url);
    $response = wp_remote_get(
        $url,
        array(
            'headers' => array(
                'X-API-Key' => BACKEND_API_KEY,
                'Content-Type' => 'application/json'
            ),
            'timeout' => 30
        )
    );

    if (is_wp_error($response)) {
        error_log('sync failed: ' . $response->get_error_message());
        return;
    }

    $body = json_decode(wp_remote_retrieve_body($response), true);
    
    return $body;
}



// ==========================================================================================
/* ==========================================================================================
 * Yahoo: Fetch latest quotes for a single symbol.
 * Returns array: ['dt'=>'YYYY-MM-DD', 'open'=>..., 'high'=>..., 'low'=>..., 'close'=>...]
 */


function bsx_get_yahoo_quote($ysym, $startdt = null, $int='15m') {
    global $tz;

    $now = current_time("timestamp");
    
    if(!is_null($startdt)) {
        $per1 = $startdt; 
    } else {
        $per1 =  $now - 50 * 24 * 3600;
    }    


    $url = 'https://query1.finance.yahoo.com/v8/finance/chart/' . $ysym . '?period1=' . $per1 . '&period2=' . $now .'&interval=' . $int ;

    $url = str_replace('^','%5E',$url);
    
    $response = wp_remote_get($url);
    if ( is_wp_error($response) ) return false;
    
    $body = wp_remote_retrieve_body($response);
    $data = json_decode($body, true);

    if ( !isset($data['chart']['result'][0]) ) return false;

    $result = $data['chart']['result'][0];
    $timestamps = $result['timestamp'] ?? []; 
    $quote = $result['indicators']['quote'][0] ?? [];
    $open  = $quote['open']  ?? [];
    $high  = $quote['high']  ?? [];
    $low   = $quote['low']   ?? [];
    $close = $quote['close'] ?? [];

    $quotes = [];
    foreach ( $timestamps as $i => $ts ) {
        // skip points where OHLC missing
        if ( ! isset( $open[$i], $high[$i], $low[$i], $close[$i] ) ) continue;
        if ( $open[$i] === null || $high[$i] === null || $low[$i] === null || $close[$i] === null ) continue;
        $quotes[] = [
            'dt' => intval( $ts ) + $tz[0],
            'open' => (float) $open[$i],
            'high' => (float) $high[$i],
            'low'  => (float) $low[$i],
            'close'=> (float) $close[$i]
        ];
    }
    if ( !$quotes ) return false;
    return $quotes;
}