Home  >  Article  >  Backend Development  >  PHP stock information query class

PHP stock information query class

WBOY
WBOYOriginal
2016-07-29 09:12:451111browse

http://www.51boxue.net/phper/php-cy/95985.htm

For the stock information query function, we need to capture third-party data, and then we analyze the data to form what we want. Let's take a look at a PHP stock information query class.
GitHub address: Github.com/widuu/stock

<?php
class stock{

    /**
     * 股票数据接口
     */

    const STOCK_URL = "http://apis.baidu.com/apistore/stockservice/stock";
   
    /**
     * 通过拼音或者汉字获取股票代码
     */

    const SOCKET_SUGGEST = "http://cjhq.baidu.com/suggest?code5=";

    /**
     * 单态实例
     */

    private static $instance;

    /**
     * API 密钥
     */

    private static $apikey;

    /**
     * 实例化类和指定API KEY
     * @param  apikey  string
     * @return instance object
     */

    public static function getInstance($apikey){
       
        if( self::$instance == NULL ){
            self::$instance = new self;
            self::$apikey = $apikey;
        }

        return self::$instance;
    }

    /**
     * 获取股票名称
     * @param  stockid    string
     * @return stockName  string
     */

    public static function getName($stockid){
        $result = self::getSingleStock($stockid);
        return $result[&#39;name&#39;];
    }

    /**
     * 获取最后更新时间
     * @param  stockid string
     * @return time    string
     */

    public static function getTime($stockid){
        $result = self::getSingleStock($stockid);
        return $result[&#39;date&#39;].$result[&#39;time&#39;];
    }

    /**
     * 获取K线图地址
     * @param  stockid  string
     * @param  date     string  min/day/week/mouth
     * @return imageUrl string
     */

    public static function getKline($stockid,$date=&#39;min&#39;){
        $result = self::getSingleStock($stockid);
        return $result[&#39;klinegraph&#39;][$date.&#39;url&#39;];
    }

    /**
     * 抓取整只股票的数据
     * @param  stockid  string
     * @return stock infomation array
     */

    public static function getSingleStock($stockid){
        $type = preg_match(&#39;/(\d+){6}/is&#39;, $stockid);
        if ( $type == 0 ){
            $stockid = self::getStockId($stockid);
        }
        $stock_url = self::STOCK_URL."?stockid=".$stockid;
        $result = self::httpGet( $stock_url , true );
        if( $result[&#39;errNum&#39;] != 0 ){
            throw new <strong>Exception</strong>($result[&#39;errMsg&#39;], 1);
            return;
        }
        return $result[&#39;retData&#39;];
    }

    /**
     * 输入拼音或者汉字来获取股票代码
     * @param  name    string
     * @return stockid string
     */

    private static function getStockId($name){
        $result = self::httpGet( self::SOCKET_SUGGEST.urlencode(iconv(&#39;utf-8&#39;, &#39;GBK&#39;, $name)),false );
        if (empty($result)){
            throw new <strong>Exception</strong>("stock name not exists", 2);
            return;
        }
        $stockid = $result[&#39;Result&#39;][0][&#39;code&#39;];
        $stock   = explode(&#39;.&#39;, $stockid);
        return   $stock[1].$stock[0];
    }

    /**
     * GET获取方法
     * @param  param string  参数
     * @author widuu
     */

    private static function httpGet($url,$header=false) {
        $curlHandle = curl_init();
        curl_setopt( $curlHandle , CURLOPT_URL, $url );
        if( $header ){
           curl_setopt( $curlHandle , CURLOPT_HTTPHEADER  , array(&#39;apikey:&#39;.self::$apikey));
        }
        curl_setopt( $curlHandle , CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $curlHandle , CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt( $curlHandle , CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt( $curlHandle , CURLOPT_TIMEOUT, 10 );
        $content = curl_exec( $curlHandle );
        curl_close( $curlHandle );
        return $header ? json_decode($content,true) :json_decode(iconv(&#39;GBK&#39;,&#39;utf-8&#39;,trim($content)),true);
    }
}

//测试代码
stock::getInstance("5040bcbfebb0a4cffc7be278723255aa");
print_r(stock::getSingleStock(&#39;sh601000&#39;));
echo stock::getKline(&#39;紫金矿业&#39;);
?>

The above introduces the PHP stock information query class, including Exception, Git, and github. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn