当前位置: 移动技术网 > IT编程>开发语言>PHP > php基于curl实现的股票信息查询类实例

php基于curl实现的股票信息查询类实例

2017年12月12日  | 移动技术网IT编程  | 我要评论

2018开学第一课视频,五金机械网,波多野结衣图片

本文实例讲述了php基于curl实现的股票信息查询类。分享给大家供大家参考,具体如下:

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

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

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

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相关内容感兴趣的读者可查看本站专题:《php curl用法总结》、《php数组(array)操作技巧大全》、《php排序算法总结》、《php常用遍历算法与技巧总结》、《php数据结构与算法教程》、《php程序设计算法总结》、《php数学运算技巧总结》、《php正则表达式用法总结》、《php运算与运算符用法总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总

希望本文所述对大家php程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网