聚合数据 --API 股票接口

Posted 奔跑的葛根

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了聚合数据 --API 股票接口相关的知识,希望对你有一定的参考价值。

<!--?php
// +----------------------------------------------------------------------
// | JuhePHP [ NO ZUO NO DIE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2010-2015 http://juhe.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Juhedata <[email protected]>
// +----------------------------------------------------------------------
 
//----------------------------------
// 股票数据调用示例代码 - 聚合数据
// 在线接口文档:http://www.juhe.cn/docs/21
//----------------------------------
 
header(‘Content-type:text/html;charset=utf-8‘);
 
 
//配置您申请的appkey
$appkey = "*********************";
 
 
 
 
//************1.沪深股市************
$url = "http://web.juhe.cn:8080/finance/stock/hs";
$params = array(
      "gid" => "",//股票编号,上海股市以sh开头,深圳股市以sz开头如:sh601009
      "key" => $appkey,//APP Key
);
$paramstring = http_build_query($params);
$content = juhecurl($url,$paramstring);
$result = json_decode($content,true);
if($result){
    if($result[‘error_code‘]==‘0‘){
        print_r($result);
    }else{
        echo $result[‘error_code‘].":".$result[‘reason‘];
    }
}else{
    echo "请求失败";
}
//**************************************************
 
 
 
 
//************2.香港股市************
$url = "http://web.juhe.cn:8080/finance/stock/hk";
$params = array(
      "num" => "",//股票代码,如:00001 为“长江实业”股票代码
      "key" => $appkey,//APP Key
);
$paramstring = http_build_query($params);
$content = juhecurl($url,$paramstring);
$result = json_decode($content,true);
if($result){
    if($result[‘error_code‘]==‘0‘){
        print_r($result);
    }else{
        echo $result[‘error_code‘].":".$result[‘reason‘];
    }
}else{
    echo "请求失败";
}
//**************************************************
 
 
 
 
//************3.美国股市************
$url = "http://web.juhe.cn:8080/finance/stock/usa";
$params = array(
      "gid" => "",//股票代码,如:aapl 为“苹果公司”的股票代码
      "key" => $appkey,//APP Key
);
$paramstring = http_build_query($params);
$content = juhecurl($url,$paramstring);
$result = json_decode($content,true);
if($result){
    if($result[‘error_code‘]==‘0‘){
        print_r($result);
    }else{
        echo $result[‘error_code‘].":".$result[‘reason‘];
    }
}else{
    echo "请求失败";
}
//**************************************************
 
 
 
 
//************4.香港股市列表************
$url = "http://web.juhe.cn:8080/finance/stock/hkall";
$params = array(
      "key" => $appkey,//您申请的APPKEY
      "page" => "",//第几页,每页20条数据,默认第1页
);
$paramstring = http_build_query($params);
$content = juhecurl($url,$paramstring);
$result = json_decode($content,true);
if($result){
    if($result[‘error_code‘]==‘0‘){
        print_r($result);
    }else{
        echo $result[‘error_code‘].":".$result[‘reason‘];
    }
}else{
    echo "请求失败";
}
//**************************************************
 
 
 
 
//************5.美国股市列表************
$url = "http://web.juhe.cn:8080/finance/stock/usaall";
$params = array(
      "key" => $appkey,//您申请的APPKEY
      "page" => "",//第几页,每页20条数据,默认第1页
);
$paramstring = http_build_query($params);
$content = juhecurl($url,$paramstring);
$result = json_decode($content,true);
if($result){
    if($result[‘error_code‘]==‘0‘){
        print_r($result);
    }else{
        echo $result[‘error_code‘].":".$result[‘reason‘];
    }
}else{
    echo "请求失败";
}
//**************************************************
 
 
 
 
//************6.深圳股市列表************
$url = "http://web.juhe.cn:8080/finance/stock/szall";
$params = array(
      "key" => $appkey,//您申请的APPKEY
      "page" => "",//第几页(每页20条数据),默认第1页
);
$paramstring = http_build_query($params);
$content = juhecurl($url,$paramstring);
$result = json_decode($content,true);
if($result){
    if($result[‘error_code‘]==‘0‘){
        print_r($result);
    }else{
        echo $result[‘error_code‘].":".$result[‘reason‘];
    }
}else{
    echo "请求失败";
}
//**************************************************
 
 
 
 
//************7.沪股列表************
$url = "http://web.juhe.cn:8080/finance/stock/shall";
$params = array(
      "key" => $appkey,//您申请的APPKEY
      "page" => "",//第几页,每页20条数据,默认第1页
);
$paramstring = http_build_query($params);
$content = juhecurl($url,$paramstring);
$result = json_decode($content,true);
if($result){
    if($result[‘error_code‘]==‘0‘){
        print_r($result);
    }else{
        echo $result[‘error_code‘].":".$result[‘reason‘];
    }
}else{
    echo "请求失败";
}
//**************************************************
 
 
 
 
 
/**
 * 请求接口返回内容
 * @param  string $url [请求的URL地址]
 * @param  string $params [请求的参数]
 * @param  int $ipost [是否采用POST形式]
 * @return  string
 */
function juhecurl($url,$params=false,$ispost=0){
    $httpInfo = array();
    $ch = curl_init();
 
    curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
    curl_setopt( $ch, CURLOPT_USERAGENT , ‘JuheData‘ );
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
    curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    if( $ispost )
    {
        curl_setopt( $ch , CURLOPT_POST , true );
        curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
        curl_setopt( $ch , CURLOPT_URL , $url );
    }
    else
    {
        if($params){
            curl_setopt( $ch , CURLOPT_URL , $url.‘?‘.$params );
        }else{
            curl_setopt( $ch , CURLOPT_URL , $url);
        }
    }
    $response = curl_exec( $ch );
    if ($response === FALSE) {
        //echo "cURL Error: " . curl_error($ch);
        return false;
    }
    $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
    $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
    curl_close( $ch );
    return $response;
}

 

以上是关于聚合数据 --API 股票接口的主要内容,如果未能解决你的问题,请参考以下文章

数据接口-免费版(股票数据API)

股票API

ajax从新浪获取实时股票数据

利用python获取股票数据

如何使用新浪股票接口获取恒生指数及美国的一些股票指数

聚合API-短信接口,THINKPHP整合聚合api短信接口