php 股票信息查询类

股票信息查询功能我们是需要抓取第三方的数据,然后我们再把这些数据进行分析组成自己想要的,下面我们来看一个php 股票信息查询类.

今天一个二逼朋友让我帮忙写个股票查询的类,来集成到微信中,所以花了一点时间写了一个不完整的,哈哈,如果有想玩的人,可以继续提交代码,让它变得完善起来!!

GitHub 地址:github.com/widuu/stock,代码如下:

  1. class stock{
  2. /**
  3. * 股票数据接口
  4. */
  5. const STOCK_URL = "http://apis.baidu.com/apistore/stockservice/stock";
  6. /**
  7. * 通过拼音或者汉字获取股票代码
  8. */
  9. const SOCKET_SUGGEST = "http://cjhq.baidu.com/suggest?code5=";
  10. /**
  11. * 单态实例
  12. */
  13. private static $instance;
  14. /**
  15. * API 密钥
  16. */
  17. private static $apikey;
  18. /**
  19. * 实例化类和指定API KEY
  20. * @param apikey string
  21. * @return instance object
  22. */
  23. public static function getInstance($apikey){
  24. if( self::$instance == NULL ){
  25. self::$instance = new self;
  26. self::$apikey = $apikey;
  27. }
  28. return self::$instance;
  29. }
  30. /**
  31. * 获取股票名称
  32. * @param stockid string
  33. * @return stockName string
  34. */
  35. public static function getName($stockid){
  36. $result = self::getSingleStock($stockid);
  37. return $result['name'];
  38. }
  39. /**
  40. * 获取最后更新时间
  41. * @param stockid string
  42. * @return time string
  43. */
  44. public static function getTime($stockid){
  45. $result = self::getSingleStock($stockid);
  46. return $result['date'].$result['time'];
  47. }
  48. /**
  49. * 获取K线图地址
  50. * @param stockid string
  51. * @param date string min/day/week/mouth
  52. * @return imageUrl string
  53. */
  54. public static function getKline($stockid,$date='min'){
  55. $result = self::getSingleStock($stockid);
  56. return $result['klinegraph'][$date.'url'];
  57. }
  58. /**
  59. * 抓取整只股票的数据
  60. * @param stockid string
  61. * @return stock infomation array
  62. */
  63. public static function getSingleStock($stockid){
  64. $type = preg_match('/(\d+){6}/is', $stockid);
  65. if ( $type == 0 ){
  66. $stockid = self::getStockId($stockid);
  67. }
  68. $stock_url = self::STOCK_URL."?stockid=".$stockid;
  69. $result = self::httpGet( $stock_url , true );
  70. if( $result['errNum'] != 0 ){
  71. throw new Exception($result['errMsg'], 1);
  72. return;
  73. }
  74. return $result['retData'];
  75. }
  76. /**
  77. * 输入拼音或者汉字来获取股票代码
  78. * @param name string
  79. * @return stockid string
  80. */
  81. private static function getStockId($name){
  82. $result = self::httpGet( self::SOCKET_SUGGEST.urlencode(iconv('utf-8', 'GBK', $name)),false );
  83. if (emptyempty($result)){
  84. throw new Exception("stock name not exists", 2);
  85. return;
  86. }
  87. $stockid = $result['Result'][0]['code'];
  88. $stock = explode('.', $stockid);
  89. return $stock[1].$stock[0];
  90. }
  91. /**
  92. * GET获取方法
  93. * @param param string 参数
  94. * @author widuu
  95. */
  96. private static function httpGet($url,$header=false) {
  97. $curlHandle = curl_init();
  98. curl_setopt( $curlHandle , CURLOPT_URL, $url );
  99. if( $header ){
  100. curl_setopt( $curlHandle , CURLOPT_HTTPHEADER , array('apikey:'.self::$apikey));
  101. }
  102. curl_setopt( $curlHandle , CURLOPT_RETURNTRANSFER, 1 );
  103. curl_setopt( $curlHandle , CURLOPT_SSL_VERIFYPEER, false);
  104. curl_setopt( $curlHandle , CURLOPT_SSL_VERIFYHOST, false);
  105. curl_setopt( $curlHandle , CURLOPT_TIMEOUT, 10 );
  106. $content = curl_exec( $curlHandle );
  107. curl_close( $curlHandle );
  108. return $header ? json_decode($content,true) :json_decode(iconv('GBK','utf-8',trim($content)),true);
  109. } //phpfensi.com
  110. }
  111. //测试代码
  112. stock::getInstance("5040bcbfebb0a4cffc7be278723255aa");
  113. print_r(stock::getSingleStock('sh601000'));
  114. echo stock::getKline('紫金矿业');