Php curl-based stock information query instance

Source: Internet
Author: User
This article mainly introduces the curl-based php stock information query class, and analyzes the related operation skills of php using curl to call the API interface to realize the stock information query function in the form of a complete instance, you can refer to the example in this article to describe the stock information query class implemented by php based on curl. We will share this with you for your reference. The details are as follows:

We need to capture third-party data for the stock information query function, and then analyze the data to make up what we want. next we will 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 the middle, so I spent a little time writing an incomplete one. haha, if someone wants to play, you can continue to submit code to make it perfect !!

GitHub address: github.com/widuu/stock. the code is as follows:

Class stock {/*** stock data interface */const STOCK_URL =" http://apis.baidu.com/apistore/stockservice/stock ";/*** Get the stock code through pinyin or Chinese characters */const SOCKET_SUGGEST =" http://cjhq.baidu.com/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 functio N 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 Kline 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'];}/*** capture the data of the entire stock * @ param stockid string * @ return stock infomation 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. "? Stockid = ". $ stockid; $ result = self: httpGet ($ stock_url, true); if ($ result ['errnum']! = 0) {throw new Exception ($ result ['errmsg '], 1); return;} return $ result ['retdata'];} /*** enter pinyin or Chinese characters to get the stock code * @ param name string * @ return stockid string */private static function getStockId ($ name) {$ result = self :: httpGet (self: SOCKET_SUGGEST.urlencode (iconv ('utf-8', 'gbk', $ name), false); if (emptyempty ($ result )) {throw new Exception ("stock name not exists", 2); return ;}$ stockid = $ result ['Result'] [0] ['code']; $ stock = explode ('. ', $ stockid); return $ stock [1]. $ stock [0];}/*** GET method * @ param string parameter * @ 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, CU Cursor, 1); curl_setopt ($ curlHandle, handle, 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 code stock:: getInstance ("5040bcbfebb0a4cffc7be278723255aa"); print_r (stock: getSingleStock ('sh601000'); echo stock: getKline ('zijin mining ');

I hope this article will help you with PHP programming.

For more articles about php stock information query instances based on curl, refer to PHP Chinese network!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.