This article describes the stock information query class implemented by PHP based on curl. Share it for your reference, as follows:
In the stock information query function, we need to capture third-party data, and then analyze these data to form what we want. Let’s take a look at a php stock information query class.
Today, a friend of mine asked me to help write a stock query class to integrate it into WeChat, so I spent a little time writing an incomplete one. Haha, if anyone wants to play, you can continue to submit the code to make it perfect! !
GitHub address:/widuu/stock, the code is as follows:
class stock{ /** * Stock data interface */ const STOCK_URL = "/apistore/stockservice/stock"; /** * Get the stock code through pinyin or Chinese characters */ const SOCKET_SUGGEST = "/suggest?code5="; /** * Single state instance */ private static $instance; /** * API key */ private static $apikey; /** * Instantiate the class and specify the 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; } /** * Get the stock name * @param stockid string * @return stockName string */ public static function getName($stockid){ $result = self::getSingleStock($stockid); return $result['name']; } /** * Get the last update time * @param stockid string * @return time string */ public static function getTime($stockid){ $result = self::getSingleStock($stockid); return $result['date'].$result['time']; } /** * Get the K-line chart address * @param stockid string * @param date string min/day/week/mouth * @return imageUrl string */ public static function getKline($stockid,$date='min'){ $result = self::getSingleStock($stockid); return $result['klinegraph'][$date.'url']; } /** * Grab the data of the whole stock * @param stockid string * @return stock information array */ public static function getSingleStock($stockid){ $type = preg_match('/(\d+){6}/is', $stockid); if ( $type == 0 ){ $stockid = self::getStockId($stockid); } $stock_url = self::STOCK_URL."?stockstock name not exists", 2); return; } $stockid = $result['Result'][0]['code']; $stock = explode('.', $stockid); return $stock[1].$stock[0]; } /** * GET acquisition method * @param param string parameters * @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('apikey:'.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('GBK','utf-8',trim($content)),true); } } //Test codestock::getInstance("5040bcbfebb0a4cffc7be278723255aa"); print_r(stock::getSingleStock('sh601000')); echo stock::getKline('Zijin Mining');
For more information about PHP related content, please check out the topic of this site:Summary of the usage of php curl》、《Complete collection of PHP array (Array) operation techniques》、《Summary of php sorting algorithm》、《Summary of common traversal algorithms and techniques for PHP》、《PHP data structure and algorithm tutorial》、《Summary of PHP Programming Algorithm》、《Summary of PHP mathematical operation skills》、《Summary of usage of php regular expressions》、《Summary of PHP operations and operator usage》、《Summary of usage of php strings"and"Summary of common database operation techniques for php》
I hope this article will be helpful to everyone's PHP programming.