PHP版本的大智慧股票API接口demo

一、         大智慧官网地址:http://yun.gw.com.cn/index.htmlphp

二、         大智慧API文档中心:http://yun.gw.com.cn/DocCenter/(技术接入文档)html

去加了他们的QQ群,而后找人申请了一个月的接口试用期..接口申请下来后发现他们官方的github上面的demo只有java的,还有两个是Android和桌面应用的..我要对接到网站上面,因此只有本身来写一个..java

目前用的是TP5框架,,因此有些写法须要本身去修改,代码只提供大致的流程git

<?php
/**
 * Created by PhpStorm.
 * User: Martinby
 * Date: 2017/10/19
 * Time: 14:42
 */
namespace app\index\controller;
use Pingpp\Token;
use think\Cache;

class Dzh extends Base{
    //接口网站
    private $host="https://gw.yundzh.com";
    //申请的appid之类的东东
    private $appid="****************";
    private $secret_key="******";
    private $short_appid="*****"; 



    public function get_info(){
        $path="/f10/gsgk";
        //获取token,若是没有未获取到token的缓存,那么请求token,并将其加入到缓存中去,,若是有token缓存,直接获取缓存中的token
        $token=array();
        if(Cache::get("token_cache")==false){
            $token=$this->get_token();
            Cache::set("token_cache",$token,$token['duration']);
        }else{
            $token=Cache::get("token_cache");
        }

        if($token!==false){
            $params=[
                "qid"=>'123456',  // qid 惟一指定经过该链接通道已被发送的请求标识(貌似能够随便乱写个数字?)
                "obj"=>"SO831621",//股票代码

                //"token"=>$token['token'],//token
            ];
            $params_string=http_build_query($params);


            $real_url=$this->host.$path."?".$params_string."&token=".$token['token'];
            $result=$this->dzh_curl($real_url);
            var_dump($real_url);echo "<br/>";echo("<br/>");
            var_dump($result);

        }


    }
    //获取热乎乎的token值
    public function get_token(){
        $path="/token/access";
        $params=[
            "appid"=>$this->appid,
            "secret_key"=>$this->secret_key,
        ];
        $params_string=http_build_query($params);
        $real_url=$this->host.$path.'?'.$params_string;
        $res=$this->dzh_curl($real_url);
        //若是err为0,即无错状况
        if($res->Err=='0'){

            //数据data(stdClass)
            $data=$res->Data;
            //获取token数据(stdClass )
            $RepDataToken=$data->RepDataToken;
            //获取result返回值 0 受权成功,-1 受权失败,-2 appid不存在,-3 appid受权数已满
            $result=$RepDataToken[0]->result;
            //转换object为数组
            $token_arr=$this->object_array($RepDataToken[0]);

            //若是result不为0,即受权失败
            if($result!==0){
                return false;
            }else{
                //var_dump($token_arr);
                return $token_arr;
            }
        }else{

        }


    }


    //执行get请求
    public function dzh_curl($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //curl_setopt($ch, CURL_SSLVERSION_SSL, 2);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $data = curl_exec($ch);
        if($data===false){
            return false;
        }else{
            $response = json_decode($data);
            return $response;
        }

    }



    //转换(stdclass)object对象为(array)数组
    function object_array($object) {
        $object =  json_decode( json_encode( $object),true);
        return  $object;
    }


}
相关文章
相关标签/搜索