{ "resultcode": "200", "reason": "SUCCESSED!", "result": [{ "1": { "variety": "Ag(T+D)", /*品种*/ "latestpri": "6585.00", /*最新价*/ "openpri": "6712.00", /*开盘价*/ "maxpri": "6721.00", /*最高价*/ "minpri": "6581.00", /*最低价*/ "limit": "-1.98%", /*涨跌幅*/ "yespri": "6718.00", /*昨收价*/ "totalvol": "1564524.0000", /*总成交量*/ "time": "2012-12-19 15:29:59" /*更新时间*/ }, ...... "2": { "variety": "Au(T+D)", "latestpri": "336.60", "openpri": "341.00", "maxpri": "341.40", "minpri": "336.00", "limit": "-1.52%", "yespri": "341.81", "totalvol": "22842.0000", "time": "2012-12-19 15:29:30" }, "7": { "variety": "Au99.99", "latestpri": "336.77", "openpri": "342.00", "maxpri": "342.00", "minpri": "336.40", "limit": "-1.53%", "yespri": "342.00", "totalvol": "6936.2000", "time": "2012-12-19 15:29:53" } ...... }] }
<?php /** * Created by PhpStorm. * User: FZS * Time: 2019/3/15 17:50 */ //---------------------------------- // 聚合 黄金数据 调用类 //---------------------------------- class freeApi{ private $apiKey = false; //聚合appkey private $apiUrl = 'http://web.juhe.cn:8080/finance/gold/shgold'; public function __construct($apikey){ $this->apiKey = $apikey; } /** * 将JSON内容转为数据,并返回 * @param string $content [内容] * @return array */ public function returnArray($content){ return json_decode($content,true); } /** * 获取 黄金数据 结果 * @return array */ public function getResult(){ $params = [ "key" => $this->apiKey,//应用APPKEY(应用详细页查询) "v" => "",//JSON格式版本(0或1)默认为0 ]; $params = http_build_query($params); return $this->returnArray($this->freeApiCurl($this->apiUrl,$params,1)); } /** * 请求接口返回内容 * @param string $url [请求的URL地址] * @param string $params [请求的参数] * @param int $ipost [是否采用POST形式] * @return string */ public function freeApiCurl($url,$params=false,$ispost=0){ $ch = curl_init(); curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 ); curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 ); curl_setopt( $ch, CURLOPT_USERAGENT , 'chuanshuoapi' ); curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 ); curl_setopt( $ch, CURLOPT_TIMEOUT , 60); curl_setopt( $ch, CURLOPT_RETURNTRANSFER , 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) { return false; } curl_close( $ch ); return $response; } }