Php curl-based Stock Information Query Class instance, curl Information Query

Source: Internet
Author: User

Php curl-based Stock Information Query Class instance, curl Information Query

This example describes 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 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 f Unction 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 s Tatic 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 ');

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.