本文实例讲述了php基于curl实现的股票信息查询类。分享给大家供大家参考,具体如下:
股票信息查询功能我们是需要抓取第三方的数据,然后我们再把这些数据进行分析组成自己想要的,下面我们来看一个php 股票信息查询类.
今天一个二逼朋友让我帮忙写个股票查询的类,来集成到微信中,所以花了一点时间写了一个不完整的,哈哈,如果有想玩的人,可以继续提交代码,让它变得完善起来!!
GitHub 地址:github.com/widuu/stock,代码如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
class stock{
/**
* 股票数据接口
*/
const STOCK_URL = "http://apis.baidu.com/apistore/stockservice/stock" ;
/**
* 通过拼音或者汉字获取股票代码
*/
const SOCKET_SUGGEST = "http://cjhq.baidu.com/suggest?code5=" ;
/**
* 单态实例
*/
private static $instance ;
/**
* API 密钥
*/
private static $apikey ;
/**
* 实例化类和指定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 ;
}
/**
* 获取股票名称
* @param stockid string
* @return stockName string
*/
public static function getName( $stockid ){
$result = self::getSingleStock( $stockid );
return $result [ 'name' ];
}
/**
* 获取最后更新时间
* @param stockid string
* @return time string
*/
public static function getTime( $stockid ){
$result = self::getSingleStock( $stockid );
return $result [ 'date' ]. $result [ 'time' ];
}
/**
* 获取K线图地址
* @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' ];
}
/**
* 抓取整只股票的数据
* @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' ];
}
/**
* 输入拼音或者汉字来获取股票代码
* @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获取方法
* @param param string 参数
* @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);
}
}
//测试代码
stock::getInstance( "5040bcbfebb0a4cffc7be278723255aa" );
print_r(stock::getSingleStock( 'sh601000' ));
echo stock::getKline( '紫金矿业' );
|
希望本文所述对大家PHP程序设计有所帮助。
相关文章
猜你喜欢
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 49
-
2025-05-29 14
-
2025-05-25 30
-
2025-06-04 38
-
2025-06-04 26
热门评论