PHP based on curl implementation of stock information query class instance

Source: Internet
Author: User
Tags php programming
This paper describes the stock information query class based on curl implemented by PHP. Share to everyone for your reference, as follows:

Stock information query function we need to crawl the data of the third party, and then we analyze the data to compose what we want, let's look at a PHP stock information query class.

Today a two-cornered friend let me help write a stock query class, to integrate into the, so spent a little time to write an incomplete, haha, if you want to play the person, you can continue to submit code, let it become perfect up!!

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";  /** * Obtain stock code by pinyin or kanji */Const SOCKET_SUGGEST = "http://cjhq.baidu.com/suggest?code5=";  /** * * Single-state instance */private static $instance;  /** * API key */private static $apikey; /** * Instantiate class and specify API KEY * @param apikey String * @return Instance Object */public static function getinstance ($apik      EY) {if (self:: $instance = = NULL) {self:: $instance = new Self;    Self:: $apikey = $apikey;  } return Self:: $instance;    /** * Get stock Name * @param stockid String * @return stockname String */public static function GetName ($stockid) {    $result = Self::getsinglestock ($stockid);  return $result [' name '];    /** * Get Last Updated * @param stockid String * @return Time String */public static function GetTime ($stockid) {    $result = Self::getsinglestock ($stockid);  return $result [' Date ']. $result [' time ']; }/** * Get the candlestick 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 entire stock data * @param stockid String * @return Stock Infomation Array */public static function Getsinglest    Ock ($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 kanji to get stock code * @param name String * @return Stockid String */private static function Getstockid ($na    Me) {$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 Get method * @param 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, 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 Code stock::getinstance ("5040BCBFEBB0A4CFFC7BE278723255AA");p Rint_r (Stock::getsinglestock (' sh601000 ')); Echo Stock::getkliNE (' Zijin mining '); 

I hope this article is helpful to you in PHP programming.

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.