This article describes the PHP based on curl implementation of the stock information query class. Share to everyone for your reference, specific as follows:
Stock information query function we need to crawl third party data, and then we analyze the data to make up 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 micro-letter, so spent a little time to write an incomplete, haha, if there are people who want to play, 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";
/** * through pinyin or Chinese characters to get stock code/Const SOCKET_SUGGEST = "http://cjhq.baidu.com/suggest?code5=";
/** * Single State example * * private static $instance;
/** * API Key * * private static $apikey; /** * Instantiate class and specify 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 stock Name * @param stockid String * @return StockName string/public static function GetName ($STOC
Kid) {$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 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 whole stock data * @param stockid String * @return the infomation array/public static function Getsi
Nglestock ($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 stock code * @param name String * @return Stockid string/private static function Getstock Id ($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 Fetch method * @param param string parameter * @author Widuu/private static function HttpGet ($url, $header =f
Alse) {$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");
Print_r (Stock::getsinglestock (' sh601000 '));
echo stock::getkline (' Zijin mining ');
More about PHP Interested readers can view the site topics: "Php Curl Usage Summary", "PHP array" operation Skills Daquan, "PHP Sorting algorithm Summary", "PHP common traversal algorithm and skills summary", "PHP Data structure and algorithm tutorial", " PHP Programming algorithm Summary, "PHP Mathematical Calculation Skills Summary", "PHP Regular Expression Usage summary", "PHP operation and operator Usage Summary", "PHP string (String) Usage summary" and "PHP common database Operation skill Summary"
I hope this article will help you with the PHP program design.