questions

Change Variable after each Rest API Call out of a list of Stock symbol names until the list is done in WP?

Rate this post

I’m trying to use the alphavantage REST API to output stock prices through Advanced Custom Fields. My issue right now is that I don’t know how I can create a list of stocksymbol names (we have specific stocks we want to check) that changes each call until all stocks have been checked.

This is my current WordPress Code in functions.php

/* Register Stock Price Custom Post Type */
add_action('init', 'register_aktienpreise_cpt');

function register_aktienpreise_cpt(){
    register_post_type('aktienpreise', [
    'label' => 'Aktienpreise',
    'public' => true,
    'capability_type' => 'post'
    ]);
}

add_action( 'wp_ajax_nopriv_get_aktienpreise_from_api', 'get_aktienpreise_from_api' );
add_action( 'wp_ajax_get_aktienpreise_from_api', 'get_aktienpreise_from_api' );

/* Initialize Function */
function get_aktienpreise_from_api(){
    /* Set Log File */
    $file = get_stylesheet_directory() . '/report.txt';
    
    /* Declare Current Symbol Variable and check if its not empty */
    $current_symbol = ( ! empty($_POST['current_symbol']) ) ? $_POST['current_symbol'] : '';
    $aktienpreise = [];
    
    /* Results output for AlphaVantage Rest API + APIKEY - Add Current Symbol Set for each API Call */
    $results = wp_remote_retrieve_body( wp_remote_get('https://www.alphavantage.co/query?function=GLOBAL_QUOTE&apikey=demo&symbol=' . $current_symbol_set ));
    file_put_contents($file, "Current Symbol: " . $current_symbol. "\n\n", FILE_APPEND);

  // turn it into a PHP array from JSON string
  $results = json_decode( $results );   
  
  // Either the API is down or something else spooky happened. Just be done.
  if( ! is_array( $results ) || empty( $results ) ){
    return false;
  }

/* Change Current Stock Symbol for the next call until empty */
 $current_symbol = $current_symbol + $current_symbol_set;
    
/* Repeat Calls until results are empty */
 wp_remote_post( admin_url('admin-ajax.php?action=get_aktienpreise_from_api'), [
     'blocking' => false,
     'sslverify' => false,
     'body' => [
         'current_symbol' => $current_symbol
     ]
 ] );
}

At this part

/* Change Current Stock Symbol for the next call until empty */
 $current_symbol = $current_symbol + $current_symbol_set;

I’d like to have a list of specific stocks I want to check (for example Apple, IBM etc.).

The goal is that on each call the variable changes to the next stock (lets say first call Apple, second IBM), then writes those into the specific ACF Fields that I can showcase in a table on our website.

If the restapi urls would just have number increases it would be easy, but how do I do it with specific terms like “IBM” etc.

Furthermore if there are easier solutions to this please tell me so since I’m fairly new to REST APIs. The Goal in general is to just display current stockprices of specific stocks inside a table on our website, preferably inside an ACF Field so we can do math formulas on the price (for example price x 2 for another table field).

Thanks for everyone taking their time to help me out!

 

✔️Solution:

Why don’t you use an array for the stock symbols and do the AlphaVantage API calls inside a loop?

I will give you a quick example:

$my_symbols = [
  'IBM',
  'Microsoft',
  'Tata'
];

foreach( $my_symbols as $current_symbol )  // iterate through the array elements
{
  //... make the API calls to AlphaVantage 
  //... do any other business logic, like storing the API result in file or update the stocks, etc..
}

One thing to keep in mind is that you should make sure the script won’t get a timeout. So increase the max_execution_time to a higher value.

Also, you don’t have to call your own API here. I mean you don’t have to do the wp_remote_post() to your own website.

Leave a Reply

Your email address will not be published.

Back to top button