简体   繁体   English

如何验证股市数据

[英]How to validate Stock Market data

Honestly, I am not an expert & right now very much confused about how to even state my problem...so please forgive my lack of knowledge and this long confusing question. 老实说,我不是专家,现在对如何陈述我的问题非常困惑......所以请原谅我缺乏知识和这个长期混乱的问题。

I was assigned a project today where the clients are displaying stock market's info on their page (image attached below). 今天我被分配了一个项目,客户在他们的页面上显示股票市场的信息(图片附在下面)。 And when you click on any one of the buttons (for example, NASDAQ) more info is displayed in a pop-up box. 当您单击任何一个按钮(例如,NASDAQ)时,弹出框中会显示更多信息。

屏幕截图

They are using onClick() to send the whole string to this third party to collect the data. 他们使用onClick()将整个字符串发送给第三方以收集数据。 Here is the HTML code for NASDAQ link: 这是NASDAQ链接的HTML代码:

    <li>
        <a href="#" onClick="open('https://app.quotemedia.com/quotetools/clientForward?symbol=^NASD&targetURL=http://app.quotemedia.com/quotetools/popups/quote.jsp?webmasterId=99944&locale=en_US','miniwin','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=550,height=270,top=20,left=0'); return false;">  

           NASDAQ             
           <span id="imageNASDAQ"></span> 
           <span id="valueNASDAQ" class="share_value"></span> 
           <span id="textNASDAQ"></span> 
        </a> 

        <script type="text/javascript" src="/getStockInfo.php?Stocks[NASD]=NASDAQ"></script>
    </li>

And then in getStockInfo.php file they are collecting the data as a JSON string and then parsing it. 然后在getStockInfo.php文件中,他们将数据收集为JSON字符串,然后解析它。 Here's how they are collecting the data: 以下是他们收集数据的方式:

    <?php
    if (array_key_exists("Stocks", $_GET)) {
        foreach($_GET['Stocks'] as $symbol=>$stock) {
           print file_get_contents("https://app.quotemedia.com/quotetools/jsVarsQuotes.go?webmasterId=99944&symbol=$symbol");
    ?>

So far pretty simple. 到目前为止很简单。 But now the client wants to do some 但现在客户想要做一些

  • "user input validation" “用户输入验证”
  • "Only accept 4 symbols: SP500, SPX, DOW & NASDAQ" “只接受4个符号:SP500,SPX,DOW和NASDAQ”

This is where I am getting confused. 这是我感到困惑的地方。 From their code (HTML part) looks like everything is hard coded ( open('...symbol=^NASD...'); or open('...symbol=^SPX...'); or open('...symbol=^DJI...'); ) and each button/link is sending specific Stock symbol's info to the getStockInfo.php file ( src="/getStockInfo.php?Stocks[NASD]=NASDAQ" or src="...Stocks[SPX]=SP500" or src="...Stocks[DJI]=DOW" ) where the stock quotes are being fetched. 从他们的代码(HTML部分)看起来一切都是硬编码的( open('...symbol=^NASD...');open('...symbol=^SPX...');open('...symbol=^DJI...'); )并且每个按钮/链接都将特定股票代码的信息发送到getStockInfo.php文件( src="/getStockInfo.php?Stocks[NASD]=NASDAQ"src="...Stocks[SPX]=SP500"src="...Stocks[DJI]=DOW" )正在提取股票报价。 There is absolutely NO way my client's users can provide any other stock symbols through the site to change the display, the only way to manipulate the symbols are by changing the code itself. 我的客户的用户绝对没有办法通过网站提供任何其他股票代码来改变显示,操纵符号的唯一方法是改变代码本身。

BUT, my client wants to implement these above 2 conditions in the code anyhow. 但是,我的客户想要无论如何在代码中实现上述两个条件。 And I am not sure how to do this. 我不知道该怎么做。

Not sure if I was able to explain my problem properly :( But I really need some help. Also I'm sorry for not being able to provide any link to the actual page here. Thank you so much for reading my confusing post and investing your time!! :) 不确定我是否能够正确解释我的问题:(但我真的需要一些帮助。此外,我很抱歉无法提供任何链接到这里的实际页面。非常感谢您阅读我困惑的帖子和投资你的时间!! :)

Here's a proof of concept: 这是一个概念证明:

if (array_key_exists("Stocks", $_GET)) {
    $stocks = array_filter($_GET['Stocks'], 'filterStocks');
    foreach ($stocks as $symbol => $stock) {
        print file_get_contents(…);
    }
}

function filterStocks($symbol) {
    return in_array(
        $symbol, 
        array('SP500', 'SPX', 'DOW', 'NASDAQ')
    )
}

Now getStockInfo.php will only return data for the four symbols. 现在getStockInfo.php只返回四个符号的数据。 If you need that configurable on an individual user basis, a simple solution would be to do change the filterStocks function and callback to 如果您需要在单个用户的基础上进行配置,一个简单的解决方案是更改filterStocks函数和回调

function filterStocksForLoggedInUser($symbol) {
    return in_array($symbol, getAllowedSymbolsForUser());
}

function getAllowedSymbolsForUser()
{
    $permissions = include '/path/to/permissions/file.php';
    return isset($permissions[$_SESSION['username']])
        ? $permissions[$_SESSION['username']]
        : array();
    }
}

and then in the permissions file put 然后在权限文件中

return array(
    'Walahh' => array('SP500', 'SPX', 'DOW', 'NASDAQ'),
    'JohnDoe' => array('SP500', 'GOOG')
);

Note 1: the above assumes you have some sort of way to identify users, here $_SESSION['username'] . 注1:以上假设您有某种方式来识别用户,这里是$_SESSION['username'] Change that with whatever you are using and adjust the permission file accordingly. 使用您正在使用的任何内容更改它并相应地调整权限文件。

Note 2: the permissions file will be read each time from disk. 注意2:每次从磁盘读取权限文件。 Disk I/O is usually slow, so you might want to consider moving the permissions to someplace faster. 磁盘I / O通常很慢,因此您可能需要考虑将权限更快地移动到某个位置。

Note 3: this is just a proof of concept. 注3:这只是一个概念证明。 It's very pragmatic. 这非常务实。 You can certainly improve the design and structure, but I guess it's good enough to illustrate how to approach the problem. 你当然可以改进设计和结构,但我想它足以说明如何解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM